2. About Me
Owner / Engineer @ Axemblr
Apache Whirr PMC Member
jclouds committer
Worked at Facebook & Adobe
Connect with me on LinkedIn
3. @ Axemblr
Building a tool and a platform for managing
Apache Hadoop clusters on cloud infrastructure
Think Amazon EMR but for your cloud.
Do you want to join? hello@axemblr.com
4. Plan
● Introduction
● Basic utilities
● Collections
● Strings
● Primitives
● Next & Questions
5. Introduction
Open-source version of Google's core Java
libraries
[...] carefully designed, tested, optimized and
used in production at Google
You don't need to write them, test them, or
optimize them: you can just use them.
6. Introduction (cont)
Battle-tested in production at Google
Staggering numbers of unit tests. >110.000 as
of January 2012 (generated)
Lives near the bottom of the stack, right on top
of the JDK itself
7. Plan
● Introduction
● Basic utilities
1.Using and avoiding null
2.Preconditions
3.Common object utilities
4.Ordering
5.
8. Using and avoiding null
● careless use of null can cause bugs
● most collections should not contain null
values (95% @ Google)
● null is ambiguous (e.g. Map.get)
Avoid null by using:
● "null object" pattern (e.g. empty collection)
● null Map entries stored as a Set of null keys
● Guava Optional<T>
9. Optional<T>
As a replacement for null when used to indicate
some sort of absence
Optional<T> may either contain a non-null
reference to a T instance or nothing.
Optional<Integer> possible = Optional.of(5);
possible.isPresent(); // returns true
possible.get(); // returns 5
10. Making an Optional
Optional.of(T) - make optional of given non-null
value or fail fast on null
Optional.absent() - return an absent optional of
some type
Optional.fromNullable(T) - turn value in
Optional and treat null as absent
11. Query methods
boolean isPresent() - true if non-null instance
T get() - instance or IllegalStateException
T or(T) - present value or specified default
T orNull() - inverse fromNullable
Set<T> asSet() - set with single value or empty
12. Convenience methods
Objects.firstNonNull(T, T) or
NullPointerException if both are null
Strings.emptyToNull(String)
Strings.isNullOrEmpty(String)
Strings.nullToEmpty(String)
" these methods are primarily for interfacing
with unpleasant APIs that equate null strings
and empty strings "
13. What's the point?
" It forces you to actively think about the absent
case if you want your program to compile at all,
since you have to actively unwrap the Optional
and address that case. "
https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained
14. Plan
● Introduction
● Basic utilities
1.Using and avoiding null
2.Preconditions
3.Common object utilities
4.Ordering
5.
15. Preconditions
● useful for validation
● recommended to be used as static imports
Each method has three variants:
● no extra arguments
● an extra object. Exception is obj.toString()
● an extra String & Objects. String.format
like but only allows %s (GWT compat)
26. ComparisonChain
Helper when implementing Comparator or the
Comparable interface.
return ComparisonChain.start()
.compare(this.aString, that.aString)
.compare(this.anInt, that.anInt)
.compare(this.anEnum, that.anEnum,
Ordering.natural().nullsLast())
.result();
Has a lazy behaviour.
27. Plan
● Introduction
● Basic utilities
1.Using and avoiding null
2.Preconditions
3.Common object utilities
4.Ordering
5.
31. Ordering: Manipulation
" A given Ordering can be modified to obtain
many other useful derived orderings "
Common: reverse(), nullsFirst(), nullsLast(),
lexicographical(), onResultOf(Function)
32. Ordering: Application
or how to apply to values and collections.
Common: greatestOf(it, k), leastOf(...),
isOrdered(it), isStrictlyOrdered(it), sortedCopy
(it), min(E... or iterable), max(E... or iterable)
33. Plan
● Introduction
● Basic utilities
● Collections
Immutable Collections
New types
Collection Utilities
Extension Utilities
35. Why immutable?
● safe for use by untrusted libraries
● thread-safe
● more efficient, time & space (analysis)
● can be used as a constant
36. How?
Immutable** can be created in several ways:
● copyOf(T) e.g. ImmutableSet.copyOf(set)
● of(elements) e.g. ImmutableMap.of("a", "b")
● using a Builder
All collections support asList (const. time view)
37.
38. Plan
● Introduction
● Basic utilities
● Collections
Immutable Collections
New types
Collection Utilities
Extension Utilities
40. Multiset Operations
count(E), add(E, int), remove(E, int), setCount
(E, int), size()
elementSet() - distinct elements as set
entrySet() - similar to Map.entrySet() returns
Set<Multiset.Entry<E>>, supports getElement()
and getCount()
45. BiMap
BiMap<K, V> is Map<K,V> with unique values
Operations: all Map, inverse(), values() as Set
Throws an IllegalArgumentException if you
attempt to map a key to an already-present
value
51. Plan
● Introduction
● Basic utilities
● Collections
Immutable Collections
New types
Collection Utilities
Extension Utilities
as ways of extending the collections framework
61. Charsets
"Charsets provides constant references to the
six standard Charset implementations
guaranteed to be supported by all Java
platform implementations."
bytes = string.getBytes(Charsets.UTF_8);