Thursday, December 08, 2016

Programming Pascal's triangle

What is Pascal's triangle?

It is a triangular array which is consists from binomial coefficients (you can see visual representation of it below).

To get more information check article on wiki: Pascal's triangle.

I had a task where I needed to find out a value in a cell and what I only had were coordinates of it. I came with quit simple solution (Scala) which I really like.

The idea is to move up from the initial cell to the borders (left and right, since I know the values there) and once I am there, I move back to my initial cell but now I can bring some values from above.

object Main {
  def main(args: Array[String]) {    
    println(pascal(4, 3))

  def pascal(c: Int, r: Int): Int = {
    if (c == 0 || r == 0 || c == r) 1
    else pascal(c - 1, r - 1) + pascal(c, r - 1)

Tuesday, March 22, 2016

Use equal for string literal, rather than for an object

When you wanna compare String object with string literal, we often disregard what we compare with what, however there is one and safe way to do it.

It's much better to use equals() and equalsIgnoreCase() for a string literal, instead of Object, because it helps to avoid possible NullPointerException.

Here is an example:
 String a = null;  
 System.out.print("123".equals(a));     // false  
 System.out.print(a.equals("123"));     // java.lang.NullPointerException

Friday, January 08, 2016

Typical mistakes with String in Java

Just few typical mistakes developers do when dealing with Strings. It's common stuff and everybody knows that, but for some reasons I still find such things (even written by myself :-)).

1. Checking empty string

In old days (version 1.5 and lower) we used String.equal(""), but 1.6 brought us String.IsEmpty which is better and faster.
 // wrong/slow  
 if (name.equals("")) {  
 // correct/fast
 if (name.isEmpty()) {

2. Concatenation

Method String.concat creates new String object, it's OK to use it when you do operation only once, otherwise use operator + += operators (see below why).
 String s = "Hello ".concat("new").concat(" world!"); // "Hello new world!"  
Using operators + and +=, they do not use String.concat method but StringBuilder and it's important to know what it means to us.
 String s = "Hello " + "new" + " world!"; // "Hello new world!"
What actually happens when you use + operator is StringBuilder used:
 String s = new StringBuilder().append("Hello ").append("new").append(" world!").toString(); // "Hello new world!"
So conclusion - it's ok to use concat if you do one time operation, for any other situation I would recommend to use + or +=.

3. String Formatting

Very often in order to format string people either concatenate the string to achieve result or do replace or invent something else. I do it as well sometimes (what a shame!). Instead we should use String.format method to do that.
 int n = 10;  
 // wrong  
 String s = "I have " + Integer.toString(10) + " books";  
 // wrong  
 String s = "I have #num books".replace("#num", Integer.toString(10));  
 // correct  
 Strig s = String.format("I have %d books", n);  

I would be glad to hear other typical mistakes we do with String object! So please share your experience.

Thursday, January 07, 2016

SOAP and passing session

Here is an example how to pass session using SOAP envelope approach. I was starlight with it for some time.

That is why people use REST our days :) and not SOAP approach.

   // Create SOAP Connection  
   SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();  
   SOAPConnection soapConnection = soapConnectionFactory.createConnection();  
   // connect to webserivce
   SOAPMessage soapResponse =, password), url);

   // read cookie from response and use it when send another requests
   MimeHeaders session = soapResponse.getMimeHeaders(); 
   String sesisonCookie = session.getHeader("Set-Cookie")[0];

   SOAPMessage soapResponse2 =, url);

Here is how we add cookie to soap request

   SOAPBody soapBody = envelope.getBody();
   SOAPElement soapBodyElem = soapBody.addChildElement("Customer_GetAll", "m");
   soapMessage.getMimeHeaders().addHeader("Cookie", sesisonCookie);

Wednesday, January 06, 2016

Java web-consumer in Domino and cookie session

Recently I've faced with few mistake that drain hours from me. I had to make integration with web-serivce. I imported WSDL file and got java classes generated, than I according to documentation I checked connect method with credentials...
Error connecting to 'endpoint' on port '443', SSL invalid certificate, may need to cross-certify
Spend some time and found very detailed answer on stackoverflow: Create cross certificate for Domino Java agent?

Tried connect method again - it worked! Gonna be easy task now (I thought). However nothing else except connect method worked. All other method of consumer either did nothing or returned null to me. I was confused, spend few hours and than contacted web-service provider, they were not familiar with Java and provided me example in PHP that worked as expected.

My issue was I had to set SESSION_MAINTAIN_PROPERTY to true for my proxy object.

 SecretServiceLocator service = new SecretServiceLocator();  
 SecretPortType port = service.getSecretPort();  
 ((javax.xml.rpc.Stub)port)._setProperty(javax.xml.rpc.Stub.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);  


Standard property: This boolean property is used by a service client to indicate whether or not it wants to participate in a session with a service endpoint. If this property is set to true, the service client indicates that it wants the session to be maintained. If set to false, the session is not maintained. The default value for this property is false.
Once I set session maintain to true - everything started to work.