Monday, November 22, 2010

google's guava library tutorial part 5: back to the primitive(s)

In the fifth part of my tutorial, I'll try to show you the Primitives package of Google Guava library. Primitives package offers several classes to work on primitives or arrays of primitives which are not found in the corresponding wrapper classes (e.g. Boolean for booleans) or Arrays. I will begin with Ints which's the helper class for int related operations. Although it's open to discussion, int is probably the most used primitive type and most of the helper methods are shared among classes in the Primitives package so when I explain Ints, I cover the majority of the methods in the Primitives package.

There are two cast methods in Ints for casting long to int. The first one, saturatedCast(), returns the nearest int value to the given long.


1// max value of long is too large for int
2// thus it'll cast to the nearest int
3// which's the max int value
4 
5int saturatedCast = Ints.saturatedCast(Long.MAX_VALUE);
6System.out.println(saturatedCast);
7//2147483647
8System.out.println(saturatedCast == Integer.MAX_VALUE); // true


1// min value of long is too small for int
2// thus it'll cast to the nearest int
3// which's the min int value
4 
5saturatedCast = Ints.saturatedCast(Long.MIN_VALUE);
6System.out.println(saturatedCast);
7//-2147483648
8System.out.println(saturatedCast == Integer.MIN_VALUE); // true


1// 0 is available in int range [-2147483648, 2147483647]
2// so the nearest value to cast
3// is itself in int
4 
5saturatedCast = Ints.saturatedCast(0L);
6System.out.println(saturatedCast);
7System.out.println(saturatedCast == 0); // true


You'll probably use saturatedCast in cases where using the approximated value wont cause you any problems. In cases you can't take that risk you should use checkedCast(). In cases where you try to cast a long out of int range this method will throw you IllegalArgumentException.

1long long1 = Integer.MAX_VALUE;
2long1++;
3int checkedCast = Ints.checkedCast(long1);
4System.out.println(checkedCast);


You will get java.lang.IllegalArgumentException: Out of range: 2147483648 (which's max int value + 1)

min() (max()) returns smallest (largest) integer in the integer array. Collections class has similar min/max methods but it was not straightforward to transform an int array to a Collection.

01int[] integerArray = new int[]{3, 5, 7, -3};
02 
03System.out.println("Max: "+  Ints.max(integerArray)  ); // 7
04System.out.println("Min: "+  Ints.min(integerArray)  ); // -3
05 
06 
07// There are two indexOf methods
08// if we dont count lastIndexOf()
09 
10// The first one finds the index of the given integer
11// in the given array
12int indexOf = Ints.indexOf(integerArray, 7);
13System.out.println("index of "+7+" in "+Ints.asList(integerArray)+" is "+indexOf);
14// index of 7 in [3, 5, 7, -3] is 2
15 
16// The second one finds the beginning index of the second
17// given array
18// in the first given array
19indexOf = Ints.indexOf(integerArray, new int[]{7, -3});
20System.out.println("index of the given array in "+Ints.join(",", integerArray)+" is "+indexOf);
21// index of the given array in 3,5,7,-3 is 2
22 
23// If it couldnt find the given value
24// it returns -1
25indexOf = Ints.indexOf(integerArray, 6);
26System.out.println("index of "+6+" in "+Ints.join(",", integerArray)+" is "+indexOf);
27// index of 6 in [3, 5, 7, -3] is -1



If you have a sorted int array in hand you better use Arrays.binarySearch() if not then use Ints.indexOf().

You can see the use of join() method. You just give a String separator and an int array for obtaining a string presentation of each element separated by the given separator.

1System.out.println(Ints.join("|", new int[]{3, 4, 5, 6, 2, 7}));
2//output: 3|4|5|6|2|7


There's no good alternative to this without Guava as far as I know. Its not straightforward to convert the int[] to a List so you can call toString() for obtaining a string presentation of int array. You can't use Joiner class either because it's for working on objects rather than primitives.


There's also a lastIndex() method which returns the index of the last occurrence of the given integer

1int[] newIntArray = new int[]{5, 23, 6, 24, 69, 6};
2int lastIndexOf = Ints.lastIndexOf(newIntArray, 6);
3System.out.println("Last index of: "+lastIndexOf);
4// Last index of: 5
5 
6System.out.println(lastIndexOf == (newIntArray.length-1));
7// 6 is the last element so this returns true



indexOf() starts from the beginning and check each element while lastIndexOf() starts from the end. If you just need if the element in hand is part of the given array or not (instead of its place in the array) then you can use contains().

1System.out.println("Is there 69 in the array? "+Ints.contains(newIntArray, 69));
2//true
3System.out.println("Is there 66 in the array? "+Ints.contains(newIntArray, 66));
4//false



concat() concats the given int arrays. Ints.concat({a, b}, {c,d}) will return {a, b, c, d}. One of the old ways of doing this was creating an int array with the sum size of the given arrays and copy their content in the new array by either iterating over them or by using System.arraycopy().

1int[] concatResult = Ints.concat(new int[]{1, 2, 3, 4, 5, 6, 7},
2new int[]{8, 9, 10}, new int[]{11, 12, 13, 14});
3System.out.println(Ints.join("|", concatResult));
4 
5// 1|2|3|4|5|6|7|8|9|10|11|12|13|14


There's a compare() method for comparing two integers. compare(x, y) returns a negative value if x is less than y, a positive value if x is greater than y or zero if they are equal which means that it works the way Integer's compareTo() works.

1System.out.println(Ints.compare(2, 4)); // -1
2System.out.println(Ints.compare(5, 3)); //  1
3System.out.println(Ints.compare(3, 3)); // 0


Assume that I have a Collection of Integers like below and I want an int array from this collection.

01List<integer> integerList =
02Lists.newArrayList(new Integer(0), new Integer(2), new Integer(-8));
03 
04// integerList.toArray() will give me an array of Integers.
05// What if I'd prefer an array of int and not Integer?
06 
07int[] array = Ints.toArray(integerList);
08System.out.println(Ints.join(", ", array));
09// 0, 2, -8
10</integer>


ensureCapacity() returns the given array directly if arrays size is more than the given min length. Else it returns a new array of length (min length+padding) and copies the given array to the newly create array. Extra elements in the new array are given default initialization values. e.g. 0 for int, false for boolean.

01int[] myIntegerArray = new int[]{1, 4, 5, 7, 3, 2};
02int[] ensureCapacity =
03Ints.ensureCapacity(myIntegerArray, 4, 3);
04// len=4, padding=3
05System.out.println(Ints.join(", ", ensureCapacity));
06// the same array is returned
07System.out.println(myIntegerArray == ensureCapacity);
08// true
09 
10int[] ensureCapacity2 =
11Ints.ensureCapacity(myIntegerArray, 10, 3);
12// len=10, padding=3
13System.out.println(Ints.join(", ", ensureCapacity2));
14// a new array is returned with 0s at the end
15System.out.println(ensureCapacity2.length == (10+3));
16// true because the size is len+padding



Ints can hand you a Comparator for comparing int arrays. First I create a List of int arrays. I'll sort them so you can see how the lexicographic comparison works

01ArrayList < int[] > newArrayList =
02Lists.newArrayList( new int[]{1}, new int[]{2},
03new int[]{1, 5, 0}, new int[]{2, 5, 0},
04new int[]{2, 1}, new int[]{}, new int[]{2, 5, 1},
05new int[]{2, 5});
06// Sort the collection in hand
07Collections.sort(newArrayList, Ints.lexicographicalComparator());
08// I do a transformation to see the String presentation
09List < String > transform5 = Lists.transform(newArrayList, new Function < int[], String >(){
10 @Override
11 public String apply(int[] array) {
12  return Ints.join("-", array);
13 }});
14System.out.println("lexicographicalComparator: "+ transform5);
15// lexicographicalComparator: [, 1, 1-5-0, 2, 2-1, 2-5, 2-5-0, 2-5-1]


Empty array is the first as you can see. When one element is the prefix of the other the shorter one is first (as in the case of 1 and 1-5-0).

You can obtain a live view of the array in hand as a List using asList(). Operations that'll cause a problem in the array wont be supported by the List in hand. Updates are bi-directional which means that if you change the live List view the array changes too, and vice versa.

1int[] integerArray2 = new int[]{1, 4, 6, 7};
2List < Integer > newIntegerList = Ints.asList(integerArray2);


You can't add or remove an element to the backing list. This operation will throw you java.lang.UnsupportedOperationException. This restriction is probably due to the fact that the underlying data structure is an array which is in fact fixed-sized thus you cant add or remove elements to/from it.

1//  newIntegerList.add(66);
2//  newIntegerList.remove(0);
3//  newIntegerList.clear();


Set operation will work indeed. See the change in the underlying array.

01// {1, 4, 6, 7}
02newIntegerList.set(0, 2); // put 2 in the zeroth place
03System.out.println("set(): "
04+Ints.join(", ", integerArray2));
05//set(): 2, 4, 6, 7
06System.out.println("set(): "
07+newIntegerList); // set(): [2, 4, 6, 7]
08 
09 
10 
11// Now lets change something from the array.
12// See the change in the list
13integerArray2[0] = 99;
14System.out.println("set [0]: "
15+Ints.join(", ", integerArray2)); //set [0]: 99, 4, 6, 7
16System.out.println("set [0]: "
17+newIntegerList); //set [0]: [99, 4, 6, 7]



There are few byte-related method that I chose to skip. If you're interested in them please check the javadoc. This is all for Ints class.

The methods I showed were identical for most of this package's classes (Booleans, Chars, Doubles, Floats, Longs, Shorts, Bytes). Now I'll try to explain the methods that are interesting and not covered.

When working with bytes you can be indifferent to their sign (Bytes) or choose to use signed bytes (SignedBytes) or unsigned bytes (UnsignedBytes). All these classes contain methods which are already covered. Only UnsignedBytes is worth mentioning.

Each negative byte is mapped to a positive one in this class. A negative byte b is treated as 256 + b. toInt() is the only method that's not previously covered.

1int int1 = UnsignedBytes.toInt((byte)-125);
2// 256 - 125 = 131
3 
4System.out.println(int1 == 131); // true



Primitives class is a helper class for working on primitive or wrapper classes. allPrimitiveTypes() returns the Set of primitive types' classes. allWrapperTypes() returns the Set of wrapper types. isWrapperType() checks if the given class is a wrapper type. unwrap() returns the given wrapper class' primitive while wrap() does the inverse.

01Set < Class > >  allPrimitiveTypes =
02Primitives.allPrimitiveTypes();
03System.out.println(
04"All primitive types: "+allPrimitiveTypes);
05// All primitive types:
06// [byte, boolean, void, int, long, short, double, char, float]
07 
08 
09Set < Class > > allWrapperTypes = Primitives.allWrapperTypes();
10System.out.println("All wrapper types: "+allWrapperTypes);
11// All wrapper types: [class java.lang.Byte, class java.lang.Short,
12// class java.lang.Float, class java.lang.Character, class java.lang.Long,
13// class java.lang.Integer, class java.lang.Boolean, class java.lang.Void, class java.lang.Double]
14 
15 
16System.out.println(Primitives.isWrapperType(int.class));
17// false because int is not a wrapper type
18System.out.println(Primitives.isWrapperType(Integer.class));
19// true because Integer is a wrapper type
20System.out.println(Primitives.isWrapperType(Object.class));
21// false because Object is not a wrapper type
22 
23// As I have a set of wrapper types
24// I can check if a class is
25//  a wrapper type or a primitive type.
26 
27System.out.println("is wrapper? "+Primitives.allWrapperTypes().contains(Integer.class));
28// true
29System.out.println("is primitive? "+Primitives.allPrimitiveTypes().contains(int.class));
30// true
31 
32//both below are true because if I unwrap an Integer
33// I obtain an int and if I wrap an int I obtain
34// an Integer
35System.out.println(
36Primitives.unwrap(Integer.class).equals(int.class));
37System.out.println(
38Primitives.wrap(int.class).equals(Integer.class));


This is all for the Primitives package. Although I think that some of the methods are redundant there are quite useful ones that'll make your life much easier.

Friday, October 22, 2010

Friday, October 15, 2010

soap basics

Nicholas Quaine has a great SOAP (Simple Object Access Protocol) basics tutorial here. As I was not a pro in web services, it was quite helpful for me for getting the basics.

Wednesday, September 1, 2010

Could not calculate build plan: Missing: maven-resources-plugin:maven-plugin:2.4.1

While I was trying to take Quartz using Maven I got "Could not calculate build plan: Missing: maven-resources-plugin:maven-plugin:2.4.1" error. What now? Everything was working just fine!
To fix this, find where your local repo is, then go to "/.m2/repository/org/apache/maven/plugins/maven-resources-plugin/", remove "2.4.1" directory and do "update dependencies" from Maven.

Friday, August 20, 2010

eclipse keymap on intellij idea

As you probably know I'm a fan of Eclipse. In our current project, we are using IntelliJ Idea as ide. Obviously I want to use Eclipse shortcuts on IntelliJ Idea but how?
Open File > Settings > Keymap and change the keymap to Eclipse. You can set Netbeans shortcuts from there too.

Wednesday, August 18, 2010

a warm welcome

I had a warm welcome from Tomcat while I was trying to learn JSF. It's a JasperException and its message is "#{...} is not allowed in template text". I was quite confident with my code so I was surprised. Later I saw that I missed two lines from the header of my JSP page.


<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

Thursday, August 12, 2010

google's guava library tutorial part 4: taste the functional programming flavor in java with predicates

In this blog entry, I'll try to explain another functional programming feature introduced by Google Guava library: Predicates. They work like the Function class with the difference that they just return a boolean value. It's possible to use Predicates for filtering elements in an Iterable that satisfy the given Predicate or to check if all/any element in an Iterable satisfies the given Predicate.

Lets write a Predicate that accepts Person objects with names that began with a vowel letter. But first, let me show you what's Person object.

01class Person implements Comparable < Person > {
02 public Person(String name, String lastName){
03  this.name = name;
04  this.lastName = lastName;
05 }
06 
07 public Person(){
08 }
09 public String getName() {
10  return name;
11 }
12 public void setName(String name) {
13  this.name = name;
14 }
15 public String getLastName() {
16  return lastName;
17 }
18 public void setLastName(String lastName) {
19  this.lastName = lastName;
20 }
21 private String name;
22 private String lastName;
23 @Override
24 public int compareTo(Person arg0) {
25  return this.getName().compareTo(arg0.getName());
26 }
27 
28 @Override
29 public String toString() {
30  return this.name+" "+this.lastName;
31 }
32 
33}


Now lets see what a Predicate looks like;

01Predicate < Person > allowNamesWithVowels = new Predicate < Person > (){
02 String[] vowels = { "a", "e", "i", "o", "u", "y" };
03 @Override
04 public boolean apply(Person person) {
05  String firstName = person.getName().toLowerCase();
06  // convert to lowercase just in case
07 
08  // for each vowel check if
09  // first name begins with that
10  // vowel
11  for(String vowel : vowels){
12   if(firstName.startsWith(vowel))
13    return true;
14  }
15 
16  return false;
17 }
18};


Now lets build a Person set and begin working using it.

01Person frodo = new Person("Frodo", "Baggins");
02Person sezin = new Person("Sezin", "Karli");
03Person luke = new Person("Luke", "Skywalker");
04Person anakin = new Person("Anakin", "Skywalker");
05Person eric = new Person("Eric", "Draven");
06 
07HashSet<person> setOfPerson =
08Sets.newHashSet( eric, frodo, sezin, luke, anakin );
09System.out.println("set of person: "+setOfPerson);
10// [Sezin Karli, Luke Skywalker, Anakin Skywalker, Frodo Baggins]
11 
12</person>


filter() will use our Predicate and create a view from elements that satisfy the
Predicate

1Set < Person > view = Sets.filter(setOfPerson, allowNamesWithVowels);
2System.out.println("view: "+view);
3// filter: [Eric Draven, Anakin Skywalker]
4// we remove eric from the view
5view.remove(eric);
6System.out.println("new view: "+view);
7//new view: [Anakin Skywalker]
8System.out.println("set of person: "+setOfPerson);
9// [set of person: [Sezin Karli, Luke Skywalker, Anakin Skywalker, Frodo Baggins]


As you can see the removal of an item from the view causes the same removal from the origin set. This removal action is bidirectional. e.g. removal from the origin set will remove the corresponding element from the view.

Lets see what happens when I add to the original list.

1Person bilbo = new Person("Bilbo", "Baggins");
2Person alex = new Person("Alex", "Delarge");
3setOfPerson.add(bilbo);
4setOfPerson.add(alex);
5System.out.println("new view: "+view);
6// new view: [Anakin Skywalker, Alex Delarge]


The view is updated as well and the elements that are accepted by the predicate are added to the view. In the case above only alex is accepted. you can try add a new element to the view which does not met the predicate's requirement.

1Person jarjar = new Person("Jarjar", "Binks");
2//view.add(jarjar);


The commented line above will throw java.lang.IllegalArgumentException because the view does not accept elements that does not met the predicate's requirement.

The filter() method can be called from Iterables for all Iterable objects. All live-view tricks we saw earlier are still valid. But what I'll do for working on Maps? It's possible to filter Map objects using keys or values or both (entries) as input. First, I will build a game characters to appearance map.

1Map < String, Integer > characterAppearancesMap = Maps.newHashMap();
2characterAppearancesMap.put("Mario", 15);
3characterAppearancesMap.put("Snake", 8);
4characterAppearancesMap.put("Kratos", 4);


Now lets create the necessary Predicates. I'd like to have game characters with 'o' in their name and with lots of game appearances (more than 10 to be exact).


01Predicate < String >  allowNamesWithO = new Predicate < String > (){
02 @Override
03 public boolean apply(String name) {
04  String lowerCaseName = name.toLowerCase();
05 
06  return lowerCaseName.contains("o");
07 }
08};
09 
10Predicate < Integer > allowLotsOfAppearances = new Predicate < Integer > (){
11 @Override
12 public boolean apply(Integer appearance) {
13  int numberOfAppearance = appearance.intValue();
14 
15  return (numberOfAppearance > 10);
16 }
17};


First we will filter using the names (keys) and then we will filter using the number of appearances (values).

01Map < String, Integer >  filterKeys =
02new HashMap  < String, Integer  > ( Maps.filterKeys(characterAppearancesMap, allowNamesWithO) );
03// I copy the live view
04System.out.println("Game Characters with 'o' in their name: "+filterKeys);
05// {Mario=15, Kratos=4}
06 
07Map < String, Integer >  filterValues = Maps.filterValues(filterKeys, allowLotsOfAppearances);
08System.out.println(
09"Game Characters with 'o' in their name and with more than 10 appearances: "+filterValues);
10//{Mario=15}


You can obtain the desired map by writing a single Predicate that will contain both predicates and use this Predicate on entries of a Map. It's not possible for Predicates.and() because we have to apply the Predicate on both keys and values but new Predicate built using and() can only be applied on keys or values. Lets create a new Predicate for our case.

01Predicate < Map.Entry < String, Integer > > characterPredicate =
02 new Predicate < Map.Entry < String, Integer  >  > (){
03 
04 @Override
05 public boolean apply(Entry<string, integer=""> entry) {
06  String key = entry.getKey();
07  int value = entry.getValue().intValue();
08 
09  return (key.contains("o") && value  > 10);
10 }
11 
12};
13 
14Map < String, Integer  > filterEntries =
15 Maps.filterEntries(characterAppearancesMap, characterPredicate);
16System.out.println("Are the game character results same? "+filterEntries.equals(filterValues));
17// As you can see the result is the same
18</string,>



Functions class has a static method that'll convert your Predicate to a Function. The said Function will use your Predicate and return the result as a Boolean.

01Function < Person, Boolean > vowelVerifier = Functions.forPredicate(allowNamesWithVowels);
02ArrayList < Person >  people = Lists.newArrayList(anakin, bilbo, alex);
03List<boolean> transform2 = Lists.transform(people, vowelVerifier);
04// Vowel verifier will return TRUE (FALSE) for each
05// person with TRUE (FALSE) Predicate result
06System.out.println("Result: "+transform2);
07//[true, false, true]
08// anakin and alex are true because the Predicate
09// was checking to see if the first name of the Person
10// begins with a vowel
11 
12</boolean>


CharMatcher is basically a Predicate who accepts Character (instead of objects). Due to this fact you can easily convert your Predicate to a CharMatcher. Lets assume that we have a Predicate that allows vowels only.

01Predicate < Character > allowVowel = new Predicate < Character > (){
02 char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y' };
03 @Override
04 public boolean apply(Character character) {
05  char lowerCase = Character.toLowerCase(character.charValue());
06 
07  for(char chr : vowels){
08   if(chr == lowerCase)
09    return true;
10  }
11 
12  return false;
13 }
14};


With forPredicate() we can build a CharMatcher from a Predicate. Lets use our new CharMatcher for counting vowels in a String.


1CharMatcher vowelMatcher = CharMatcher.forPredicate(allowVowel);
2int vowelCount = vowelMatcher.countIn("starkiller");
3System.out.println("Vowel Count in 'starkiller': "+vowelCount);


Predicates class contains several useful methods for creating Predicates. If you plan using Predicates you should take a look. Lets see its methods.

Assume that we don't want people with long last names. Our character limit can be 8 for instance. Lets write a Predicate for the purpose.

01Predicate < Person >  allowShortLastNames =
02 new Predicate < Person > (){
03 @Override
04 public boolean apply(Person person) {
05  String lastName = person.getLastName();
06 
07  return (lastName.length() < 8);
08 }
09};
10 
11System.out.println(setOfPerson);
12// [Alex Delarge, Frodo Baggins, Anakin Skywalker,
13//  Luke Skywalker, Bilbo Baggins, Sezin Karli]
14Set<person> onlyShortLastNames =
15Sets.filter(setOfPerson, allowShortLastNames);
16System.out.println(
17"People with short last names: "+onlyShortLastNames);
18//[Sezin Karli, Alex Delarge, Bilbo Baggins, Frodo Baggins]
19 
20</person>


Because Skywalker is more than 7 characters it's filtered out. All other last names are in line with the predicate but what if I want a more complicated Predicate? For instance one which will accept people with first name that begins with a vowel and with short last name. I can combine 2 Predicates in this case.

1Predicate < Person >  combinedPredicates =
2Predicates.and(allowShortLastNames, allowNamesWithVowels );
3Set < Person >  filter = Sets.filter(setOfPerson, combinedPredicates);
4System.out.println("Combined Predicates: "+filter);
5// Alex Delarge


We know that longer last names will return false and we will be left with shorter last names. e.g. [Bilbo Baggins, Alex Delarge, Sezin Karli, Frodo Baggins].Then we will apply the vowel Predicate which will give [Alex Delarge] because Alex is the only first name that begins with a vowel. All Predicates that are combined will be applied for each element in the Iterable (Set, here). If one of the Predicates are false then the overall result is false else it will return true.

There are three and() methods. We show the one that'll work with 2 Predicates. There's a version that'll work with more than 2 Predicates (var-arg as argument). The last one takes Predicates from an Iterable that contains them (Iterable ). We previously used the boolean and() operator. It's also possible to combine Predicates with or() operator. or() will return true even one of the results is true if none is true then it'll return false. Assume that I want either short first names or short last names. I have a Predicate for last names. Lets add a new one for first names.


01Predicate < Person > allowShortFirstNames = new Predicate < Person > (){
02 @Override
03 public boolean apply(Person person) {
04  String firstName = person.getName();
05 
06  return (firstName.length()  <  8);
07 }
08};
09 
10Predicate < Person >  predicatesCombinedWithOr =
11Predicates.or(allowShortLastNames, allowShortFirstNames);
12filter = Sets.filter(setOfPerson, predicatesCombinedWithOr);
13System.out.println(setOfPerson);
14System.out.println("Combined with or:"+filter);
15 
16// As you can see nothing is filtered because
17// setOfPerson elements have either short
18// first names or short last names
19// Lets add a person with a long first name
20// and last name
21 
22setOfPerson.add(new Person("Abdullah", "Jabbarian"));
23filter = Sets.filter(setOfPerson, predicatesCombinedWithOr);
24System.out.println("Combined with or 2: "+filter);
25// [Alex Delarge, Frodo Baggins, Anakin Skywalker,
26// Luke Skywalker, Bilbo Baggins, Sezin Karli]
27 
28// As you can see Abdullah who has a long first and last name
29// is not in the new set.


It's possible to negate the Predicate in hand using not() method. Lets negate the combined predicate we created using and(). Normally we are accepting people with first name that begins with a vowel and with short last name. If we negate this the Predicate will accept people with first name that begin with a consonant and with a long last name.

01Predicate<person> negatedPredicates =
02 Predicates.not(combinedPredicates);
03 
04//Alex delarge was the only person with the predicate in hand.
05//When we negate the predicate
06//we will get a predicate that will return
07//every element but alex delarge
08 
09filter = Sets.filter(setOfPerson, negatedPredicates);
10System.out.println("Negated and combined Predicates: "+filter);
11// [Frodo Baggins, Anakin Skywalker, Abdullah Jabbarian,
12// Luke Skywalker, Bilbo Baggins, Sezin Karli]
13 
14</person>


Anakin is the only one with a vowel in the first name but as his last name is long he's allowed by the Predicate.

You can create two interesting Predicates using isNull() and notNull(). isNull() (notNull()) returns a Predicate that returns true if the object in hand is null (not null).

01setOfPerson.add(null);
02System.out.println(setOfPerson);
03//[null, Alex Delarge, Frodo Baggins, Anakin Skywalker,
04// Abdullah Jabbarian, Luke Skywalker, Bilbo Baggins, Sezin Karli]
05 
06Predicate < Person >  notNullPredicate = Predicates.notNull();
07// this predicate will return true if and
08// only if the Person in hand is not null
09Set < Person >  filter2 = Sets.filter(setOfPerson, notNullPredicate);
10System.out.println(filter2);
11 
12//[Alex Delarge, Frodo Baggins, Anakin Skywalker,
13//Abdullah Jabbarian, Luke Skywalker, Bilbo Baggins, Sezin Karli]


As you can see the null value is not present in the new view Set.

It's possible to use Predicates' contains() or containsPattern() methods for building a Predicate that'll accept only CharSequences that contain the given pattern. contains() takes a Pattern object while containsPattern() takes a regular expression as a String.

01// the Predicate below only accepts words with a number inside
02Predicate < CharSequence >  containsPattern =
03Predicates.containsPattern("[0-9]");
04Set<string> sw = Sets.newHashSet("r2-d2", "luke", "c3po", "darth");
05Set<string> filter3 = Sets.filter(sw, containsPattern);
06System.out.println("New set of sw characters "+filter3);
07 
08// contains and containsPattern are similar
09Predicate < CharSequence >  containsPattern2 =
10Predicates.contains( Pattern.compile("[0-9]") );
11Set < String >  filter4 = Sets.filter(sw, containsPattern2);
12System.out.println("Do they have the same result? "+filter4.equals(filter3));
13</string></string>


Another useful method of Predicates is in(). It allows elements that are inside a given Collection and rejects all others.

01ArrayList < String >  fruitList =
02 Lists.newArrayList("apple", "melon", "strawberry");
03Predicate < String >  fruitPredicate = Predicates.in(fruitList);
04List < String >  fruitBasket =
05Lists.newArrayList("apple", "mango", "melon", "banana", "apple", "melon", "strawberry", "melon", "strawberry");
06Iterable < String >  filter5 = Iterables.filter(fruitBasket, fruitPredicate);
07// As Lists does not have a filter method I used from Collections2
08 
09System.out.println("Filtered Fruit Basket: "+filter5);
10// Filtered Fruit Basket: [apple, melon, apple,
11// melon, strawberry, melon, strawberry]
12// only 3 types of fruits are left as you can see
13 
14// If you dont need a live view you can easily use retainAll()
15// for both List and Set which has a similar effect.
16fruitBasket.retainAll(fruitList);
17System.out.println("Filtered Fruit Basket: "+fruitBasket);


Predicates' instanceOf() checks if the object in hand is an instance of the given Class.

01Predicate < Object >  listPredicate = Predicates.instanceOf(List.class);
02List < Collection <  collectionsList =
03new ArrayList < Collection > ();
04collectionsList.add(new HashSet());
05collectionsList.add(new ArrayList());
06collectionsList.add(new LinkedList());
07collectionsList.add(new TreeSet());
08collectionsList.add(new LinkedHashSet());
09 
10Collection < Collection >  filter6 =
11Collections2.filter(collectionsList, listPredicate);
12System.out.println("Are only Lists filtered? "
13+ (filter6.size() == 2) );
14// only two elements are allowed


Only instances of List are allowed so the result contains only the ArrayList and LinkedList.

Another interesting method of Predicates is compose(). compose() method allows you to process all your elements using the given Function and apply a Predicate on the result. You can do that in 2 steps but when you can do that in a single step do you need it? I have a set of objects of type Circle. I'd like to filter Circles with their area less than a value. It's possible do the calculation of the area in the Predicate or we can put the method in a helper class (e.g. Circles.getArea()). Another possibility is to write a Function for this operation.

01Function < Circle, Double >  calculateArea =
02new Function < Circle, Double > () {
03 
04 @Override
05 public Double apply(Circle circle) {
06  double radius = circle.getRadius();
07  double area = Math.PI * radius * radius;
08 
09  return area;
10 }
11};
12 
13Predicate < Double >  allowCirclesWithLargeArea =
14new Predicate < Double > () {
15 double value = 10d;
16 @Override
17 public boolean apply(Double area) {
18  return area > value;
19 }
20};
21 
22Predicate < Circle > allowCircles =
23Predicates.compose(allowCirclesWithLargeArea, calculateArea);
24 
25HashSet < Circle > circleSet =
26Sets.newHashSet( new Circle(3.5, 4.5, 0.3),
27 new Circle(2.5, 1.5, 0.03), new Circle(3.4, 4.6, 4.3),
28 new Circle(3.5, 6.5, 5.3), new Circle(0.5, 4.5, 12.3));
29 
30Set < Circle > filter7 =
31Sets.filter(circleSet, allowCircles);
32System.out.println(
33"Allow circle with area less than 10: "+filter7);
34// output: Allow circle with area less than 10:
35//[{x=0.5, y=4.5, radius=12.3},
36// {x=3.5, y=6.5, radius=5.3},
37//{x=3.4, y=4.6, radius=4.3}]


Each circle is first processed by calculateArea Function and their areas are obtained. Then allowCirclesWithLargeArea Predicate can be used for filtering the areas in hand.

Iterables class contains several methods for working using Predicates. all() and any() are quite straightforward as methods. all (any) returns true if all (any) elements in the given Iterable satisfy the given Predicate. I'm going to create Band class for storing two attributes of a Band (its name and country of origin). Then create a country Predicate so we can see what all() and any() do.

01class Band{
02 String name, country;
03 public Band(String name, String country) {
04  this.name = name;
05  this.country = country;
06 }
07 public String getName() {
08  return name;
09 }
10 
11 public void setName(String name) {
12  this.name = name;
13 }
14 
15 public String getCountry() {
16  return country;
17 }
18 
19 public void setCountry(String country) {
20  this.country = country;
21 }
22  
23 @Override
24 public String toString() {
25  return "{name= "+name+", country= "+country+"}";
26 }
27}


Now lets create the bands.

01Band stratovarius = new Band("Stratovarius", "Finland");
02Band amorphis = new Band("Amorphis", "Finland");
03 
04Set < Band > metalBands =
05Sets.newHashSet(stratovarius, amorphis);
06 
07Predicate < Band > finlandPredicate =
08new Predicate < Band > () {
09 @Override
10 public boolean apply(Band band) {
11  boolean result =
12band.getCountry().equals("Finland");
13// is band country Finland?
14   
15  return result;
16 }
17};
18 
19 
20// Easy to predict what I'll do right?
21 //I have two bands from Finland and a Finland Predicate.
22 //So all() should return true to me because every band
23 //is from Finland.
24 
25 
26boolean all =
27Iterables.all(metalBands, finlandPredicate);
28System.out.println(
29"every band is from Finland? "+all); // TRUE
30 
31// Lets add Mezarkabul to the band set
32// With this addition not every element
33// satisfies the Predicate
34 
35Band mezarkabul = new Band("Mezarkabul", "Turkey");
36metalBands.add(mezarkabul) ;
37 
38all = Iterables.all(metalBands, finlandPredicate);
39System.out.println(
40"every band is from Finland? "+all); // FALSE
41 
42 
43// Still any() should return true because
44// at least one element is still satisfying the
45// Predicate.
46 
47 
48boolean any = Iterables.any(metalBands, finlandPredicate);
49System.out.println(
50"at least one band is from Finland? "+any);



Lets remove all bands form Finland and see any()s behavior. There are more than one way to do that. The most straightforward way is to use removeIf() method of Iterables class which removes elements that satisfy the given Predicate from the given Iterable.

1Iterables.removeIf(metalBands, finlandPredicate);
2// you can see that none of the bands are from Finland as of now
3System.out.println(metalBands);
4// output: [{name= Mezarkabul, country= Turkey}]
5 
6//lets see any()'s behavior now
7 
8any = Iterables.any(metalBands, finlandPredicate);
9System.out.println("at least one band is from Finland? "+any);


Iterables' filter() method can be used for filtering any Iterable using Predicates. It's similar to Collections2's filter method but usable for a larger number of Classes.

Iterables class has two more interesting methods. indexOf() which returns the index of the first element that satisfies the given Predicate. If it cant find a matching element it returns -1. The other method is find() which returns the first element which satisfies the given Predicate. If it cant find a matching element it throws NoSuchElementException. To sum up find() returns the object, indexOf() returns its index in the Iterable. As find() throws an exception in the case it couldnt find a matching element it would be wise to check first with indexOf() and then either use the index to obtain the object or to use find().


01metalBands.add(new Band("Lordi", "Finland"));
02 
03// returns the index of the
04// first element that satisfies
05// the predicate which's
06// Lordi
07 
08if(Iterables.indexOf(metalBands, finlandPredicate) != -1){
09 // here we are sure that
10 // such a band exists
11 Band bandFromFinland =
12        Iterables.find(metalBands, finlandPredicate);
13 System.out.println("The first band from Finland: "
14                           +bandFromFinland);
15}


This is all for the Predicates. I hope I was able to give you a taste of functional programming in Java using Guava.

Monday, May 17, 2010

google's guava library tutorial part 3: taste the functional programming flavor in java

Java is not a functional programming language but thanks to Google Guava Library
you can incorporate functional programming with it. In the third part of my tutorial I'll talk about Function which's one of the two classes related to the functional programming. The other is the Predicate class.

If you are not familiar to functional programming, I can briefly say that functions are like factories. They accept objects. They work using these objects and they return new and useful objects.

Usual Function definition in Guava contains two generics. The first one is the object type the function accepts, the second one is the object type it returns. The function below will accept a Person object, find the initial of this name and last name, create a StringBuilder containing these initials and return the StringBuilder object.

01    Function < Person, StringBuilder > returnNameInitials =
02 new Function < Person, StringBuilder > () {
03 
04        @Override
05        public StringBuilder apply(Person person) {
06// Sezin Karli
07            char firstCharOfName = Character.toUpperCase(person.getName().charAt(0)); // S
08            char firstCharOfLastName = Character.toUpperCase(person.getLastName().charAt(0)); // K
09           
10            return new StringBuilder(firstCharOfName+
11"."+firstCharOfLastName+"."); // S.K.
12        }
13    };


For the sake of completeness, lets give the Person object that'll be used during the examples.

01class Person implements Comparable < Person > {
02    private String name;
03    private String lastName;
04     
05    public Person(String name, String lastName){
06        this.name = name;
07        this.lastName = lastName;
08    }
09  
10    public Person(){
11    }
12    public String getName() {
13        return name;
14    }
15    public void setName(String name) {
16        this.name = name;
17    }
18    public String getLastName() {
19        return lastName;
20    }
21    public void setLastName(String lastName) {
22        this.lastName = lastName;
23    }
24 
25    @Override
26    public int compareTo(Person arg0) {
27        return this.getName().compareTo(arg0.getName());
28    }
29   
30    @Override
31    public String toString() {
32        return this.name+" "+this.lastName;
33    }
34}


If you want, you can transform a whole list of Persons (the initial list) to a list of StringBuilders of person name initials (result list) using returnNameInitials function.

01    // process each Person in personList using
02//returnNameInitials function
03    // return the result as a view of the initial list.
04    List < Person > personList =
05Lists.newArrayList(new Person("sezin", "karli"),
06 new Person("brad", "delp"));
07    List < StringBuilder > transform3 =
08Lists.transform(personList, returnNameInitials);
09    System.out.println("Name initials: "+transform3);
10// o: Name initials: [S.K., B.D.]


Notice that any change in the initial list will result in updates in the obtained list. e.g. if you remove, change, add an element in the initial list you'll see that the corresponding element in the result list will get updated as well. The result list is basically the view of the initial list in hand. Functions work one way (from Person to StringBuilder here), so although it's said (in javadoc) that the updates are bi-directional you can't do much operation in the result (view) list. For instance, you can't add new elements. If I could add "T.P." to the result list, what name and last name will this Person have in the initial list? This information cannot be obtained from the Function we use. You can't change the element content of the result (view) list too. The reason is the same with the previous case. But as you probably guess, removing an element from the result (view) list has no negative impact. Corresponding element in the initial list will be removed as well.


01    personList.add(new Person("rick","nielsen"));
02 //added an element o: [S.K., B.D., R.N.]
03    System.out.println(transform3);
04// R.N. is added  so the addition to the initial
05// list has an impact on the result list
06    personList.get(0).setName("tom"); //changed an element content
07    System.out.println(transform3);
08// T.K. becomes M.K. so the update to the initial
09// list has an impact on the result list
10    personList.remove(0);// remove the first element.
11//o: [B.D., R.N.]
12    System.out.println(transform3);
13//T.K. is no more so the update to the initial
14//list again has an impact on the result list
15 
16    transform3.remove(0);
17    System.out.println(personList);
18 // o: [rick nielsen]. as you can see the
19// removal from the result list (view) has an impact
20//on the initial list
21    transform3.get(0).append("B.");
22 // lets try to change to element content
23    System.out.println("transform 3: "+transform3);
24 // o: transform 3: [R.N.]. you cant see the change
25//because you cant do updates on the result list
26    // if you check the initial list
27    // you'll see no change too



Collections2.transform() is similar to Lists.transform() with the difference that it can work on a larger number of classes. For instance, if you want to transform an HashSet of Person can't do that with Lists' or Sets' methods (though you can do it with Iterable.transform()). All the live view-related stuff is valid for Lists.transform().

1    Set < Person > personSet =
2Sets.newHashSet(new Person("sezin", "karli"),
3 new Person("brad", "delp"));
4    Collection < StringBuilder > transform4 =
5Collections2.transform(personSet, returnNameInitials);
6    System.out.println(transform4); // o: [S.K., B.D.]



Functions' compose() method allowsus to compose functions. if f: A->B and g: B->C then f o g: A->C. The output of the first function will be the input of the second function.

01   //Assume that we used to have a function
02   //for taking the square of a double.
03   Function < Double, Double > squareOfADouble =
04new Function < Double, Double > () {
05 
06       @Override
07       public Double apply(Double double1) {
08           return double1 * double1;
09       }
10   };

But I have a list of floats, what should I do in this case? I'd like to change the squareOfADouble function but there are previous code that uses it. What if I write a float to double function and compose both functions? First I'll convert float to double, then I'll apply square of a double function.

01    Function < Float, Double > floatToDouble =
02new Function < Float, Double > () {
03        
04        @Override
05        public Double apply(Float float1) {
06            return float1.doubleValue();
07        }
08    };
09 
10      // thanks to autoboxing
11    // float values will be  put to Float wrappers.
12    ArrayList < Float > floatList =
13 Lists.newArrayList(12.3536343f, 142.3346435633f, 1.44564564f);
14 
15 
16    // first the second function then the first
17// function must be written.
18    Function < Float, Double > floatToDoubleSquare =
19Functions.compose(squareOfADouble, floatToDouble);
20 
21 
22    List < Double > squareOfFloatsAsDoubles =
23Lists.transform(floatList, floatToDoubleSquare);
24    System.out.println(squareOfFloatsAsDoubles);
25    // o: [152.61227005628461, 20259.149887098232,
26// 2.089891460912341]


Functions class contains a number of useful methods. One of them is toStringFunction(). This predefined Function takes each object and returns their toString() result.

1    List < Person > newPersonList =
2Lists.newArrayList(new Person("sezin", "karli"),
3 new Person("bilbo", "baggins"));
4    List < String > toStringList =
5Lists.transform(newPersonList, Functions.toStringFunction());
6    System.out.println(toStringList);
7// o: [sezin karli, bilbo baggins]



There are 2 quite useful methods in Functions for working with Maps.
The first forMap() performs a lookup from the given map and returns the value corresponding to the key in hand.

01    Map < String, Long > emailToEmployeeNo = Maps.newHashMap();
02    emailToEmployeeNo.put("luke@starwars.com", 1684684684L);
03    emailToEmployeeNo.put("darth@starwars.com", 245468687687L);
04    emailToEmployeeNo.put("obiwan@starwars.com", 368684674684L);
05 
06    Function < String, Long > lookupFunction =
07Functions.forMap(emailToEmployeeNo);
08    ArrayList < String > cantinaVisitOrder =
09Lists.newArrayList("luke@starwars.com",
10 "darth@starwars.com", "obiwan@starwars.com", "luke@starwars.com", "darth@starwars.com");
11    List < Long > cantinaVisitOrderById =
12Lists.transform(cantinaVisitOrder, lookupFunction);
13    System.out.println("cantina Visit Order By Id: "
14+cantinaVisitOrderById);
15    //cantina Visit Order By Id:
16//[1684684684, 245468687687, 368684674684,
17// 1684684684, 245468687687]



First "luke@starwars.com" is checked from the map and 1684684684L is returned by the function. Beware that you have to be sure that every element in the initial list is available in the function's map as a key else you'll get IllegalArgumentException.

The other forMap() is more useful if you don't want to check the initial list's validity. If the corresponding key is not found during the lookup then the given value is returned by the function.

01    Long defaultId = 0L;
02    Function < String, Long > lookupFunctionWithDefault = Functions.forMap(emailToEmployeeNo, defaultId);
03    cantinaVisitOrder.add("greedo@starwars.com");
04    cantinaVisitOrder.add("yoda@starwars.com");
05 
06    // greedo and yoda visited the cantina
07    // but they are not in the employee_email=id
08    // map
09 
10    System.out.println("cantina visit order by emails: "
11+cantinaVisitOrder);
12    //cantina visit order by emails:
13 //[luke@starwars.com, darth@starwars.com, obiwan@starwars.com,
14// luke@starwars.com, darth@starwars.com,
15// greedo@starwars.com, yoda@starwars.com]
16 
17    List < Long > cantinaVisitOrderById2 =
18 Lists.transform(cantinaVisitOrder, lookupFunctionWithDefault);
19 
20    // in the previous forMap() example we would
21//take IllegalArgumentException
22    //for yoda and greedo. now you'll get the defaultId for them
23 
24    System.out.println("cantina Visit Order By Id 2: "
25+cantinaVisitOrderById2);
26 
27    // as you can see the last 2 employees have 0 as id
28    //cantina Visit Order By Id 2: [1684684684,
29//245468687687, 368684674684, 1684684684, 245468687687, 0, 0]



Maps class has two interesting methods that work with Functions; uniqueIndex() and transformValues(). The first one's a bit counterintuive but the second one is similar to the transform() methods we saw before.

Maps' uniqueIndex() takes two args: an iterable i and a function f. For each element e in the iterable i, function f is called and the result of f is used as the key to the value e. For the mathematically inclined, a new map of f(e)-e pairs will be built using f() and i.

In the example below, extractNickName extracts nicknames from the names (name in between '') when we call uniqueIndex with this function we will have nicknames as keys and full names as values. The result is an immutable map of nickname=fullname pairs. We will explain immutables at another part of the tutorial.

01    ArrayList < String > characterList =
02 Lists.newArrayList("Luke 'Blondie' Skywalker",
03"Darth 'DarkHelmet' Vader", "Obiwan 'Ben' Kenobi");
04 
05    Function < String, String > extractNickname =
06new Function < String, String > () {
07 
08        @Override
09        public String apply(String name) {
10//Luke 'Blondie' Skywalker
11            //as there will be no space in our nicknames
12            //we can apply the split below
13            Iterable < String > tokens = Splitter.on(" ").trimResults().split(name);
14            
15            for(String token : tokens){
16// Luke, 'Blondie', Skywalker
17                if(token.startsWith("'")) //'Blondie'
18                    return token.substring(1, token.length()-1);
19// will remove ' in both ends
20            }
21            
22            return "none";
23        }
24    };
25    ImmutableMap < String, String > nicknameToNameMap =
26 Maps.uniqueIndex(characterList, extractNickname);
27 
28    System.out.println("nicknameToNameMap: "+nicknameToNameMap);
29//nicknameToNameMap: {Blondie=Luke 'Blondie' Skywalker,
30//DarkHelmet=Darth 'DarkHelmet' Vader, Ben=Obiwan 'Ben' Kenobi}


Lets see the other useful method of Maps; transformValues()

01    //For the example I write a Function that'll
02    // extract the username part of an e-mail.
03    Function < String, String > onlyUsernamePart =
04new Function < String, String > () {
05        
06        @Override
07        public String apply(String string) { //luke@starwars.com
08            int index = string.indexOf("@");
09            return string.substring(0, index); //luke
10        }
11    };
12 
13    //lets create a star wars map with nicknames as
14//keys and emails as values
15    Map < String, String > nicknameToMailMap =
16 Maps.newHashMap();
17    nicknameToMailMap.put("luke skywalker", "luke@starwars.com");
18    nicknameToMailMap.put("darth vader", "vader@starwars.com");
19 
20 
21    //Function will process the values
22    Map < String, String > viewMap =
23Maps.transformValues(nicknameToMailMap, onlyUsernamePart);
24    System.out.println(viewMap);
25    //After the transformation only username part of the emails is left
26    //{darth vader=vader, luke skywalker=luke}
27    viewMap.remove("darth vader");
28    System.out.println(nicknameToMailMap);
29 // removal on the initial map has an impact on the view as before
30    //{luke skywalker=luke@starwars.com}
31 
32    /*
33     * As Lists.transform() the Maps.transformValues()
34     * return us a view which's backed by the initial map.
35     * All view-related stuff is similar with List' transform() method.
36     * */


While we were explaining Ordering in part 2 of this tutorial, we skipped an important method because we didn't explain Functions. It's time for onResultOf() of Ordering class. Before the ordering the chosen function is applied to the iterable and then the sorting takes places.

I'd like to sort emails based on their domain name. For the purpose I wrote a Function that'll extract the domain part of an e-mail.

01    Function < String, String > onlyDomainPart =
02new Function < String, String > () {
03 
04        @Override
05        public String apply(String string) {
06//luke@lightside.com
07            int index = string.indexOf("@");
08            return string.substring(index);
09 // lightside.com
10        }
11    };
12 
13 
14    ArrayList < String > usernames =
15 Lists.newArrayList("luke@lightside.com", "darth@darkside.com", "obiwan@lightside.com", "leia@starwars.com");
16 
17    // onResultOf() will apply the given Function first
18//and do the sort as it's desired.
19    List < String > sortedCopy8 =
20Ordering.natural().onResultOf(onlyDomainPart).
21sortedCopy(usernames);
22    System.out.println(sortedCopy8);
23    //[darth@darkside.com, luke@lightside.com,
24// obiwan@lightside.com, leia@starwars.com]
25 
26    /*As you can see the usernames list is
27 ordered based on the domain name
28     * */

Thursday, May 13, 2010

google's guava library tutorial part 2: joys of Ordering

In the first part of my tutorial , I explained anything related to Strings in Guava. In this part of my tutorial, I will explain the Ordering class which combines the power of Comparators with Collections' functionality and which adds other interesting features (such as compound comparators). This class is really useful if you need to order your Iterable, find the maximum/minimum element in your Iterable, find the index of an arbitrary element. It implements Comparator interface for backward compatibility.

I will use a basic Employee class during my examples. Employee is a mutable class with three attributes: id, name and years of service..

01class Employee implements Comparable  < Employee  > {
02 private int id;
03 private String name;
04 private int yearsOfService;
05 
06 public Employee(int id, String name, int yearsOfService){
07  this.id = id;
08  this.name = name;
09  this.yearsOfService = yearsOfService;
10 }
11 public int getId() {
12  return id;
13 }
14 public void setId(int id) {
15  this.id = id;
16 }
17 public String getName() {
18  return name;
19 }
20 public void setName(String name) {
21  this.name = name;
22 }
23 public int getYearsOfService() {
24  return yearsOfService;
25 }
26 public void setYearsOfService(int yearsOfService) {
27  this.yearsOfService = yearsOfService;
28 }
29 @Override
30 public int compareTo(Employee employee) {
31  return this.getName().compareTo(employee.getName());
32 }
33 
34 @Override
35 public String toString() {
36  String toString = "id="+id
37                +"-name="+name+"-years of service="+yearsOfService;
38 
39  return toString;
40 }
41 
42}


As I implemented Comparable in the Employee class, I have to add compareTo(). In this method I only compare the name of the employees. I'll create three employees and add them to an ArrayList.

01   Employee sezinKarli = new Employee(4, "Sezin Karli", 4);
02   Employee darthVader =  new Employee(3, "Darth Vader", 5);
03   Employee hanSolo =  new Employee(2, "Han Solo", 10);
04   List < Employee > employeeList =
05   Lists.newArrayList(sezinKarli, hanSolo, darthVader);
06   System.out.println("employee list: "+employeeList);
07//output:
08//employee list: [id=4-name=Sezin Karli-years of service=4,
09// id=2-name=Han Solo-years of service=10,
10// id=3-name=Darth Vader-years of service=5]


There are many ways to create an Ordering. You can create one using the good old Comparator, using a predefined list of objects that explicitly imposes the ordering strategy, using the toString() method of the objects in hand and using the compareTo() method of the objects. You'll need to implement Comparable in your object if you want to take the compareTo() road. Lets create two comparators. One for comparing the years of service and another for employee id.

01Comparator < Employee > yearsComparator =
02new Comparator < Employee >(){
03 @Override
04 public int compare(Employee employee1, Employee employee2) {
05  return (
06employee1.getYearsOfService() - employee2.getYearsOfService()); 
07 }
08};
09 
10Comparator < Employee > idComparator =
11new Comparator < Employee >(){
12 @Override
13 public int compare(Employee employee1, Employee employee2) {
14  return (employee1.getId() - employee2.getId()); 
15 }
16};
17 
18 
19//Create an Ordering from a Comparator
20Ordering < Employee > orderUsingYearsComparator =
21 Ordering.from(yearsComparator);
22//Sort the employee list using years comparator
23List  < Employee  > sortedCopy =
24 
25orderUsingYearsComparator.sortedCopy(employeeList);
26System.out.println(
27"sorted copy based on years of service comparator: "+sortedCopy);
28/*
29output:
30sorted copy based on years of service comparator:
31[id=4-name=Sezin Karli-years of service=4,
32 id=3-name=Darth Vader-years of service=5,
33 id=2-name=Han Solo-years of service=10]
34 
35*/


The example above is not different from calling Collections.sort() with the comparator in hand but notice that we don't sort Collections but Iterables.

With explicit(), we create an Ordering where we impose an order explicitly.
I want to order en enum type (colors) and I want red, blue, green as order. I created the enum type as instance variable.
1static enum Colors{RED, GREEN, BLUE, YELLOW};


Not lets see the Ordering part.
01Ordering  < Colors  >  explicitOrdering =
02Ordering.explicit(Colors.RED, Colors.BLUE, Colors.GREEN);
03 
04List  < Colors  > colorList =
05Lists.newArrayList(Colors.BLUE, Colors.RED,
06                   Colors.BLUE, Colors.GREEN, Colors.RED);
07 
08List  < Colors  >  sortedCopy8 =
09explicitOrdering.sortedCopy(colorList);
10System.out.println("ordered color list: "+sortedCopy8);
11//ordered color list: [RED, RED, BLUE, BLUE, GREEN]
12//we imposed the RED, BLUE, GREEN order and
13//the result conforms to that


Notice that explicit() works only with the given objects. You can't sort you iterable if you didnt specify the order in explicit(). If you try to to that you'll get IncomparableValueException.

Let me show you how to create an Ordering using toString() method of the object in hand.

01Ordering < Object > usingToString = Ordering.usingToString();
02// returns an ordering which uses the natural
03// order comparator on toString() results of objects
04List  < Employee  >  sortedCopy3 =
05usingToString.sortedCopy(employeeList);
06System.out.println("sorted usingToString: "+sortedCopy3);
07 
08/*
09sorted usingToString:
10[id=2-name=Han Solo-years of service=10,
11 id=3-name=Darth Vader-years of service=5,
12 id=4-name=Sezin Karli-years of service=4]
13*/


Lets create an Ordering with natural ordering (using compareTo() of the objects)

01Ordering  < Employee > natural = Ordering.natural();
02List  < Employee >  sortedCopy4 = natural.sortedCopy(employeeList);
03System.out.println("sorted with natural: "+sortedCopy4);
04/*
05sorted with natural:
06[id=3-name=Darth Vader-years of service=5,
07 id=2-name=Han Solo-years of service=10,
08 id=4-name=Sezin Karli-years of service=4]
09 
10*/


We can do binary search on the sorted list for an element. Notice that the Ordering that calls the binary search method and Ordering that sorted the Iterable in hand must be the same. binarySearch() was available in Collections class just like min() and max() we'll see in next examples.

1int binarySearch = natural.binarySearch(sortedCopy4, sezinKarli);
2System.out.println("My index in the list: "+binarySearch); // 2


I will add elements of employee list, 2 null elements to a new list so we can explore new methods related to the handling of null elements.

1List  < Employee > employeeListWithNulls =
2new ArrayList  < Employee  > (employeeList);
3employeeListWithNulls.add(null);
4employeeListWithNulls.add(null);



2 methods can be used for changing the handling of null elements. nullsFirst(), (nullsLast()) returns an Ordering which puts null elements to the beginning (end) of every non-null value.

01// sort the employee list with natural ordering
02// and put the null elements to the beginning
03List < Employee >  sortedCopy5 =
04natural.nullsFirst().sortedCopy(employeeListWithNulls);
05System.out.println("nulls first: "+sortedCopy5);
06/*
07nulls first:
08[null, null, id=3-name=Darth Vader-years of service=5,
09 id=2-name=Han Solo-years of service=10,
10 id=4-name=Sezin Karli-years of service=4]
11 
12*/
13 
14// sort the employee list with natural ordering
15//and put the null elements to the ending
16List < Employee > sortedCopy6 =
17natural.nullsLast().sortedCopy(employeeListWithNulls);
18System.out.println("nulls last: "+sortedCopy6);
19 
20/*
21nulls last:
22[id=3-name=Darth Vader-years of service=5,
23 id=2-name=Han Solo-years of service=10,
24 id=4-name=Sezin Karli-years of service=4, null, null]
25 
26*/


I'd like to find the employee with the highest (lowest) time of service. I can do a sort based on years of service but that'll be slower then doing a single sweep for finding the element with max (min) year of service.

01Employee employeeWithMaxYearsOfService =
02orderUsingYearsComparator.max(employeeList);
03System.out.println(employeeWithMaxYearsOfService);
04// Han solo has the biggest year of service for the company
05 
06//lets find the minimum
07Employee employeeWithMinYearsOfService =
08orderUsingYearsComparator.min(employeeList);
09System.out.println(employeeWithMinYearsOfService);
10// Sezin has the smallest year of service for the company
11 
12     
13  /*
14   * min() and max() can be used for any iterable.
15   * Notice that these methods are overloaded and
16   * it's possible to use them on two elements or
17   *  an arbitrary number of elements (var-arg)
18   * */
19 
20//Sezin or Darth has the longest year of service?
21Employee max =
22orderUsingYearsComparator.max(sezinKarli, darthVader);
23System.out.println(max); //Vader
24 
25//Lets see the var-arg version,
26// The result below must be the same
27//with employeeWithMaxYearsOfService
28//because we used each element in the
29// employee list and the same Ordering
30Employee max2 =
31orderUsingYearsComparator.max(sezinKarli, darthVader, hanSolo);
32System.out.println(
33"Are the max results the same?: "
34+max2.equals(employeeWithMaxYearsOfService)); //true


In each ordering we defined an increasing order. We can reverse the Ordering calling reverse() and obtaining a new Ordering. Lets build a new ordering that'll use the idComparator.

01Ordering < Employee >  reverseIdOrdering =
02Ordering.from(idComparator).reverse();
03 
04// We will have an ordering of decreasing employee ids if we use
05// this Ordering
06List < Employee >  employeeWithReverseIdOrder =
07 
08reverseIdOrdering.sortedCopy(employeeList);
09System.out.println(
10"employeeWithReverseIdOrder:"+ employeeWithReverseIdOrder);
11/*
12employeeWithReverseIdOrder:[
13id=4-name=Sezin Karli-years of service=4,
14id=3-name=Darth Vader-years of service=5,
15 id=2-name=Han Solo-years of service=10]
16 
17*/
18// As you can see the ids are 4, 3 and
19//2 (an array with decreasing order



A useful ability of Orderings is the fact that you can combine multiple Comparators with it. Call compound() for this. If the result of comparator1 is 0 then the second one will be called and this will continue until a non-zero value (a larger/smaller relationship) is obtained from the comparator. If all comparators return 0 then the elements are treated as equal (we get 0 from the combined comparator).

01List < Employee >  newEmployeeList =
02Lists.newArrayList(new Employee(1, "Mr Pink", 8),
03new Employee(2, "Mr Brown", 8), new Employee(3, "Mr Green", 3),
04new Employee(4, "Mr Yellow", 5));
05 
06//We previously created an Ordering
07// by id (orderUsingYearsComparator)
08//Now lets add id comparator to this Ordering
09 
10Ordering < Employee >  combinedComparatorOrdering =
11orderUsingYearsComparator.compound(idComparator);
12List < Employee >  sortedCopy7 =
13combinedComparatorOrdering.sortedCopy(newEmployeeList);
14//First by years of service then by id
15System.out.println("Combined comparators: "+sortedCopy7);
16/*Combined comparators:
17[id=3-name=Mr Green-years of service=3,
18id=4-name=Mr Yellow-years of service=5,
19id=1-name=Mr Pink-years of service=8,
20id=2-name=Mr Brown-years of service=8]
21*/


As you can see the years of service increase (3 then 5 then two 8). When years of service comparator see that the result is a draw the second comparator is called. As you can see Mr Pink and Mr Brown have the same years of service so their id are inspected and the order between them is calculated. As the id of Mr Pink is less than Mr Brown's he's before Mr Brown.

This is all for compound comparators.
There are two slightly different methods for checking if the iterable in hand is ordered. isOrdered() checks if each element is less than/equal to the subsequent element. isStrictlyOrdered() checks if each element is strictly less than the subsequent element.

01List < Integer >  integers = Lists.newArrayList(1, 2, 3, 4);
02// This should return true from isOrdered() and isStrictlyOrdered()
03// because the numbers are increasing( isOrdered() = true)
04// and there's no consecutive and equal
05// elements (isStrictlyOrdered = true)
06 
07boolean ordered = Ordering.natural().isOrdered(integers);
08System.out.println("isOrdered: "+ordered);
09boolean strictlyOrdered =
10Ordering.natural().isStrictlyOrdered(integers);
11System.out.println("isStrictlyOrdered: "+strictlyOrdered);
12 
13//Lets use another list with equal elements inside
14 
15List < Integer >  newIntegers =
16Lists.newArrayList(1, 2, 3, 3, 4);
17// The numbers are increasing( isOrdered() = true)
18// and there's  consecutive and equal
19// elements (isStrictlyOrdered = false)
20 
21ordered = Ordering.natural().isOrdered(newIntegers);
22System.out.println("isOrdered: "+ordered);
23strictlyOrdered =
24Ordering.natural().isStrictlyOrdered(newIntegers);
25System.out.println("isStrictlyOrdered: "+strictlyOrdered);


Using functions as Ordering is quite interesting, but I will explain functional programming capabilities later so lets skip it for the moment.

Tuesday, May 11, 2010

google's guava library tutorial part 1: fun with string-related stuff

I was planning to create a Guava tutorial. But it seems like it'll be too large for a single post, so I opted on splitting it into several parts. The first part contains everything related to Strings. Four main classes are explained:
  • CharMatcher (which can be considered as a light form of JDK's Pattern+Matcher with string manipulation capabilities)
  • Joiner and MapJoiner (which are useful for joining iterables or arrays into string representations)
  • Splitter (which is split() of JDK on steroids).


CharMatcher can be thought as a Pattern+Matcher of JDK in a more simple and practical form. It's not a full fledged replacement because you can't use regular expressions as you do on JDK.

01String string = "Scream 4";
02// I get a predefined CharMatcher
03//which will accept letters or digits
04CharMatcher matcher = CharMatcher.JAVA_LETTER_OR_DIGIT;
05// You can find how many times a letter
06// or a digit is seen
07// Much more practical to use a Pattern
08//and a Matcher then iterate over the
09//Matcher results for counting
10int count = matcher.countIn(string);
11System.out.println("Letter or digit count: "+count);
12// 7 characters
13 
14/*
15* matchesAllOf (matchesNoneOf) checks
16* if all (none) of the elements
17* in the given string matches with the
18* matcher in hand.
19* */
20System.out.println(matcher.matchesAllOf("scream"));
21// true
22System.out.println(matcher.matchesAllOf("scream "));
23// false because there's an empty
24//space at the end
25System.out.println(matcher.matchesNoneOf("_?=)("));
26// true because no letters or
27//digits in it


You can negate the matcher so it accepts the complementary character set. e.g. if our CharMatcher was accepting {a, b, c}, it'll accept any character except {a, b, c}.

01CharMatcher negatedMatcher = matcher.negate();
02/*
03* You can think that true, false,
04* true will become false, true, false
05* because now our matcher is a
06* non-letter, non-digit matcher.
07* But no, the result will be false,
08* false, false.
09* The interesting one is the second one.
10* The negatedMatcher matches only
11* the empty space part of "scream ".
12* So it returns "false".
13* */
14 
15System.out.println(negatedMatcher.matchesAllOf("scream"));
16//false
17System.out.println(negatedMatcher.matchesAllOf("scream "));
18//false
19System.out.println(negatedMatcher.matchesNoneOf("_?=)("));
20//false


removeFrom() and retainFrom() are really convenient methods. The first one removes the matching string while the second one extracts the matching string.


01String review = "Scream 4 is the #1 teen-slasher!";
02CharMatcher whitespaceMatcher = CharMatcher.JAVA_WHITESPACE;
03String result = whitespaceMatcher.removeFrom(review);
04// This matcher will remove the
05//matching characters (whitespaces)
06System.out.println("The sentence without whitespaces: "+result);
07//output: Scream4isthe#1teen-slasher!
08 
09/*
10* I want the numbers in the text above.
11* I can do that by first taking
12*the predefined digit CharMatcher and
13* then calling retainFrom() for
14* the string in hand.
15* */
16String result2 = CharMatcher.DIGIT.retainFrom(review);
17System.out.println("Retained digits: "+result2);
18// I'll get '41' as a result
19// because I have 4 and 1 as digits


indexIn() returns the index of the first matching character.

1//I'd like to learn the index
2// of the digit result too.
3//The first element is '4'
4 
5int indexOfDigit = CharMatcher.DIGIT.indexIn(review);
6System.out.println("index Of Digit: "+indexOfDigit);
7// 4's index is 7


Although it's possible to use CharMatcher with predefined matcher setting you can as well build your own.

01CharMatcher onlyEvenNumbersMatcher = CharMatcher.anyOf("2468");
02// This accepts any even number
03CharMatcher noEvenNumbersMatcher = CharMatcher.noneOf("2468");
04// This accepts everything
05//but even numbers
06CharMatcher largeAtoZ = CharMatcher.inRange('A', 'Z');
07CharMatcher aToZ = CharMatcher.inRange('a', 'z').or(largeAtoZ);
08// we added A-Z with 'or' here.
09// You can join CharMatchers
10// with "and" too.
11 
12System.out.println(
13"Even numbers matcher result: "
14+onlyEvenNumbersMatcher.matchesAllOf("1354"));
15// false. 1,3,5 are not ok
16 
17System.out.println(
18"Even numbers matcher result: "
19+onlyEvenNumbersMatcher.matchesAllOf("starwars"));
20// false. only even numbers are ok
21 
22System.out.println(
23"Even numbers matcher result: "
24+onlyEvenNumbersMatcher.matchesAllOf("2466"));
25// true. all of them are even
26 
27System.out.println(
28"No even numbers matcher result: "
29+noEvenNumbersMatcher.matchesAllOf("1354"));
30// false. 4 is not ok
31 
32System.out.println(
33"No even numbers matcher result: "
34+noEvenNumbersMatcher.matchesAllOf("1337"));
35// true. none of them are even
36 
37System.out.println(
38"No even numbers matcher result: "
39+noEvenNumbersMatcher.matchesAllOf("supermario"));
40// true. none of them are even
41 
42System.out.println(
43"a to Z matcher result: "+aToZ.matchesAllOf("sezin"));
44System.out.println(
45"a to Z matcher result: "+aToZ.matchesAllOf("Sezin"));
46System.out.println(
47"a to Z matcher result: "+aToZ.matchesAllOf("SeZiN"));
48System.out.println(
49"a to Z matcher result: "+aToZ.matchesAllOf("SEZIN"));
50// true. all strings are ok.
51// All of the characters are
52// in {a, .., z} and {A, .., Z} range
53 
54System.out.println(
55"a to Z matcher result: "+aToZ.matchesAllOf("scream4"));
56// false. if 4 was not here every
57// character in hand was in [a-Z]  


You can use trimFrom(), trimLeadingFrom() and trimTrailingFrom() for enhanced trimming capability. Next class is the Joiner class. You probably know splitting capabilities of JDK. It's a mystery why a string joining mechanism is not added to JDK. Guava's Joiner is here to help you in case you need one. Joiner basically takes an iterable or an array and joins all the elements inside as Strings. After that, you can directly add it to a StringBuilder, an Appendable (like PrintWriter, BufferedWriter ... etc), or obtain a String in the "element1 SEPARATOR element2...." form. We choose the separator with on() method of Joiner class. It's possible to use a CharMatcher, a Pattern or a String as separator.

01// lets build an array list with
02//4 letters content
03ArrayList<string> charList = Lists.newArrayList("a", "b", "c", "d");
04StringBuilder buffer = new StringBuilder();
05 
06// You can easily add the joined
07// element list to a StringBuilder
08buffer = Joiner.on("|").appendTo(buffer, charList);
09System.out.println(
10"Joined char list appended to buffer: "+buffer.toString());
11// Joined char list appended to buffer: a|b|c|d
12 
13//Below we join a list with ", "
14// separator for obtaining a String
15String joinedCharList = Joiner.on(", ").join(charList);
16System.out.println(
17"Joined char list as String: "+joinedCharList);
18 
19//Joined char list as String: a, b, c, d
20 
21//I'm adding a null value for
22// further exploration of Joiner features
23charList.add(null);
24System.out.println(charList);
25//  null at the end:
26//[a, b, c, d, null]
27 
28// Below the Joiner will skip
29// null valued elements automatically
30String join4 = Joiner.on(" - ").skipNulls().join(charList);
31System.out.println(join4);
32// output: a - b - c - d
33 
34// Below, the Joiner will give
35// a value to null valued elements automatically
36join4 = Joiner.on(" - ").
37useForNull("defaultValue").join(charList);
38System.out.println(join4);
39// output: a - b - c - d - defaultValue
40 
41</string>



If you have predefined String values no need to create an array or an iterable for joining them. Notice that you can join an arbitrary number of objects with the method below. The method works with var-args.

1join4 = Joiner.on("|").
2join("first", "second", "third", "fourth", "rest");
3System.out.println(join4);
4//output: first|second|third|fourth|rest


Notice that if neither skipNulls() nor useForNull(String)is used, the joining methods will throw NullPointerException if any given element is null.

Joiner is for iterables and arrays. Joiner.MapJoiner inner class is the map counterpart of Joiner. You can join the map content directly using Joiner.MapJoiner class. First you have to build a Joiner and assign it a separator(1) using on(). Then you can call withKeyValueSeparator() which takes the separator(2) between key value pairs This map joiner can be used to join a map for obtaining a string or this can be appended to an Appendable. The form of the result is "key1 SEPARATOR(1) value1 SEPARATOR(2) key2 SEPARATOR(1) value2 SEPARATOR(2)..." without the empty spaces.

01Map < String, Long > employeeToNumber = Maps.newHashMap();
02// Create a Map using static
03// method of Maps
04 
05employeeToNumber.put("obi wan", 1L);
06employeeToNumber.put("bobba", 2L);
07 
08MapJoiner mapJoiner =
09Joiner.on("|").withKeyValueSeparator("->");
10// | between each key-value pair
11//and -> between a key and its value
12String join5 = mapJoiner.join(employeeToNumber);
13System.out.println(join5);
14//output is "obi wan->1|bobba->2".


Google Guava library contains a cool Splitter class that harness more power than the JDK's split functionality.

01String text = "I have to test my string splitter,
02for this purpose I'm writing this text,  ";
03 
04//I want to split the text
05// above using ","
06// I'll have three elements
07// with the usual splitter:
08// first sentence, then the
09//second sentence and the empty space at the end.
10 
11String[] split = text.split(","); // split with ","
12System.out.println(Arrays.asList(split));
13//output: [I have to test my string splitter,  
14//for this purpose I'm writing this text,   ]

I'd want to remove the empty elements and then trim each element to remove the unnecessary empty spaces before and after them. I can do this in several steps with the old splitter. It's quite easy with Guava's Splitter.

01// Again, the on parameter is the separator.
02//You can use a CharMatcher,
03//a Pattern or a String as a separator.
04Iterable<string> split2  = Splitter.on(",").omitEmptyStrings()
05.trimResults().split(text);
06System.out.println(Lists.newArrayList(split2));
07 
08// output:
09//[I have to test my string splitter,
10// for this purpose I'm writing this text]
11 
12// I can split tokens of length 5
13//from the string in hand
14Iterable<string> split3 = Splitter.fixedLength(5).split(text);
15System.out.println(Lists.newArrayList(split3));
16// each token's length is 5
17//output:
18//[I hav, e to , test , my st, ring , split,
19// ter, ,  for , this , purpo, se I', m wri,
20// ting , this , text,,   ]
21</string></string>


Notice that trimming is applied before checking for an empty result, regardless of the order in which the trimResults() and omitEmptyStrings() methods were invoked.

Strings class contains a number of utility methods.Most of them are checking String objects'. emptyToNull() and nullToEmpty() are quite similar. emptyToNull (nullToEmpty) returns the given string if it is non-empty (non-null) else it returns an empty (null) string.

1String emptyToNull = Strings.emptyToNull("test");
2System.out.println(emptyToNull);
3// returns "test" because it's not empty
4 
5emptyToNull = Strings.emptyToNull("");
6System.out.println(emptyToNull);
7// returns null because the argument is empty


isNullOrEmpty() is quite practical. I don't remember how many times I had to write (string != null && !string.isEmpty())in my code.
01String arg = "";
02boolean nullOrEmpty = Strings.isNullOrEmpty(arg);
03// arg is empty
04System.out.println("Null or Empty?: "+nullOrEmpty);
05// true because it's empty
06 
07arg =  null;
08nullOrEmpty = Strings.isNullOrEmpty(arg); // arg is null
09System.out.println("Null or Empty?: "+nullOrEmpty);
10// true because it's null
11 
12arg = "something";
13nullOrEmpty = Strings.isNullOrEmpty(arg);
14// arg is not null or empty so the result is 'false'
15System.out.println("Null or Empty?: "+nullOrEmpty);


I'll show you repeat() which returns a string consisting of the given number of concatenated copies of the input string.

1String repeat = Strings.repeat("beetlejuice", 3);
2System.out.println(repeat);
3// output is "beetlejuicebeetlejuicebeetlejuice"


padEnd() and padStart() are quite similar. The first one adds the given char at the end of the given string as many times as the given integer value allows. The second one adds to the start.

01String padEnd = Strings.padEnd("star wars", 15, 'X');
02String padStart = Strings.padStart("star wars", 15, 'X');
03System.out.println("padEnd: "+padEnd);
04// padEnd: star warsXXXXXX
05 
06System.out.println("padStart: "+padStart);
07// padStart: XXXXXXstar wars
08 
09System.out.println(padStart.length() == 15);
10// true, because we give 15 as character limit

This is all for string-related classes of Guava.

Thursday, May 6, 2010

10 ways to suck at programming

FinalInt has a "don't do these" article for programmers. You have to check it; it's awesome.

Wednesday, May 5, 2010

use an online todo list

Using an online todo list is quite practical if you don't like keeping this kind of stuff old fashioned way. If you are a gmail fan like me, I'm sure you'll find "tasks" service of gmail useful. Tasks in gmail is a nice todo list where you can assign due dates to items and where you can keep multiple todo lists. I recommend it to anyone who emails reminders to himself. You can find its link in the left side of the main gmail page just below the "Contacts" link.

Tuesday, May 4, 2010

use maven on eclipse

Maven is a popular project management tool and it is quite practical to use it on Eclipse with the help of m2eclipse. To install m2eclipse in Eclipse:
  1. Help > Install New Software.
  2. Paste http://m2eclipse.sonatype.org/sites/m2e in "work with:" field and press enter
  3. Choose the only component listed under m2eclipse
  4. Click next and finish.
Notice that it's a good idea to install JDK so that Eclipse can work with it (instead of JRE) for a fully functional maven. I recommend Karol Zielinski's article for this.

Tuesday, April 6, 2010

are your passwords weak?

One man's blog contains a very nice article about password weaknesses and tips on building strong passwords. I recommend it to anyone with an interest on using stronger and harder to crack passwords.

Wednesday, March 17, 2010

op4j - a useful java library

op4j is a cool java library with lots of nice features. Let's hear the definition of op4j from the official website;

It is a Java library aimed at improving quality, semantics, cleanness and readability of Java code, especially auxiliary code like data conversion, structure iteration, filtering, mapping.

These are the features of the library (again, from the website):


  • Apply functions on object:
    Including more than 200 predefined functions for data conversion, string handling, calendar operation, math operations, etc...

  • Manage structures (arrays, lists, maps and sets), including:
    Easily creating them from their elements (building).
    Modifying them (adding / removing / replacing elements).
    Iterating them, applying functions on each element.
    Transforming them into other structures.

  • Execute conditional logic (execute actions depending on conditions).

  • Define functions from expressions, for later use (similar to closures).


I recommend the reading of the op4j presentation here for further information.

Wednesday, March 3, 2010

old versions of applications

Didn't you sometimes feel the need to use an older version of an application instead of the current version? If your answer is yes, then a time-consuming adventure lies before you. Or you can check two websites I'll recommend. OldVersion.com and OldApps.com are there to help you in your quest for older versions of popular applications.