What methods should go into a java.util.Objects class in JDK 7?
Andrew John Hughes
gnu_andrew at member.fsf.org
Wed Sep 9 21:22:04 UTC 2009
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, 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
More information about the core-libs-dev
mailing list