• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Google Guava - Basic Utilities
 

Google Guava - Basic Utilities

on

  • 1,942 views

An introduction to google guava basic utilities

An introduction to google guava basic utilities

Statistics

Views

Total Views
1,942
Views on SlideShare
1,924
Embed Views
18

Actions

Likes
3
Downloads
0
Comments
0

2 Embeds 18

http://blog.bluesoft.com.br 16
http://www.bluesoft.com.br 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Google Guava - Basic Utilities Google Guava - Basic Utilities Presentation Transcript

    • Google Guava Basic UtilitiesAndré Faria Gomes @andrefaria
    • Basic UtilitiesUsing and Avoiding NullPrecoditionsOrderingObject MethodsThrowables
    • Maven<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>11.0</version></dependency>
    • Null
    • Careless use of null can cause a staggering variety of bugs http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=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
    • OptionalOptional<Integer> possible = Optional.of(5);possible.isPresent(); // returns truepossible.get(); // returns 5 http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
    • String HelpersObjects.firstNonNull(T, T) (colasce)emptyToNull(String)isNullOrEmpty(String)nullToEmpty(String)
    • Preconditions
    • 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
    • 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
    • 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
    • Ordering
    • 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
    • creation manipulationhttp://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
    • applicationhttp://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6
    • ObjectMethods
    • 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
    • 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
    • http://www.tfnico.com/presentations/google-guava
    • 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
    • http://www.tfnico.com/presentations/google-guava
    • http://www.tfnico.com/presentations/google-guava
    • http://www.tfnico.com/presentations/google-guava
    • Throwables
    • try { someMethodThatCouldThrowAnything();} catch (IKnowWhatToDoWithThisException e) { handle(e);} catch (Throwable t) { Throwables.propagateIfInstanceOf(t, IOException.class); Throwables.propagateIfInstanceOf(t, SQLException.class); throw Throwables.propagate(t);}
    • Obrigado @andrefaria http://blog.andrefaria.com http://blog.bluesoft.com.br