Package io.microsphere.util
Class ValueHolder<V>
- java.lang.Object
-
- io.microsphere.util.ValueHolder<V>
-
- Type Parameters:
V- the type of the value being held
public class ValueHolder<V> extends java.lang.ObjectA container class to hold a mutable value of any type. This class is not thread-safe.It provides methods to set, get, and reset the value, as well as utility methods like
equals(Object),hashCode(), andtoString()for general usage.Example Usage
// Create an empty ValueHolder for String type ValueHolder<String> stringHolder = new ValueHolder<>(); stringHolder.setValue("Hello"); System.out.println(stringHolder.getValue()); // Output: Hello // Create a ValueHolder with an initial value using factory method ValueHolder<Integer> intHolder = ValueHolder.of(123); System.out.println(intHolder.getValue()); // Output: 123 // Reset the value back to null intHolder.reset(); System.out.println(intHolder.getValue()); // Output: null- Since:
- 1.0.0
- Author:
- Mercy
-
-
Constructor Summary
Constructors Constructor Description ValueHolder()Constructs an emptyValueHolderwith anullinitial value.ValueHolder(V value)Constructs aValueHolderwith the specified initial value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object o)Checks whether thisValueHolderis equal to another object.VgetValue()Returns the currently held value.inthashCode()Returns the hash code of the held value usingObjects.hashCode(Object).static <V> ValueHolder<V>of(V value)Creates a newValueHoldercontaining the specified value.voidreset()Resets the held value tonull.voidsetValue(V value)Sets the held value to the specified value.java.lang.StringtoString()Returns a string representation of thisValueHolderin the formatValueHolder{value=...}.
-
-
-
Constructor Detail
-
ValueHolder
public ValueHolder()
Constructs an emptyValueHolderwith anullinitial value.Example Usage
ValueHolder<String> holder = new ValueHolder<>(); System.out.println(holder.getValue()); // Output: null
-
ValueHolder
public ValueHolder(V value)
Constructs aValueHolderwith the specified initial value.Example Usage
ValueHolder<Integer> holder = new ValueHolder<>(42); System.out.println(holder.getValue()); // Output: 42- Parameters:
value- the initial value to hold, may benull
-
-
Method Detail
-
getValue
public V getValue()
Returns the currently held value.Example Usage
ValueHolder<String> holder = ValueHolder.of("Hello"); String value = holder.getValue(); // "Hello"- Returns:
- the held value, or
nullif no value has been set
-
setValue
public void setValue(V value)
Sets the held value to the specified value.Example Usage
ValueHolder<Double> holder = new ValueHolder<>(); holder.setValue(3.14); System.out.println(holder.getValue()); // Output: 3.14- Parameters:
value- the value to hold, may benull
-
reset
public void reset()
Resets the held value tonull.Example Usage
ValueHolder<String> holder = ValueHolder.of("data"); holder.reset(); System.out.println(holder.getValue()); // Output: null
-
equals
public boolean equals(java.lang.Object o)
Checks whether thisValueHolderis equal to another object. TwoValueHolderinstances are considered equal if they hold equal values as determined byObjects.equals(Object, Object).Example Usage
ValueHolder<Integer> holder1 = ValueHolder.of(100); ValueHolder<Integer> holder2 = ValueHolder.of(100); System.out.println(holder1.equals(holder2)); // Output: true- Overrides:
equalsin classjava.lang.Object- Parameters:
o- the object to compare with- Returns:
trueif the other object is aValueHolderholding an equal value,falseotherwise
-
hashCode
public int hashCode()
Returns the hash code of the held value usingObjects.hashCode(Object).Example Usage
ValueHolder<String> holder = ValueHolder.of("key"); int hash = holder.hashCode(); // hash code of "key"- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code of the held value, or
0if the value isnull
-
toString
public java.lang.String toString()
Returns a string representation of thisValueHolderin the formatValueHolder{value=...}.Example Usage
ValueHolder<String> holder = ValueHolder.of("test"); System.out.println(holder.toString()); // Output: ValueHolder{value=test}- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this holder and its value
-
of
public static <V> ValueHolder<V> of(V value)
Creates a newValueHoldercontaining the specified value.Example Usage
ValueHolder<String> holder = ValueHolder.of("Hello World"); System.out.println(holder.getValue()); // Output: Hello World- Type Parameters:
V- the type of the value- Parameters:
value- the value to hold, may benull- Returns:
- a new
ValueHoldercontaining the given value
-
-