ConstantInitializer

public class org.apache.commons.lang3.concurrent.ConstantInitializer<T> implements ConcurrentInitializer<T>[source]

A very simple implementation of the ConcurrentInitializer interface 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.

Parameters:

<T> – The type of the object managed by this initializer

Since:

3.0

Constructors

ConstantInitializer(Object)

Creates a new instance of ConstantInitializer and initializes it with the object to be managed.

public ConstantInitializer(T obj)[source]

Creates a new instance of ConstantInitializer and initializes it with the object to be managed. The get() method will always return the object passed here. This class does not place any restrictions on the object. It may be null, then get() will return null, too.

Parameters:

obj – The object to be managed by this initializer

Methods

equals(Object)

Compares this object with another one.

get()

Gets the object managed by this initializer.

getObject()

Gets the object passed to the constructor.

hashCode()

Returns a hash code for this object.

isInitialized()

Tests whether this initializer is initialized.

toString()

Returns a string representation for this object.

@Override public boolean equals(Object obj)[source]

Compares this object with another one. This implementation returns true if and only if the passed in object is an instance of ConstantInitializer which refers to an object equals to the object managed by this instance.

Parameters:

obj – The object to compare to

Returns:

A flag whether the objects are equal

@Override public T get() throws ConcurrentException[source]

Gets the object managed by this initializer. This implementation just returns the object passed to the constructor.

Returns:

The object managed by this initializer

Throws:

org.apache.commons.lang3.concurrent.ConcurrentException – Thrown if an error occurs.

public final T getObject()[source]

Gets the object passed to the constructor. This is the same object returned by get(), but this method does not declare a checked exception.

Returns:

The object managed by this initializer

@Override public int hashCode()[source]

Returns a hash code for this object. This implementation returns the hash code of the managed object.

Returns:

A hash code for this object

public boolean isInitialized()[source]

Tests whether this initializer is initialized. Always returns true because initialization occurs during construction.

Returns:

true.

Since:

3.14.0

@Override public String toString()[source]

Returns a string representation for this object. This string also contains a string representation of the object managed by this initializer.

Returns:

A string for this object