Google Guava

6,845 views

Published on

Published in: Technology, News & Politics
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,845
On SlideShare
0
From Embeds
0
Number of Embeds
4,609
Actions
Shares
0
Downloads
80
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Google Guava

    1. 1. Google
    2. 2.  Guava Alex
    3. 3.  Korotkikh @alexkorotkikh
    4. 4. Cleaner
    5. 5.  code
    6. 6. Objects:
    7. 7.  hashcodeBefore: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;}
    8. 8. Objects:
    9. 9.  hashcodeBefore: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;}After:public int hashCode() { return Objects.hashCode(amount, uuid, channel, type);}
    10. 10. Preconditions:
    11. 11.  exampleBefore:public Period(Date start, Date end) { if (start.after(end)) { throw new IllegralArgumentException(Start cannot be after end); } this.start = start; this.end = end;}
    12. 12. Preconditions:
    13. 13.  exampleBefore:public Period(Date start, Date end) { if (start.after(end)) { throw new IllegralArgumentException(Start cannot be after end); } this.start = start; this.end = end;}After:public Public(Date start, Date end) { checkArguments(start.after(end), Start cannot be after end); this.start = start; this.end = end;}
    14. 14. Preconditions:
    15. 15.  example
    16. 16.  IIBefore: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;}
    17. 17. Preconditions:
    18. 18.  example
    19. 19.  IIBefore: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;}After: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);}
    20. 20. Preconditions:
    21. 21.  list•checkNotNull
    22. 22.  (NullPointerException)•checkArgument
    23. 23.  (IllegalArgumentException)•checkState
    24. 24.  (IllegalStateException)•checkElementIndex
    25. 25.  (IndexOutOfBoundsException)•etc
    26. 26. Collections:
    27. 27.  creationBefore:ListSomeJavaObject list = new ArrayListSomeJavaObject();list.add(firstObj);list.add(secondObj);
    28. 28. Collections:
    29. 29.  creationBefore:ListSomeJavaObject list = new ArrayListSomeJavaObject();list.add(firstObj);list.add(secondObj);After:ListSomeJavaObject list = newArrayList(firstObj, secondObj);
    30. 30. New
    31. 31.  collections
    32. 32. Collections:
    33. 33.  BiMapBiMapString, String biMap = HashBiMap.create();biMap.put(google, guava);biMap.put(apache, commons);assertEquals(guava, biMap.get(google));assertEquals(google, biMap.inverse().get(guava));
    34. 34. Collections:
    35. 35.  MultimapBefore:MapString, ListString map = new HashMapString, ListString();if (map.get(key) == null) { map.get(key) = new ArrayListString();}map.get(key).add(value);
    36. 36. Collections:
    37. 37.  MultimapBefore:MapString, ListString map = new HashMapString, ListString();if (map.get(key) == null) { map.get(key) = new ArrayListString();}map.get(key).add(value);After:MultimapString, String map = HashMultimap.create();map.put(key, value);assertTrue(map.get(key) instanceof Collection);
    38. 38. Collections:
    39. 39.  immutablesBefore:ListString strings = newArrayList(“a”, “b”, “c”);ListString unmodifiableStrings = Collections.unmodifiableList(strings);strings.add(“d”);assertEquals(4, unmodifiableStrings());
    40. 40. Collections:
    41. 41.  immutablesBefore:ListString strings = newArrayList(“a”, “b”, “c”);ListString unmodifiableStrings = Collections.unmodifiableList(strings);strings.add(“d”);assertEquals(4, unmodifiableStrings());After:@Test(expected = UnsupportedOperationException.class)public void immutableList_addItem_throwException() { ListInteger ints = ImmutableList.of(1, 2, 3); ints.add(4)}
    42. 42. Helper
    43. 43.  utils
    44. 44. Utils:
    45. 45.  SplitterBefore:CollectionString strings = newArrayList();for (String s : abc,def,,gh, ijk .split(,)) { if (!s.isEmpty()) { strings.add(s.trim()); }}
    46. 46. Utils:
    47. 47.  SplitterBefore:CollectionString strings = newArrayList();for (String s : abc,def,,gh, ijk .split(,)) { if (!s.isEmpty()) { strings.add(s.trim()); }}After:IterableString strings = Splitter.on(,) .omitEmptyStrings() .trimResults() .split(abc,def,,gh, ijk );
    48. 48. Utils:
    49. 49.  SplitterBefore:CollectionString strings = newArrayList();for (String s : abc,def,,gh, ijk .split(,)) { if (!s.isEmpty()) { strings.add(s.trim()); }}After:Splitter splitter = Splitter.on(,) .omitEmptyStrings() .trimResults();IterableString strings = splitter.split(abc,def,,gh, ijk );IterableString strings2 = splitter.split(lmno,, pqrstu,vwxyz );
    50. 50. Utils:
    51. 51.  JoinerBefore:ListString 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(, ); }}
    52. 52. Utils:
    53. 53.  JoinerBefore:ListString 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(, ); }}After:String s = Joiner.on(, ) .skipNulls() .join (Atomicity, Consistency, Isolation, null, Durability);
    54. 54. Utils:
    55. 55.  RangesBefore:SetInteger acceptableMarks = newHashSet();for (int i = 60; i = 100; i++) { acceptableMarks.add(i);}boolean examPassed = acceptableMarks.contains(mark);
    56. 56. Utils:
    57. 57.  RangesBefore:SetInteger acceptableMarks = newHashSet();for (int i = 60; i = 100; i++) { acceptableMarks.add(i);}boolean examPassed = acceptableMarks.contains(mark);After:RangeInteger acceptableMarks = Ranges.closed(60, 100); // [60, 100]boolean examPassed = acceptableMarks.contains(mark);
    58. 58. Just
    59. 59.  use
    60. 60.  itdependency    groupIdcom.google.guava/groupId    artifactIdguava/artifactId    version12.0/version/dependency
    61. 61. Tnx!Alex
    62. 62.  Korotkikh@alexkorotkikh

    ×