Guava

592 views

Published on

Published in: Internet, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
592
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Guava

  1. 1. Guava By Franck Benault Created 01/07/2014 Last updated 02/08/2014
  2. 2. Guava plan ● Goal of Guava ● Limitations ● Reasons to use Guava ? ● Example
  3. 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. 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. 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. 6. Goal of Guava : first example ● Splitter.on(',') – .trimResults() – .omitEmptyStrings() – .split(" foo, ,bar, quux,"); ● => ["foo", "bar", "quux"]
  7. 7. Goal of Guava : first application ● List<String> list = Lists.newArrayList(null, "two", null, "four") ; ● Joiner.on(":") – .userForNull("") – .join(list); ● => ":two::four"
  8. 8. Limitation : Security fix breaks  Guava ● 22 janvier 2014, security fix in Java update51 breaks Guava library ● Sun.reflect.generics.reflectiveObjects.TypeVariableImpl issue on the Equals() method
  9. 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 ?
  10. 10. Limitation : new functionality in  Java language ● Guava is maybe more interesting for old version of Java (java7) than in last version Java8 ● Example integration of Optional in Java8
  11. 11. Reasons to use Guava ● Collection Initializers and Utilities ● Limited Functional-Style Programming ● Multimap and Bimaps ● Easy Hashcodes and Comparators ● Defensive Coding
  12. 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
  13. 13. Example : simple in-memory cache ● Quiz 2 – How to create a simple cache ?
  14. 14. Example : simple in-memory cache ● Two types of cache – Loading cache (the interresting one) – Cache ● Simple in-memory cache – Thread safe – Simple eviction strategy
  15. 15. Example : simple in-memory cache public CacheLoader<String,String> loader = new CacheLoader<String,String>() { public String load(String key) { return key.toUpperCase(); } }; public LoadingCache<String, String> cache = CacheBuilder .newBuilder() .build(loader);
  16. 16. Example : simple in-memory cache public LoadingCache<String, String> cache CacheBuilder.newBuilder() .maximumSize(size) .recordStats() .build(loader); for(int i=0 ; i<1000; i++) { myCache.cache.getUnchecked("simple test"+random10()); } CacheStats stats = myCache.cache.stats(); stats.hitCount(); stats.missCount();
  17. 17. Example : Equals hash ... methods ● Class Person – Attributs firstname, lastname, birthday... – Methods : ● constructor, ● Equals/ hashCode / toString ● CompareTo
  18. 18. Example : Equals hash ... methods ● Class Person – Attributs firstname, lastname, birthday... – Methods : ● constructor, ● Equals/ hashCode / toString ● CompareTo
  19. 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; }
  20. 20. Example : Equals hash ... methods ● Class PersonWithGuava public int hasCode() { return Objects.hashCode(birthday, firstname, lastname); }
  21. 21. Example : Equals hash ... methods Test with EclEmma comparison with and without Guava PersonSimple PersonWithGuava Constructor 58 instructions 42  equals 69 41 hashCode 45 19 toString 50 16 compareTo 28 18 Total instructions 238 136 Code coverage 84 % 90 %
  22. 22. Example : performance mesure ● Quiz 3 – How mesure the elapsed time ● Example one method/ a part of a method
  23. 23. Example : performance mesure ● Prefer Stopwatch – over System.nanoTime – and definitively over currentTimeMillis() ● Other solutions Jamon, java8 date API
  24. 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; }
  25. 25. Conclusion ● When will you start with Guava ?

×