2. Guava plan
● Goal of Guava
● Limitations
● Reasons to use Guava ?
● Example
3. Goal of Guava
● Guava is low level core libraries coming from Google
project (Java6+)
● We should expect them in the JDK (JDK+)
● With Guava, the code looks good
– Less code
– Simplier code
– Cleaner code
– More readable code
4. Goal of Guava : quiz question
Quiz1
" foo, ,bar, quux,".split(",");
a [" foo"," ", "bar", " quux"]
b ["foo", "bar", "quux"]
c ["foo","", "bar", "quux"]
d [" foo"," ", "bar", " quux",""]
5. Goal of Guava : quiz answer
" foo, ,bar, quux,".split(",");
a [" foo"," ", "bar", " quux"]
b ["foo", "bar", "quux"] //probably what we want
c ["foo","", "bar", "quux"]
d [" foo"," ", "bar", " quux",""]
6. Goal of Guava : first example
● Splitter.on(',')
– .trimResults()
– .omitEmptyStrings()
– .split(" foo, ,bar, quux,");
● => ["foo", "bar", "quux"]
7. Goal of Guava : first application
● List<String> list = Lists.newArrayList(null, "two",
null, "four") ;
● Joiner.on(":")
– .userForNull("")
– .join(list);
● => ":two::four"
9. Limitation : annotation @Beta
● Guava is mixture of frozen API and non frozen API
(marked with @Beta)
● See Guava documentation the list of non frozen API
● One new version each three months ?
12. Reasons to use Guava : Guava vs
Commons
● Apache commons and Guava have some similar features
but ...
● Apache Commons
– Mature library (almost 10 years)
– Target java 1.4 (Commons 3.0 java 1.5+)
● Guava
– Well designed and documented
– Target java6+
– Guava is still in active development
19. Example : Equals hash ... methods
●
Class PersonSimple
public int hasCode() {
final int prime = 31;
int result =1;
result = prime * result + ((birthday==null ? 0 : birthday.hashCode()));
result = prime * result + ((firstname==null ? 0 :
firstname.hashCode()));
result = prime * result + ((lastname==null ? 0 : lastname.hashCode()));
return result;
}
24. Example : performance mesure
public static long longProcess() {
Stopwatch stopwatch = Stopwatch.createStarted();
subProcess1();
final long date2 = stopwatch.elapsed(TimeUnit.MILLISECONDS);
System.out.println("subprocess1 "+date2);
subProcess2();
final long date3 = stopwatch.elapsed(TimeUnit.MILLISECONDS);
System.out.println("subprocess1 "+(date3-date2));
return date3;
}