Showing posts with label guava. Show all posts
Showing posts with label guava. Show all posts

Monday, June 10, 2013

Guava - simple recipes to make your Java code cleaner, 1st part

It isn't article for peoples knowing Guava. It is set of simple examples to encourage to use Guava Library in your code.

#1. You can use Optional instead of simply returning null in some specific cases:

Insted of:
     /**  
     * Can return null in specific cases... but it's
     * hard to remember
     */
    public static String someMethod() {
        String returnValue = null;
        if (new Date().getTime() % 2 == 0) {
            returnValue = "time % 2 == 0";
        }
        return returnValue;
    }

    public static void main(String[] args) {
        String str = someMethod();
        str.contains("%") // will crash
    }
use this:
    /**
     * Explicite shows, that method can
     * return empty (null) value
     */
    public static Optional< String > someMethod() {
        Optional< String > returnValue = Optional.absent();
        if (new Date().getTime() % 2 == 0) {
            returnValue = Optional.of(new String());
        }
        return returnValue;
    }

    public static void main(String[] args) {
        Optional< String > str  = someMethod();
        if(str.isPresent()) {
            // here you know that value is not null
        }
        // or you can operate on given value or a default one
        str.or("default value").contains("%");
    }

#2 You can use firstNonNull from Guavas Objects class insted of write "if else"

Insted of:
    public T foo() {
        ...
        ...
        if(first != null) {
            return first;
        } else {
            return second;
        }
    }
use this:
import static com.google.common.base.Objects.firstNonNull;
...
    public T foo() {
        ...
        ...
        return firstNonNull(first, second);
    }

#3 You can use Guava Strings class methods to deal with null or empty Strings

Insted of:
if(str == null) {
    str = "";
}

if("".equals(str)) {
    str = null;
}

if(str == null || str.length() == 0) {
   // is null or empty
}
use this:
import static com.google.common.base.Strings.*;

str = nullToEmpty(str);
str = emptyToNull(str);
if(isNullOrEmpty(str)) {
   // is null or empty
}

#4 You can use Object.equals(a, b) to check equality safely

Insted of:
a.equals(b); // will crash if a is null
use this:
import static com.google.common.base.Objects.equal;
...
equal(a, null); // return false
equal(null, null); // return true
equal(a, b); // return true if a is equal b

#5 You can use Joiner to join Strings

Insted of:
StringBuffer buffer = new StringBuffer();
for (String str : strs) {
    if (str != null) {
        buffer.append(str);
        buffer.append(", ");
    }
}
if (buffer.length() >= 2) {
    buffer.substring(0, buffer.length() - 2);
}
return buffer.toString();
use this:
import com.google.common.base.Joiner;
...
return Joiner.on(", ").skipNulls().join(strs);

#6 You can use Splitter to split String

Insted of:
String str = "abc, bcd,, cde   ,zsa";
String[] split = str.split(",");

// What with trimming? Whitespaces? Empty strings???
use this:
import com.google.common.base.Splitter;
...
Splitter.on(',')
       .trimResults()
       .omitEmptyStrings()
       .split("abc, bcd,, cde   ,zsa");

#7 You can use Multiset to count object occurences

Insted of:
Map< String, Integer > countMap = new HashMap< String, Integer >();
for (String word : words) {
    if(!countMap.containsKey(word)) {
        countMap.put(word, 0);
    }
    countMap.put(word, countMap.get(word) + 1);
}
use this:
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
...
Multiset< String > wordsMultiset = HashMultiset.create();
wordsMultiset.addAll(words);

#8 You can use Multimap insted of map with List or Set as values

Insted of:
Map< String, List< String > > languagesMap = new HashMap< String, List< String >>();
for (Programmer programmer : programmers) {
    if (languagesMap.get(programmer.getLanguage()) != null) {
        languagesMap.put(programmer.getLanguage(), new ArrayList< String >());
    }
    languagesMap.get(programmer.getLanguage()).add(programmer.getEmail());
}
use this:
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
...
Multimap< String, String > languagesMap = HashMultimap.create();
for (Programmer programmer : programmers) {
    languagesMap.put(programmer.getLanguage(), programmer.getEmail());
}

...next simple, but usable examples soon...

Monday, April 15, 2013

Guavas Optional... I know where are you from :)

Consider the example of a function that calculates the square root of x. That's make sense if try to calculate square root of a number which is equal or greater than zero, but what if it isn't? Return -1 or throw exception? This does not sound good... Fortunately google-guava provides more elegant solution, which is Optional class. Let the code speak:
import com.google.common.base.Optional;

public class Example {

    public static Optional< Double > sqrt(Double arg) {
        Optional< Double > result = Optional.absent();
        if(arg >= 0.0) {
            result = Optional.of(Math.sqrt(arg));
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(Example.sqrt(-2.0));
        System.out.println(Example.sqrt(2.0));
    }
}
Returning Optional allow us to explicite showing, than in certain circumstances function can return result which dosen't make sense or it's empty or whatever else. What the best? In Scala, this functionality is part of the language... and it is in quite nice and readable form:
class Example {

  def sqrt(arg : Double) = {
    if (arg>=0) {
      Some(math.sqrt(arg))
    } else {
      None
    }
  }
}

object Main {
  def main(args: Array[String]) {
    val example = new Example
    println(example.sqrt(-2))
    println(example.sqrt(2))
  }
}