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

Rémi Forax forax at univ-mlv.fr
Mon Nov 16 00:47:27 UTC 2009


Le 16/11/2009 00:48, Martin Buchholz a écrit :
>
>
> On Sat, Nov 14, 2009 at 20:46, David Holmes - Sun Microsystems 
> <David.Holmes at sun.com <mailto:David.Holmes at sun.com>> wrote:
>
>     Paul,
>
>     Paul Benedict said the following on 11/15/09 11:28:
>
>         I would like to propose adding this method:
>
>         /**
>          * Selects the object if not {@code null}; otherwise fallsback
>         to the
>          * specified default object.
>          *
>          * @param object the object to test
>          * @param defaultObject the default object
>          * @return the object if not {@code null}; otherwise the
>         default object
>          */
>         public static Object defaultIfNull(Object object, Object
>         defaultObject) {
>            return (object != null) ? object : defaultObject;
>         }
>
>
>     I don't get it. Why would anyone need the above when they can write:
>
>        obj != null ? obj : otherObj
>
>
> The obvious reason is to save creation of a temp variable
>
> Some<Really<Long<Java<Type<Here>>>>> tmp = longExpression;
> return tmp != null ? tmp : defaultValue;
>
> But I'm not in favor of this proposal either.
> The biggest problem is that you want
> defaultObject to be an expression
> that only gets evalutated if tmp == null.
> And that suggests adding some kind of syntactic abstraction
> that I would like to use myself, but is not
> in the spirit of Java.
>
> Martin

Do you want to say something like "I need a closure here" :)

Rémi

>
>
>     themselves ??? Compared to
>
>        defaultIfNull(obj, otherObj)
>
>     you don't even save any keystrokes (and this is with a static import)!
>
>     David Holmes
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20091116/e8b31008/attachment.html>


More information about the core-libs-dev mailing list