What methods should go into a java.util.Objects class in JDK 7?

Joe Darcy Joe.Darcy at Sun.COM
Wed Sep 9 23:36:41 UTC 2009


Kevin Bourrillion wrote:
> -1 on reflection-based stuff in the Objects class.
>
> equal() and hashCode() are the only two things we saw fit to include 
> in our Objects class, and actually, our hashCode() is identical to 
> Arrays.hashCode() (but is varargs).
>
> http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Objects.html

Hmm, java.util.Arrays.{hashCode, deepHashCode} could be changed to be 
var-args methods in JDK 7.  Possibly the toString methods on Object[] 
would benefit from being var-args too.

>
> Please do add the compare() methods to all the rest of the wrapper 
> types; we would use those a lot.

I was considering putting all the compare/compareTo methods for 
primitives in one class; that way the type of the operands doesn't have 
to be expressed in a typically call site.

>
> When it comes to cloning, I tried very hard once to provide something 
> useful -- a "clone any object without knowing its concrete type" 
> method -- and it was a disaster.  A total mess of reflection, and no 
> one wanted to use it anyway.  Cloning in Java is discredited.
>
> So I'm left with only equal() for your new Objects class.  By the way, 
> the best implementation is a == b || (a != null && a.equals(b)).

Thanks for the tip,

-Joe

>
>
>
> On Wed, Sep 9, 2009 at 2:22 PM, Andrew John Hughes 
> <gnu_andrew at member.fsf.org <mailto:gnu_andrew at member.fsf.org>> wrote:
>
>     2009/9/9 Joe Darcy <Joe.Darcy at sun.com <mailto:Joe.Darcy at sun.com>>:
>     > Hello.
>     >
>     > For JDK 7, I think it is high-time the platform included a class
>     like
>     > java.util.Objects to hold commonly-written utility methods.  For
>     example, a
>     > two-argument static equals method that returned true if both
>     arguments are
>     > null, returns false is one argument is null, and otherwise
>     returns the
>     > result of calling arg1.equals(arg2)  (6797535 "Add shared two
>     argument
>     > static equals method to the platform").
>     >
>     > A static hashCode method returning 0 for null and the value of
>     > arg.hashCode() has also been suggested.
>     >
>     > A set of
>     >
>     >   static int compareTo(int, int)
>     >   static int compareTo(long, long)
>     >   ....
>     >
>     > methods probably belongs somewhere in the platform too.
>     >
>     > What other utility methods would have broad enough use and
>     applicability to
>     > go into a common java.util class?
>     >
>     > -Joe
>     >
>
>     Given you've listed utility methods for two Object methods, equals and
>     hashCode, toString seems an obvious one to handle as well:
>
>     public static String toString(Object o)
>      throws IllegalAccessException
>     {
>      Class<?> c = o.getClass();
>      StringBuilder b = new StringBuilder(c.getName());
>      b.append("[");
>      for (Field f : c.getDeclaredFields())
>      {
>        f.setAccessible(true);
>        b.append(f.getName() + "=" + f.get());
>        b.append(",");
>      }
>      b.replace(b.length() - 1, b.length(), "]");
>      return b.toString();
>     }
>
>     Maybe there's also a useful utility implementation of clone too, but I
>     can't think of one offhand.
>     --
>     Andrew :-)
>
>     Free Java Software Engineer
>     Red Hat, Inc. (http://www.redhat.com)
>
>     Support Free Java!
>     Contribute to GNU Classpath and the OpenJDK
>     http://www.gnu.org/software/classpath
>     http://openjdk.java.net
>
>     PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
>     Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8
>
>
>
>
> -- 
> Kevin Bourrillion @ Google
> internal:  http://go/javalibraries
> google-collections.googlecode.com 
> <http://google-collections.googlecode.com>
> google-guice.googlecode.com <http://google-guice.googlecode.com>
>




More information about the core-libs-dev mailing list