Class StringHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<String>
org.apache.juneau.commons.lang.StringHolder

public class StringHolder extends Holder<String>
A simple mutable string value.

This class extends Holder<String> and adds convenience methods for string equality testing, which are useful in lambdas and conditional logic.

Notes:
  • This class is not thread-safe. For concurrent access, use synchronization or atomic classes.
Example:

// Create a mutable string StringHolder name = StringHolder.of("John"); // Check equality if (name.is("John")) { System.out.println("Name is John"); } // Check multiple values if (name.isAny("John", "Jane", "Bob")) { System.out.println("Name found"); }

See Also:
  • Constructor Details

    • StringHolder

      public StringHolder()
      Constructor.
    • StringHolder

      public StringHolder(String value)
      Constructor.
      Parameters:
      value - The initial value. Can be null.
  • Method Details

    • create

      public static StringHolder create()
      Creates a new empty string value.
      Example:

      StringHolder name = StringHolder.create(); assertNull(name.get());

      Returns:
      A new string value.
    • of

      public static StringHolder of(String value)
      Creates a new string value with the specified initial value.
      Example:

      StringHolder name = StringHolder.of("Hello"); assertEquals("Hello", name.get());

      Parameters:
      value - The initial value. Can be null.
      Returns:
      A new string value.
    • is

      public boolean is(String value)
      Checks if the current value is equal to the specified value.

      Uses Shorts.eq(Object, Object) for deep equality comparison, which handles nulls safely.

      Example:

      StringHolder name = StringHolder.of("John"); assertTrue(name.is("John")); assertFalse(name.is("Jane")); // Handles null safely StringHolder empty = StringHolder.create(); assertTrue(empty.is(null));

      Overrides:
      is in class Holder<String>
      Parameters:
      value - The value to compare to. Can be null.
      Returns:
      true if the current value is equal to the specified value.
    • isAny

      public boolean isAny(String... values)
      Checks if the current value matches any of the specified values.

      Uses Shorts.eq(Object, Object) for deep equality comparison of each value.

      Example:

      StringHolder name = StringHolder.of("John"); assertTrue(name.isAny("John", "Jane", "Bob")); assertFalse(name.isAny("Alice", "Charlie")); // Empty array returns false assertFalse(name.isAny());

      Parameters:
      values - The values to compare to. Must not be null.
      Returns:
      true if the current value matches any of the specified values.