First round of java.util.Objects for code review (bug 6797535)

Marek Kozieł develop4lasu at gmail.com
Fri Oct 9 06:58:56 UTC 2009


2009/10/8 Joseph D. Darcy <Joe.Darcy at sun.com>:
> Hello.
>
> Please code review the first-round of java.util.Objects; the patch is below:
> http://cr.openjdk.java.net/~darcy/6797535.0/
>
> -Joe
>
> --- old/make/java/java/FILES_java.gmk   2009-10-08 11:04:03.000000000 -0700
> +++ new/make/java/java/FILES_java.gmk   2009-10-08 11:04:02.000000000 -0700
> @@ -258,6 +258,7 @@
>    java/util/ServiceConfigurationError.java \
>    java/util/Timer.java \
>    java/util/TimerTask.java \
> +    java/util/Objects.java \
>    java/util/UUID.java \
>    java/util/concurrent/AbstractExecutorService.java \
>    java/util/concurrent/ArrayBlockingQueue.java \
> --- /dev/null   2009-07-06 20:02:10.000000000 -0700
> +++ new/src/share/classes/java/util/Objects.java        2009-10-08
> 11:04:03.000000000 -0700
> @@ -0,0 +1,102 @@
> +/*
> + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
> + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
> + *
> + * This code is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 only, as
> + * published by the Free Software Foundation.  Sun designates this
> + * particular file as subject to the "Classpath" exception as provided
> + * by Sun in the LICENSE file that accompanied this code.
> + *
> + * This code is distributed in the hope that it will be useful, but WITHOUT
> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
> + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> + * version 2 for more details (a copy is included in the LICENSE file that
> + * accompanied this code).
> + *
> + * You should have received a copy of the GNU General Public License
> version
> + * 2 along with this work; if not, write to the Free Software Foundation,
> + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
> + * CA 95054 USA or visit www.sun.com if you need additional information or
> + * have any questions.
> + */
> +
> +package java.util;
> +
> +/**
> + * This class consists of {@code static} utility methods for operating
> + * on objects.  These utilities include {@code null}-safe or {@code
> + * null}-tolerant methods for computing the hash code of an object,
> + * returning a string for an object, and comparing two objects.
> + *
> + * @since 1.7
> + */
> +public class Objects {
> +    private Objects() {
> +        throw new AssertionError("No java.util.Objects instances for
> you!");
> +    }
> +
> +    /**
> +     * Returns {@code true} if the arguments are equal to each other
> +     * and {@code false} otherwise.
> +     * Consequently, if both arguments are {@code null}, {@code true}
> +     * is returned and if exactly one argument is {@code null}, {@code
> +     * false} is returned.  Otherwise, equality is determined by using
> +     * the {@link Object#equals equals} method of the first
> +     * argument.
> +     *
> +     * @return {@code true} if the arguments are equal to each other
> +     * and {@code false} otherwise
> +     * @see Object#equals(Object)
> +     */
> +    public static boolean equals(Object a, Object b) {
> +        return (a == b) || (a != null && a.equals(b));
> +    }

Hello,
I would suggest other implementation of equals method:

	public static boolean equals(Object a, Object b) {
		if (a == null) return (b == null ? true : b.equals(null));
		if (b == null) return a.equals(null);
		return a.equals(b);
	}

This one could cover case when null is considered as object in object
equals method, while in not time implementation result might depend
from arguments order:

	class Boo {
		public String	data;

		@Override
		public boolean equals(Object obj) {
			if (obj == null) return (data == null);//
			...
		}
	}


-- 
Regards.
Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the core-libs-dev mailing list