Class ConstantInitializer<T>
- java.lang.Object
-
- org.apache.commons.lang3.concurrent.ConstantInitializer<T>
-
- Type Parameters:
T- The type of the object managed by this initializer
- All Implemented Interfaces:
ConcurrentInitializer<T>,FailableSupplier<T,ConcurrentException>
public class ConstantInitializer<T> extends java.lang.Object implements ConcurrentInitializer<T>
A very simple implementation of theConcurrentInitializerinterface which always returns the same object.An instance of this class is passed a reference to an object when it is constructed. The
get()method just returns this object. No synchronization is required.This class is useful for instance for unit testing or in cases where a specific object has to be passed to an object which expects a
ConcurrentInitializer.- Since:
- 3.0
-
-
Field Summary
-
Fields inherited from interface org.apache.commons.lang3.function.FailableSupplier
NUL
-
-
Constructor Summary
Constructors Constructor Description ConstantInitializer(T obj)Creates a new instance ofConstantInitializerand initializes it with the object to be managed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)Compares this object with another one.Tget()Gets the object managed by this initializer.TgetObject()Gets the object passed to the constructor.inthashCode()Returns a hash code for this object.booleanisInitialized()Tests whether this initializer is initialized.java.lang.StringtoString()Returns a string representation for this object.
-
-
-
Constructor Detail
-
ConstantInitializer
public ConstantInitializer(T obj)
Creates a new instance ofConstantInitializerand initializes it with the object to be managed. Theget()method will always return the object passed here. This class does not place any restrictions on the object. It may benull, thenget()will returnnull, too.- Parameters:
obj- The object to be managed by this initializer
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
Compares this object with another one. This implementation returns true if and only if the passed in object is an instance ofConstantInitializerwhich refers to an object equals to the object managed by this instance.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The object to compare to- Returns:
- A flag whether the objects are equal
-
get
public T get() throws ConcurrentException
Gets the object managed by this initializer. This implementation just returns the object passed to the constructor.- Specified by:
getin interfaceFailableSupplier<T,ConcurrentException>- Returns:
- The object managed by this initializer
- Throws:
ConcurrentException- Thrown if an error occurs.
-
getObject
public final T getObject()
Gets the object passed to the constructor. This is the same object returned byget(), but this method does not declare a checked exception.- Returns:
- The object managed by this initializer
-
hashCode
public int hashCode()
Returns a hash code for this object. This implementation returns the hash code of the managed object.- Overrides:
hashCodein classjava.lang.Object- Returns:
- A hash code for this object
-
isInitialized
public boolean isInitialized()
Tests whether this initializer is initialized. Always returns true because initialization occurs during construction.- Returns:
- true.
- Since:
- 3.14.0
-
toString
public java.lang.String toString()
Returns a string representation for this object. This string also contains a string representation of the object managed by this initializer.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string for this object
-
-