Google Guava - Basic Utilities

1,951 views

Published on

An introduction to google guava basic utilities

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

No Downloads
Views
Total views
1,951
On SlideShare
0
From Embeds
0
Number of Embeds
40
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Google Guava - Basic Utilities

  1. 1. Google Guava Basic UtilitiesAndré Faria Gomes @andrefaria
  2. 2. Basic UtilitiesUsing and Avoiding NullPrecoditionsOrderingObject MethodsThrowables
  3. 3. Maven<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>11.0</version></dependency>
  4. 4. Null
  5. 5. Careless use of null can cause a staggering variety of bugs http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  6. 6. Null is ambiguous. Its rarely obviouswhat a null return value is supposedto mean Map.get(key) can return nulleither because the value in the map is null, or the value is not in the map http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  7. 7. OptionalOptional<Integer> possible = Optional.of(5);possible.isPresent(); // returns truepossible.get(); // returns 5 http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  8. 8. String HelpersObjects.firstNonNull(T, T) (colasce)emptyToNull(String)isNullOrEmpty(String)nullToEmpty(String)
  9. 9. Preconditions
  10. 10. import them staticallycheckArgument(i >= 0, "Argument was %s but expected nonnegative", i);checkArgument(i < j, "Expected i < j, but %s > %s", i, j); http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  11. 11. if (state != State.PLAYABLE) { throw new IllegalStateException ("Cant play movie; state is " + state);}Preconditions.checkState (state == State.PLAYABLE, "Cant play movie; state is %s", state); http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  12. 12. public void setRating(StarRating rating) { if (rating == null) { throw new NullPointerException(); } this.rating = rating;}public void setRating(StarRating rating) { this.rating = checkNotNull(rating);} http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  13. 13. Ordering
  14. 14. Ordering<String> byLengthOrdering = new Ordering<String>() {     public int compare(String left, String right) {       return Ints.compare(left.length(), right.length());     }}if (Ordering.from(comparator).reverse().isOrdered(list)){ ... } http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  15. 15. creation manipulationhttp://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  16. 16. applicationhttp://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  17. 17. ObjectMethods
  18. 18. equalsObjects.equal("a", "a"); // returns trueObjects.equal(null, "a"); // returns falseObjects.equal("a", null); // returns falseObjects.equal(null, null); // returns true http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  19. 19. ToString// Returns "ClassName{x=1}"Objects.toStringHelper(this) .add("x", 1) .toString();// Returns "MyObject{x=1}"Objects.toStringHelper("MyObject") .add("x", 1) .toString(); http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  20. 20. http://www.tfnico.com/presentations/google-guava
  21. 21. Comparator Chain public int compareTo(Person other) { int cmp = lastName.compareTo(other.lastName); if (cmp != 0) { return cmp; } cmp = firstName.compareTo(other.firstName); if (cmp != 0) { return cmp; } return Integer.compare(zipCode, other.zipCode); }public int compareTo(Foo that) { return ComparisonChain.start() .compare(this.aString, that.aString) .compare(this.anInt, that.anInt) .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast()) .result(); } http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
  22. 22. http://www.tfnico.com/presentations/google-guava
  23. 23. http://www.tfnico.com/presentations/google-guava
  24. 24. http://www.tfnico.com/presentations/google-guava
  25. 25. Throwables
  26. 26. try { someMethodThatCouldThrowAnything();} catch (IKnowWhatToDoWithThisException e) { handle(e);} catch (Throwable t) { Throwables.propagateIfInstanceOf(t, IOException.class); Throwables.propagateIfInstanceOf(t, SQLException.class); throw Throwables.propagate(t);}
  27. 27. Obrigado @andrefaria http://blog.andrefaria.com http://blog.bluesoft.com.br

×