Google Guava
Upcoming SlideShare
Loading in...5
×
 

Google Guava

on

  • 4,847 views

 

Statistics

Views

Total Views
4,847
Views on SlideShare
1,660
Embed Views
3,187

Actions

Likes
5
Downloads
67
Comments
0

9 Embeds 3,187

http://jeeconf.com 3109
http://www.scoop.it 56
http://tedwon.com 13
https://twimg0-a.akamaihd.net 2
http://webcache.googleusercontent.com 2
http://plus.url.google.com 2
http://a0.twimg.com 1
https://si0.twimg.com 1
http://www.party09.com 1
More...

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Google Guava Google Guava Presentation Transcript

  • Google
  •   Guava Alex
  •   Korotkikh @alexkorotkikh
  • Cleaner
  •   code
  • Objects:
  •   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;}
  • Objects:
  •   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);}
  • Preconditions:
  •   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;}
  • Preconditions:
  •   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;}
  • Preconditions:
  •   example
  •   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;}
  • Preconditions:
  •   example
  •   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");}
  • Preconditions:
  •   list•checkNotNull
  •   (NullPointerException)•checkArgument
  •   (IllegalArgumentException)•checkState
  •   (IllegalStateException)•checkElementIndex
  •   (IndexOutOfBoundsException)•etc
  • Collections:
  •   creationBefore:List<SomeJavaObject> list = new ArrayList<SomeJavaObject>();list.add(firstObj);list.add(secondObj);
  • Collections:
  •   creationBefore:List<SomeJavaObject> list = new ArrayList<SomeJavaObject>();list.add(firstObj);list.add(secondObj);After:List<SomeJavaObject> list = newArrayList(firstObj, secondObj);
  • New
  •   collections
  • Collections:
  •   BiMapBiMap<String, String> biMap = HashBiMap.create();biMap.put("google", "guava");biMap.put("apache", "commons");assertEquals("guava", biMap.get("google"));assertEquals("google", biMap.inverse().get("guava"));
  • Collections:
  •   MultimapBefore: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);
  • Collections:
  •   MultimapBefore: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);After:Multimap<String, String> map = HashMultimap.create();map.put(key, value);assertTrue(map.get(key) instanceof Collection);
  • Collections:
  •   immutablesBefore:List<String> strings = newArrayList(“a”, “b”, “c”);List<String> unmodifiableStrings = Collections.unmodifiableList(strings);strings.add(“d”);assertEquals(4, unmodifiableStrings());
  • Collections:
  •   immutablesBefore:List<String> strings = newArrayList(“a”, “b”, “c”);List<String> unmodifiableStrings = Collections.unmodifiableList(strings);strings.add(“d”);assertEquals(4, unmodifiableStrings());After:@Test(expected = UnsupportedOperationException.class)public void immutableList_addItem_throwException() { List<Integer> ints = ImmutableList.of(1, 2, 3); ints.add(4)}
  • Helper
  •   utils
  • Utils:
  •   SplitterBefore:Collection<String> strings = newArrayList();for (String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); }}
  • Utils:
  •   SplitterBefore:Collection<String> strings = newArrayList();for (String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); }}After:Iterable<String> strings = Splitter.on(",") .omitEmptyStrings() .trimResults() .split("abc,def,,gh, ijk ");
  • Utils:
  •   SplitterBefore:Collection<String> strings = newArrayList();for (String s : "abc,def,,gh, ijk ".split(",")) { if (!s.isEmpty()) { strings.add(s.trim()); }}After:Splitter splitter = Splitter.on(",") .omitEmptyStrings() .trimResults();Iterable<String> strings = splitter.split("abc,def,,gh, ijk ");Iterable<String> strings2 = splitter.split("lmno,, pqrstu,vwxyz ");
  • Utils:
  •   JoinerBefore: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(", "); }}
  • Utils:
  •   JoinerBefore: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(", "); }}After:String s = Joiner.on(", ") .skipNulls() .join ("Atomicity", "Consistency", "Isolation", null, "Durability");
  • Utils:
  •   RangesBefore:Set<Integer> acceptableMarks = newHashSet();for (int i = 60; i <= 100; i++) { acceptableMarks.add(i);}boolean examPassed = acceptableMarks.contains(mark);
  • Utils:
  •   RangesBefore:Set<Integer> acceptableMarks = newHashSet();for (int i = 60; i <= 100; i++) { acceptableMarks.add(i);}boolean examPassed = acceptableMarks.contains(mark);After:Range<Integer> acceptableMarks = Ranges.closed(60, 100); // [60, 100]boolean examPassed = acceptableMarks.contains(mark);
  • Just
  •   use
  •   it<dependency>    <groupId>com.google.guava</groupId>    <artifactId>guava</artifactId>    <version>12.0</version></dependency>
  • Tnx!Alex
  •   Korotkikh@alexkorotkikh