ããã ããGuavaã£ã¦ã©ã¤ãã©ãªãè¯ãã£ã¦ã¤ã³ã¿ã¼ãããã«æ¸ãã¦ãã®ã§èª¿ã¹ã¦ã¿ããã ãµã³ãã«ã½ã¼ã¹ã®ã»ã¨ãã©ã¯æ¬å®¶ããã³ãããããã
guava-libraries - Guava: Google Core Libraries for Java 1.6+ - Google Project Hosting
nullã½ãé¿ããããã®ä»çµã¿
{% highlight java %}
Optional
å¼æ°ã®å¤ãã§ãã¯ãªã©ãåææ¡ä»¶ããã§ãã¯ããä»çµã¿ãassertã¨ãã«ä¼¼ã¦ãã¨æãã æ¡ä»¶ãfalseã ã¨å¯¾å¿ããExceptionãã¹ãã¼ãããã
Comparatorãã¹ãã¼ãã«ãããã¤ãComparatorã¯ããã©ãããã»ã»ã»ã
{% highlight java %}
Ordering
if (Ordering.from(comparator).reverse().isOrdered(list)) { ... }
{% endhighlight %} ããç´ æµã
ãªã«ãã便å©ã¡ã½ããã
nullã½ãæ°ã«ããnullãã§ãã¯ãã§ãããã
{% highlight java %} Objects.equal("a", "a"); // returns true Objects.equal(null, "a"); // returns false Objects.equal("a", null); // returns false Objects.equal(null, null); // returns true
å®å ¨ãªããã·ã¥ãç°¡åã«ä½ãã¾ãã {% highlight java %} Objects.hashCode(Object...)
toStringã¡ã½ããã®æ§ç¯ã»ä¿å®ã楽ã«ãªããã {% highlight java %} @override public String toString(){ // Returns "ClassName{x=1}" return Objects.toStringHelper(this) .add("x", this.x)//å¤æ°åã¨ãã®å¤ .toString(); //or // Returns "MyObject{x=1}" return Objects.toStringHelper("MyObject") .add("x", 1) .toString(); } {% endhighlight %} eclipseã®ã¸ã§ãã¬ã¼ã¿ã¼ã§ãããã¨æããã©ã
ComparisonChainã使ãã¨ãé£ç¶ããæ¯è¼ãç°¡åã«ã {% highlight java %} 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(); }
caseæã®ããã«ãé£éããtry catchæãæ¸ããããã«ãªãã¾ãã ä¾ãã°ã以ä¸ã®å¦ç㧠IKnowWhatToDoWithThisException ãçºçããå ´åã1 2 3 ã®é çªã§å¦çãçºçãã¾ãã {% highlight java %} try { // 1 someMethodThatCouldThrowAnything(); } catch (IKnowWhatToDoWithThisException e) { // 2 handle(e); } catch (Throwable t) { // 3 Throwables.propagateIfInstanceOf(t, IOException.class); Throwables.propagateIfInstanceOf(t, SQLException.class); throw Throwables.propagate(t); } {% endhighlight %} java7ã ã¨ç°¡æ½ã«æ¸ãããã©ãAndroidã¨ãã¯ã¾ã 使ããªãããããããããã
ä¸å¤ã®ã³ã¬ã¯ã·ã§ã³ãä½ãããæã«æ±ããé²å¾¡çã³ãã¼(defensive copy)ãããæã¨ãã«ä¾¿å©ã
{% highlight java %}
public static final ImmutableSet
class Foo {
Set<Bar> bars;
Foo(Set<Bar> bars) {
this.bars = ImmutableSet.copyOf(bars); // defensive copy!
}
}
{% endhighlight %}
ã³ã¬ã¯ã·ã§ã³åã®è¿½å
ä¾ãã°ãè¦ç´ ã®ä¸ã«ç¹å®ã®åèªãããã¤å«ã¾ããããæ¤åºã§ããã
{% highlight java %}
Multiset
1ã¤ã®keyã«å¯¾ãã¦ãvalueãããããaddã§ãã¾ãã
{% highlight java %}
Set
BindingMapã®ç¥ããªï¼Mapã«ä¿æããå¤ã¨åãåºãã¦å©ç¨ãã¦ããå¤ã§æ´åæ§ãä¿ã¡ãããããä»çµã¿ã mapã®keyã«å¤ãç´ä»ãã¦ããã®ã«putããå ´åãIllegalArgumentExceptionãçºçããã åé¤ããæã¯forcePutã¡ã½ãããå©ç¨ããã â»ããåãã£ã¦ãªã
è¡ã¨åã®æ§é ãæã£ãã³ã¬ã¯ã·ã§ã³ãï¼æ¬¡å é åã¨ãéããè¡ãããåãããã¢ã¯ã»ã¹ã§ããã
ã¯ã©ã¹ã®åããã¼ã«ãããå¤ãã£ãMapã§ããï¼ä½¿ãéãæãæµ®ãã°ãªãï¼
ç¯å²ã®ã³ã¬ã¯ã·ã§ã³ã
java6å¿
é ãGWTã¯é対å¿ã
Collectionæä½ã®ã¦ã¼ãã£ãªãã£
{% highlight java %}
List
{% highlight java %}
List
é
åã®åæåã¿ããã«èªç±ã«è¿½å ã§ããã
{% highlight java %}
Set
guavaã¯CollectionããIterableãªã®ã好ããããã
{% highlight java %}
Iterable
String lastAdded = Iterables.getLast(myLinkedHashSet);
String theElement = Iterables.getOnlyElement(thisSetIsDefinitelyASingleton); // if this set isn't a singleton, something is wrong!
ãã®ä»ãCollectionã®ä¾¿å©ã¯ã©ã¹ãè²ã ã
ãã¶ã¤ã³ãã¿ã¼ã³ã® decorator pattern ãç°¡åã«å®è£ ã§ããä»çµã¿ã 以ä¸ã®ãµã³ãã«ã 㨠addã«logã®å¦çãdecorateãã¦ããã
{% highlight java %}
class AddLoggingList
Iteratorã§æ¬¡ã®è¦ç´ ãè¦ãããã©indexãé²ããããªãæã«ä½¿ãpeekã¡ã½ãããæ¡å¼µããã¦ããã
{% highlight java%}
List
ç¬èªã®ã¤ãã¬ã¼ã¿ã¼ã楽ã«å®è£
ããã
{% highlight java%}
public static Iterator
ãããªããµã³ãã«ã§ããã
{% highlight java %}
LoadingCache