// primarily by name, secondarily by value; null-safe; case-insensitive public int compareTo(final Metadata other) { int result = nullSafeStringComparator(this.name, other.name); if (result != 0) { return result; } return nullSafeStringComparator(this.value, other.value); } public static int nullSafeStringComparator(final String one, final String two) { if (one == null ^ two == null) { return (one == null) ? -1 : 1; } if (one == null && two == null) { return 0; } return one.compareToIgnoreCase(two); }
A java blog with a collection of examples and tutorials on Java and related technologies. (Under maintenance with continuous updates) Be in touch with java jazzle or k2java.blogspot.com.
Tuesday, February 1, 2011
Implementing null-safe compareTo
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment