Class ValueHolder<V>

  • Type Parameters:
    V - the type of the value being held

    public class ValueHolder<V>
    extends java.lang.Object
    A 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(), and toString() 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
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)
      Checks whether this ValueHolder is equal to another object.
      V getValue()
      Returns the currently held value.
      int hashCode()
      Returns the hash code of the held value using Objects.hashCode(Object).
      static <V> ValueHolder<V> of​(V value)
      Creates a new ValueHolder containing the specified value.
      void reset()
      Resets the held value to null.
      void setValue​(V value)
      Sets the held value to the specified value.
      java.lang.String toString()
      Returns a string representation of this ValueHolder in the format ValueHolder{value=...}.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ValueHolder

        public ValueHolder()
        Constructs an empty ValueHolder with a null initial value.

        Example Usage

        
           ValueHolder<String> holder = new ValueHolder<>();
           System.out.println(holder.getValue()); // Output: null
         
      • ValueHolder

        public ValueHolder​(V value)
        Constructs a ValueHolder with 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 be null
    • 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 null if 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 be null
      • reset

        public void reset()
        Resets the held value to null.

        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 this ValueHolder is equal to another object. Two ValueHolder instances are considered equal if they hold equal values as determined by Objects.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:
        equals in class java.lang.Object
        Parameters:
        o - the object to compare with
        Returns:
        true if the other object is a ValueHolder holding an equal value, false otherwise
      • hashCode

        public int hashCode()
        Returns the hash code of the held value using Objects.hashCode(Object).

        Example Usage

        
           ValueHolder<String> holder = ValueHolder.of("key");
           int hash = holder.hashCode(); // hash code of "key"
         
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code of the held value, or 0 if the value is null
      • toString

        public java.lang.String toString()
        Returns a string representation of this ValueHolder in the format ValueHolder{value=...}.

        Example Usage

        
           ValueHolder<String> holder = ValueHolder.of("test");
           System.out.println(holder.toString()); // Output: ValueHolder{value=test}
         
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this holder and its value
      • of

        public static <V> ValueHolder<V> of​(V value)
        Creates a new ValueHolder containing 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 be null
        Returns:
        a new ValueHolder containing the given value