Basic Utilities
1.Optional
Optional<T> is a way of replacing a nullable T reference with a non-null value. An Optional may either contain a non-null T reference (in which case we say the reference is "present"), or it may contain nothing (in which case we say the reference is "absent"). It is never said to "contain null."
Optional<Integer> possible = Optional.of(5);
possible.isPresent(); // returns true
possible.get(); // returns 5
Making an Optional
| Method | Description |
|---|---|
| Optional.of(T) | Make an Optional containing the given non-null value, or fail fast on null. |
| Optional.absent() | Return an absent Optional of some type. |
| Optional.fromNullable(T) | Turn the given possibly-null reference into an Optional, treating non-null as present and null as absent. |
Query methods
| Method | Description |
|---|---|
| boolean isPresent() | Returns true if this Optional contains a non-null instance. |
| T get() | Returns the contained T instance, which must be present; otherwise, throws an IllegalStateException. |
| T or(T) | Returns the present value in this Optional, or if there is none, returns the specified default. |
| T orNull() | Returns the present value in this Optional, or if there is none, returns null. The inverse operation of fromNullable. |
| Set |
Returns an immutable singleton Set containing the instance in this Optional, if there is one, or otherwise an empty immutable set. |
2.Preconditions
| Signature (not including extra args) | Description | Exception thrown on failure |
|---|---|---|
| checkArgument(boolean) | Checks that the boolean is true. Use for validating arguments to methods. | IllegalArgumentException |
| checkNotNull(T) | Checks that the value is not null. Returns the value directly, so you can use checkNotNull(value) inline. | NullPointerException |
| checkState(boolean) | Checks some state of the object, not dependent on the method arguments. For example, an Iterator might use this to check that next has been called before any call to remove. | IllegalStateException |
3.Ordering
Ordering<String> byLengthOrdering = new Ordering<String>() {
public int compare(String left, String right) {
return Ints.compare(left.length(), right.length());
}
};
Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
public String apply(Foo foo) {
return foo.sortedBy;
}
});
| Method | Description |
|---|---|
| reverse() | Returns the reverse ordering. |
| nullsFirst() | Returns an Ordering that orders nulls before non-null elements, and otherwise behaves the same as the original Ordering. See also nullsLast() |
| compound(Comparator) | Returns an Ordering which uses the specified Comparator to "break ties." |
| lexicographical() | Returns an Ordering that orders iterables lexicographically by their elements. |
| onResultOf(Function) | Returns an Ordering which orders values by applying the function to them and then comparing the results using the original Ordering. |
4.Object methods
equals
Objects.equal("a", "a"); // returns true
Objects.equal(null, "a"); // returns false
Objects.equal("a", null); // returns false
Objects.equal(null, null); // returns true
hashCode
Objects.hashCode(field1, field2, ..., fieldn)
**in JDK7**
Objects.hash(Object...)
toString
MoreObjects.toStringHelper()
compare/compareTo
ComparisonChain performs a "lazy" comparison: it only performs comparisons until it finds a nonzero result, after which it ignores further input.
public int compareTo(Foo that) {
return ComparisonChain.start()
.compare(this.aString, that.aString)
.compare(this.anInt, that.anInt)
.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
.result();
}
5.Throwables
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
Throwables.propagateIfInstanceOf(t, SQLException.class);
throw Throwables.propagate(t);
}