Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Google Guava

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Google Guava

Avatar for Alexander Korotkikh

Alexander Korotkikh

May 19, 2012
Tweet

More Decks by Alexander Korotkikh

Other Decks in Programming

Transcript

  1.        public int hashCode()

    { int result = super.hashCode(); result = 31 * result + (amount != null ? amount.hashcode() : 0); result = 31 * result + (uuid != null ? uuid.hashcode() : 0); result = 31 * result + (channel != null ? channel.hashcode() : 0); result = 31 * result + (type != null ? type.hashcode() : 0); return result; }    public int hashCode() { return Objects.hashCode(amount, uuid, channel, type); }
  2.          public

    Period(Date start, Date end) { if (start.after(end)) { throw new IllegralArgumentException("Start cannot be after end"); } this.start = start; this.end = end; }    public Public(Date start, Date end) { checkArguments(start.after(end), "Start cannot be after end"); this.start = start; this.end = end; }
  3.          public

    Period(Date start, Date end) { if (start == null || end == null) { throw new NullPointerException("Dates cannot be null"); } if (start.after(end)) { throw new IllegralArgumentException("Start can’t be after end"); } this.start = start; this.end = end; }    public Period(Date start, Date end) { this.start = checkNotNull(start, "Start cannot be null"); this.end = checkNotNull(end, "End cannot be null"); checkArgument(start.after(end), "Start cannot be after end"); }
  4.          

           
  5.      List<SomeJavaObject> list = new ArrayList<SomeJavaObject>();

    list.add(firstObj); list.add(secondObj);   List<SomeJavaObject> list = newArrayList(firstObj, secondObj);
  6.     BiMap<String, String> biMap = HashBiMap.create(); biMap.put("google",

    "guava"); biMap.put("apache", "commons"); assertEquals("guava", biMap.get("google")); assertEquals("google", biMap.inverse().get("guava"));
  7.         Map<String, List<String>>

    map = new HashMap<String, List<String>>(); if (map.get(key) == null) { map.get(key) = new ArrayList<String>(); } map.get(key).add(value);    Multimap<String, String> map = HashMultimap.create(); map.put(key, value); assertTrue(map.get(key) instanceof Collection);
  8.         List<String> strings

    = newArrayList(“a”, “b”, “c”); List<String> unmodifiableStrings = Collections.unmodifiableList(strings); strings.add(“d”); assertEquals(4, unmodifiableStrings());    @Test(expected = UnsupportedOperationException.class) public void immutableList_addItem_throwException() { List<Integer> ints = ImmutableList.of(1, 2, 3); ints.add(4) }
  9.      Collection<String> strings = newArrayList(); for

    (String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); } }   Iterable<String> strings = Splitter.on(",") .omitEmptyStrings() .trimResults() .split("abc,def,,gh, ijk ");
  10.      Collection<String> strings = newArrayList(); for

    (String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); } }   Splitter splitter = Splitter.on(",") .omitEmptyStrings() .trimResults(); Iterable<String> strings = splitter.split("abc,def,,gh, ijk "); Iterable<String> strings2 = splitter.split("lmno,, pqrstu,vwxyz ");
  11.      List<String> strings = newArrayList("Atomicity", "Consistency",

    "Isolation", null, "Durability"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < strings.size(); i++) { String s = strings.get(i); if (s == null) { continue; } sb.append(s); if (i < strings.size() - 1) { sb.append(", "); } }   String s = Joiner.on(", ") .skipNulls() .join ("Atomicity", "Consistency", "Isolation", null, "Durability");
  12.      Set<Integer> acceptableMarks = newHashSet(); for

    (int i = 60; i <= 100; i++) { acceptableMarks.add(i); } boolean examPassed = acceptableMarks.contains(mark);   Range<Integer> acceptableMarks = Ranges.closed(60, 100); // [60, 100] boolean examPassed = acceptableMarks.contains(mark);