Class StringSetting

All Implemented Interfaces:
Supplier<String>, OptionalSupplier<String>

public class StringSetting extends Setting<String>
A specialized Setting for string values that provides convenience methods for type conversion.

This class extends Setting with type-specific conversion methods such as asInteger(), asBoolean(), asCharset(), etc.

  • Constructor Details

    • StringSetting

      public StringSetting(Settings settings, Supplier<String> supplier)
      Creates a new StringSetting from a Settings instance and a Supplier.
      Parameters:
      settings - The Settings instance that created this setting. Must not be null.
      supplier - The supplier that provides the string value. Must not be null.
  • Method Details

    • mapString

      public StringSetting mapString(Function<? super String,? extends String> mapper)
      If a value is present, applies the provided mapping function to it and returns a StringSetting describing the result.

      This method is specifically for String-to-String mappings. For mappings to other types, use the inherited Setting.map(Function) method.

      The returned StringSetting maintains its own cache, independent of this supplier. Resetting the mapped supplier does not affect this supplier, and vice versa.

      Parameters:
      mapper - A mapping function to apply to the value, if present. Must not be null.
      Returns:
      A StringSetting describing the result of applying a mapping function to the value of this StringSetting, if a value is present, otherwise an empty StringSetting.
    • filter

      public StringSetting filter(Predicate<? super String> predicate)
      If a value is present, and the value matches the given predicate, returns a StringSetting describing the value, otherwise returns an empty StringSetting.

      The returned StringSetting maintains its own cache, independent of this supplier. Resetting the filtered supplier does not affect this supplier, and vice versa.

      Specified by:
      filter in interface OptionalSupplier<String>
      Overrides:
      filter in class Setting<String>
      Parameters:
      predicate - A predicate to apply to the value, if present. Must not be null.
      Returns:
      A StringSetting describing the value of this StringSetting if a value is present and the value matches the given predicate, otherwise an empty StringSetting.
    • asInteger

      Converts the string value to an Integer.

      The property value is parsed using Integer.valueOf(String). If the property is not found or cannot be parsed as an integer, returns Optional.empty().

      Returns:
      The property value as an Integer, or Optional.empty() if not found or not a valid integer.
    • asLong

      public Setting<Long> asLong()
      Converts the string value to a Long.

      The property value is parsed using Long.valueOf(String). If the property is not found or cannot be parsed as a long, returns Optional.empty().

      Returns:
      The property value as a Long, or Optional.empty() if not found or not a valid long.
    • asBoolean

      Converts the string value to a Boolean.

      The property value is parsed using Boolean.parseBoolean(String), which returns true if the value is (case-insensitive) "true", otherwise false. Note that this method will return Optional.of(false) for any non-empty value that is not "true", and Optional.empty() only if the property is not set.

      Returns:
      The property value as a Boolean, or Optional.empty() if not found.
    • asDouble

      public Setting<Double> asDouble()
      Converts the string value to a Double.

      The property value is parsed using Double.valueOf(String). If the property is not found or cannot be parsed as a double, returns Optional.empty().

      Returns:
      The property value as a Double, or Optional.empty() if not found or not a valid double.
    • asFloat

      public Setting<Float> asFloat()
      Converts the string value to a Float.

      The property value is parsed using Float.valueOf(String). If the property is not found or cannot be parsed as a float, returns Optional.empty().

      Returns:
      The property value as a Float, or Optional.empty() if not found or not a valid float.
    • asFile

      public Setting<File> asFile()
      Converts the string value to a File.

      The property value is converted to a File using the File(String) constructor. If the property is not found, returns Optional.empty(). Note that this method does not validate that the file path is valid or that the file exists.

      Returns:
      The property value as a File, or Optional.empty() if not found.
    • asPath

      public Setting<Path> asPath()
      Converts the string value to a Path.

      The property value is converted to a Path using Paths.get(String, String...). If the property is not found or the path string is invalid, returns Optional.empty().

      Returns:
      The property value as a Path, or Optional.empty() if not found or not a valid path.
    • asURI

      public Setting<URI> asURI()
      Converts the string value to a URI.

      The property value is converted to a URI using URI.create(String). If the property is not found or the URI string is invalid, returns Optional.empty().

      Returns:
      The property value as a URI, or Optional.empty() if not found or not a valid URI.
    • asCharset

      Converts the string value to a Charset.

      The property value is converted to a Charset using Charset.forName(String). If the property is not found or the charset name is not supported, returns Optional.empty().

      Returns:
      The property value as a Charset, or Optional.empty() if not found or not a valid charset.
    • asType

      public <T> Setting<T> asType(Class<T> c)
      Converts the string value to the specified type using the Settings type conversion functions.

      The property value is converted using Settings.toType(String, Class). If the property is not found or cannot be converted to the specified type, returns Optional.empty().

      Type Parameters:
      T - The target type.
      Parameters:
      c - The target class. Must not be null.
      Returns:
      The property value as the specified type, or Optional.empty() if not found or not a valid conversion.