What methods should go into a java.util.Objects class in JDK 7?
kevinb at google.com
Wed Sep 9 23:25:57 UTC 2009
-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).
Please do add the compare() methods to all the rest of the wrapper types; we
would use those a lot.
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)).
On Wed, Sep 9, 2009 at 2:22 PM, Andrew John Hughes <
gnu_andrew at member.fsf.org> wrote:
> 2009/9/9 Joe Darcy <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,
> > two-argument static equals method that returned true if both arguments
> > 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
> > 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());
> for (Field f : c.getDeclaredFields())
> b.append(f.getName() + "=" + f.get());
> 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
> PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
> Fingerprint: F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8
Kevin Bourrillion @ Google
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the core-libs-dev