001package votorola.g.lang; // Copyright 2003-2005, 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003
004/** Object utilities.
005  */
006public final class ObjectX
007{
008
009    private ObjectX() {}
010
011
012
013    /** An equator that uses the {@linkplain Object#equals(Object) #equals}() method of
014      * the first object, if it is not null.  Its thread safety is that of
015      * <code>o1.equals(object)</code>.
016      */
017    public static final Equator<Object> EQUATOR = new Equator<Object>()
018    {
019        // changing?  change also in g/web/gwt/super/
020        public boolean equals( final Object o1, final Object o2 )
021        {
022            if( o1 == null ) return o2 == null;
023
024            return o1.equals( o2 );
025        }
026    };
027
028
029
030 // /** Returns null if o is null; o.clone() otherwise.
031 //   */
032 // public static Object nullClone( Object o )
033 // {
034 //     if( o == null ) return o;
035 //     else return o.clone();
036 // }
037 //////// sorry, clone() is protected
038
039
040
041    /** Returns true iff the objects are either 'equal', or both null.  This does the same
042      * thing as {@linkplain #EQUATOR EQUATOR} and will eventually be deprecated.  The
043      * thread safety is that of <code>o1.equals(object)</code>.
044      */
045    public static boolean nullEquals( final Object o1, final Object o2 )
046    {
047        // changing?  change also in g/web/gwt/super/
048        if( o1 == null ) return o2 == null;
049
050        return o1.equals( o2 );
051    }
052
053
054
055 // /** Returns o.toString() if o is non-null; null otherwise.
056 //   */
057 // public static String nullToString( Object o )
058 // {
059 //     if( o == null ) return null;
060 //
061 //     return o.toString();
062 // }
063 //////// this sort of thing would have to be done everywhere
064
065
066
067    /** Same as object.wait(), but returns normally if interrupted.
068      *
069      *     @return true if the wait ended normally; false if an InterruptedException occured
070      */
071    public static @ThreadSafe boolean tryWait( final Object object )
072    {
073        try
074        {
075            object.wait();
076            return true;
077        }
078        catch( InterruptedException x ) { return false; }
079    }
080
081
082
083    /** Same as object.wait(milliseconds), but returns normally if interrupted.
084      *
085      *     @return true if the wait ended normally; false if an InterruptedException occured
086      */
087    public static @ThreadSafe boolean tryWait( final Object object, final long timeout )
088    {
089        try
090        {
091            object.wait( timeout );
092            return true;
093        }
094        catch( InterruptedException x ) { return false; }
095    }
096
097
098
099}