com.sun.sgs.impl.sharedutil
Class Objects

java.lang.Object
  extended by com.sun.sgs.impl.sharedutil.Objects

public final class Objects
extends Object

Utility methods for working with Objects.


Method Summary
static void checkNull(String variableName, Object value)
          Checks that a variable is not null, throwing NullPointerException if it is.
static String fastToString(Object object)
          Returns a string representing the object without calling any methods on the object.
static String safeToString(Object object)
          Returns a string representing the object, returning a failsafe value if the toString or hashCode methods throw exceptions or if the method ends up being called recursively.
static
<T> T
uncheckedCast(Object object)
          Casts an object to the required type with no unchecked warnings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

safeToString

public static String safeToString(Object object)
Returns a string representing the object, returning a failsafe value if the toString or hashCode methods throw exceptions or if the method ends up being called recursively.

Parameters:
object - the object or null
Returns:
a string representing the object

fastToString

public static String fastToString(Object object)
Returns a string representing the object without calling any methods on the object. Callers can use this method to avoid the accesses that toString might perform to any ManagedReferences that the object contains, which could lead to exceptions or scaling problems.

Returns "null" if the argument is null, otherwise returns the concatenation of the fully qualified name of the object's class, the '#' character, and the identity hash code of the object as returned by System.identityHashCode, represented in base 16.

Parameters:
object - the object or null
Returns:
a string representing the object

uncheckedCast

public static <T> T uncheckedCast(Object object)
Casts an object to the required type with no unchecked warnings.

This method is similar to using the @SuppressWarnings("unchecked") annotation, but is more flexible and often more succinct. It is more flexible because it can be used to convert the type of an object passed as a method argument, while the annotation needs to be applied to a declaration. It is more succinct because it avoids needing to restate the type.

For example, compare:

 @SuppressWarnings("unchecked")
 Set<Foo> setOfFoo = (Set<Foo>) object;
 return foo(setOfFoo);
 
with:
 return foo(uncheckedCast(object));
 
Note that this method cannot be used when the return type is a type variable.

Type Parameters:
T - the result type
Parameters:
object - the object to cast
Returns:
the object cast to type T

checkNull

public static void checkNull(String variableName,
                             Object value)
Checks that a variable is not null, throwing NullPointerException if it is. The exception message includes the name of the variable.

Parameters:
variableName - the name of the variable being checked
value - the value of the variable to check

Project Darkstar, Version 0.9.10.5
2013-07-29 21:14:44

Copyright © 2007-2013 Sun Microsystems, Inc. All rights reserved