BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JDK 7: What Frequently Rewritten Methods Should be Included in java.util.Objects?

JDK 7: What Frequently Rewritten Methods Should be Included in java.util.Objects?

Leia em Português

This item in japanese

In preparation for the arrival of a new class in JDK7 called java.util.Objects which will contain frequently-written utility methods, Joe Darcy of Sun has put out a request to the OpenJDK core-libs-dev group for ideas on what methods should be contained in this class. InfoQ would like to pass this request along to the wider Java community.

In his initial post, Darcy highlighted a couple of potential methods which could be useful in this class:

  • A null-safe equals(arg1, arg2) method which returns true if both arguments are null, false if only one is null, and arg1.equals(arg2) otherwise
  • A set of compareTo(arg1, arg2) methods for all primitive types (int, long, etc)

Some other suggestions came from Andrew John Hughes, including:

  • toString(arg), which would write out via reflection all of the fields inside the object to the returned output string
  • A utility implementation of the clone() method

Martin Buchholz also proposed a null-safe hashCode() implementation, which would return 0 for a null object.

Stephen Colebourne created a long list of proposed methods for this class, primarily by choosing existing methods from the Apache Commons Lang utility classes. Some of those proposed include:

  • min(comparable1, comparable2) and max(comparable1, comparable2), which would be null-safe and which would return the smallest/largest non-null object (or null if both arguments were null)
  • defaultNull(obj, defaultValue) which would return the defaultValue object if obj was null

What are your thoughts? What methods should be added to java.util.Objects?

Rate this Article

Adoption
Style

BT