Google Guava

Google Guava

5694167cd4e5ff648661ddf1c58be31b?s=128

Alexander Korotkikh

May 19, 2012
Tweet

Transcript

  1.          

    
  2. None
  3.   

  4.        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); }
  5.          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; }
  6.          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"); }
  7.          

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

    list.add(firstObj); list.add(secondObj);   List<SomeJavaObject> list = newArrayList(firstObj, secondObj);
  9.   

  10.     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"));
  11.         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);
  12.         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) }
  13.  

  14.      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 ");
  15.      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 ");
  16.      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");
  17.      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);
  18.  <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>12.0</version> </dependency>

  19.