Saturday, July 3, 2010

Guava Object helpers

Say hello to the Spring utils.

import com.google.common.base.Objects;

// Null-safe equals
Objects.equal("foo", null);

// Choose first not-null or throw NPE
Objects.firstNonNull("foo", null);

// Helper for Arrays.hashCode()
Objects.hashCode(1, "foo", null);

// ToStringHelper which generates - String{foo, bar, baz=qux}
String toStringTarget = "foo";
Objects.toStringHelper(toStringTarget).
   addValue(toStringTarget).addValue("bar").add("baz", "qux");

No comments:

Post a Comment