Class Objects
- java.lang.Object
-
- com.google.common.base.Objects
-
@GwtCompatible public final class Objects extends java.lang.Object
Helper functions that can operate on anyObject
.See the Guava User Guide on writing
Object
methods withObjects
.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Objects.ToStringHelper
Deprecated.UseMoreObjects.ToStringHelper
instead.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static boolean
equal(java.lang.Object a, java.lang.Object b)
Determines whether two possibly-null objects are equal.static <T> T
firstNonNull(T first, T second)
Deprecated.UseMoreObjects.firstNonNull(T, T)
instead.static int
hashCode(java.lang.Object... objects)
Generates a hash code for multiple values.static Objects.ToStringHelper
toStringHelper(java.lang.Class<?> clazz)
Deprecated.UseMoreObjects.toStringHelper(Class)
instead.static Objects.ToStringHelper
toStringHelper(java.lang.Object self)
Deprecated.UseMoreObjects.toStringHelper(Object)
instead.static Objects.ToStringHelper
toStringHelper(java.lang.String className)
Deprecated.UseMoreObjects.toStringHelper(String)
instead.
-
-
-
Method Detail
-
equal
public static boolean equal(@Nullable java.lang.Object a, @Nullable java.lang.Object b)
Determines whether two possibly-null objects are equal. Returns:true
ifa
andb
are both null.true
ifa
andb
are both non-null and they are equal according toObject.equals(Object)
.false
in all other situations.
This assumes that any non-null objects passed to this function conform to the
equals()
contract.Note for Java 7 and later: This method should be treated as deprecated; use
Objects.equals(java.lang.Object, java.lang.Object)
instead.
-
hashCode
public static int hashCode(@Nullable java.lang.Object... objects)
Generates a hash code for multiple values. The hash code is generated by callingArrays.hashCode(Object[])
. Note that array arguments to this method, with the exception of a single Object array, do not get any special handling; their hash codes are based on identity and not contents.This is useful for implementing
Object.hashCode()
. For example, in an object that has three properties,x
,y
, andz
, one could write:public int hashCode() { return Objects.hashCode(getX(), getY(), getZ()); }
Warning: When a single object is supplied, the returned hash code does not equal the hash code of that object.
Note for Java 7 and later: This method should be treated as deprecated; use
Objects.hash(java.lang.Object...)
instead.
-
toStringHelper
@Deprecated public static Objects.ToStringHelper toStringHelper(java.lang.Object self)
Deprecated.UseMoreObjects.toStringHelper(Object)
instead. This method is scheduled for removal in Guava 21.0.Creates an instance ofObjects.ToStringHelper
.This is helpful for implementing
Object.toString()
. Specification by example:// Returns "ClassName{}" Objects.toStringHelper(this) .toString(); // Returns "ClassName{x=1}" Objects.toStringHelper(this) .add("x", 1) .toString(); // Returns "MyObject{x=1}" Objects.toStringHelper("MyObject") .add("x", 1) .toString(); // Returns "ClassName{x=1, y=foo}" Objects.toStringHelper(this) .add("x", 1) .add("y", "foo") .toString(); // Returns "ClassName{x=1}" Objects.toStringHelper(this) .omitNullValues() .add("x", 1) .add("y", null) .toString();
}Note that in GWT, class names are often obfuscated.
- Parameters:
self
- the object to generate the string for (typicallythis
), used only for its class name- Since:
- 2.0
-
toStringHelper
@Deprecated public static Objects.ToStringHelper toStringHelper(java.lang.Class<?> clazz)
Deprecated.UseMoreObjects.toStringHelper(Class)
instead. This method is scheduled for removal in Guava 21.0.Creates an instance ofObjects.ToStringHelper
in the same manner astoStringHelper(Object)
, but using the name ofclazz
instead of using an instance'sObject.getClass()
.Note that in GWT, class names are often obfuscated.
- Parameters:
clazz
- theClass
of the instance- Since:
- 7.0 (source-compatible since 2.0)
-
toStringHelper
@Deprecated public static Objects.ToStringHelper toStringHelper(java.lang.String className)
Deprecated.UseMoreObjects.toStringHelper(String)
instead. This method is scheduled for removal in Guava 21.0.Creates an instance ofObjects.ToStringHelper
in the same manner astoStringHelper(Object)
, but usingclassName
instead of using an instance'sObject.getClass()
.- Parameters:
className
- the name of the instance type- Since:
- 7.0 (source-compatible since 2.0)
-
firstNonNull
@Deprecated public static <T> T firstNonNull(@Nullable T first, @Nullable T second)
Deprecated.UseMoreObjects.firstNonNull(T, T)
instead. This method is scheduled for removal in Guava 21.0.Returns the first of two given parameters that is notnull
, if either is, or otherwise throws aNullPointerException
.Note: if
first
is represented as anOptional
, this can be accomplished with first.or(second). That approach also allows for lazy evaluation of the fallback instance, using first.or(Supplier).- Returns:
first
iffirst
is notnull
, orsecond
iffirst
isnull
andsecond
is notnull
- Throws:
java.lang.NullPointerException
- if bothfirst
andsecond
werenull
- Since:
- 3.0
-
-