Guava

Páginas: 7 (1705 palabras) Publicado: 27 de octubre de 2012
Guava
Google's Core Libraries for Java
Kevin Bourrillion, Google Inc. as presented at Netflix 2010-04-26

Overview
Guava: Google's core Java libraries for Java 5+. This presentation: broad overview, partial highlight reel, and lots of questions? Presenter (me): At Google >5 years Lead engineer, Java core libraries >3 years

Overview
Guava: Google's core Java libraries for Java 5+. Thispresentation: broad overview, partial highlight reel, and lots of questions? Presenter (me): At Google >5 years Lead engineer, Java core libraries >3 years Devoted Netflix subscriber >9 years! your company changed my life I OWE YOU GUYS

Overview (of library)
http://guava-libraries.googlecode.com

Apache 2 license (very permissive). Frequent releases ("r03" a few weeks ago, "r04" this week).Under com.google.common:
base, collect, io, net*, primitives, util.concurrent

Er, what about the "Google Collections Library?"
(most of collect, some of base)

We want you to use Guava!
"I could just write that myself." But... These things are much easier to mess up than it seems With a library, other people will make your code faster for you When you use a popular library, your code isin the mainstream When you find an improvement to your private library, how many people did you help? Well argued in Effective Java 2e, Item 47.

1. com.google.common.base
"The corest of the core." "java.langy" stuff.

The Objects class
public class Person { final String name, nickname; final Movie favMovie; @Override public boolean equals(Object object) { if (object instanceof Person) {Person that = (Person) object; return Objects.equal(this.name, that.name) && Objects.equal(this.nickname, that.nickname) && Objects.equal(this.favMovie, that.favMovie); } return false; } @Override public int hashCode() { return Objects.hashCode(name, nickname, favMovie); }

Objects example cont.
public class Person { final String name, nickname; final Movie favMovie; // ... @Override publicString toString() { return Objects.toStringHelper(this) .add("name", name) .add("nickname", nickname) .add("favMovie", favMovie) .toString(); } public String preferredName() { return Objects.firstNonNull(nickname, name); } }

Preconditions
Our class com.google.common.base.Preconditions supports defensive coding. You can choose either
if (state != State.PLAYABLE) { throw newIllegalStateException( "Can't play movie; state is " + state); }

. . . or . . .
Preconditions.checkState(state == State.PLAYABLE, "Can't play movie; state is %s", state);

(what's the difference? none!)

Preconditions (2)
Or compare . . .
public void setRating(StarRating rating) { if (rating == null) { throw new NullPointerException(); } this.rating = rating; }

. . . with (using static import) . . .public void setRating(StarRating rating) { this.rating = checkNotNull(rating); }

CharMatcher
We once had a StringUtil class. It grew large:
allAscii, collapse, collapseControlChars, collapseWhitespace, indexOfChars, lastIndexNotOf, numSharedChars, removeChars, removeCrLf, replaceChars, retainAllChars, strip, stripAndCollapse, stripNonDigits, ...

These represent a partial cross product of twonotions: (a) what's a "matching" character? (b) what to do with those matching characters? This approach could not scale, so we created CharMatcher. An instance of this type represents part (a), and the operation you invoke on it represents part (b).

Getting a CharMatcher
Use a predefined constant (examples)
CharMatcher.WHITESPACE (tracks Unicode defn.) CharMatcher.JAVA_DIGITCharMatcher.ASCII CharMatcher.ANY

Getting a CharMatcher
Use a predefined constant (examples)
CharMatcher.WHITESPACE (tracks Unicode defn.) CharMatcher.JAVA_DIGIT CharMatcher.ASCII CharMatcher.ANY

Use a factory method (examples)
CharMatcher.is('x') CharMatcher.isNot('_') CharMatcher.oneOf("aeiou").negate() CharMatcher.inRange('a', 'z').or(inRange('A', 'Z'))

Getting a CharMatcher
Use a predefined...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Juan diaz y guava.

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS