Class Option<T>

java.lang.Object
com.headius.options.Option<T>
Type Parameters:
T - the type of value associated with the option
Direct Known Subclasses:
BooleanOption, EnumerationOption, IntegerOption, StringOption

public abstract class Option<T> extends Object
Represents a single option, with a category, name, value type, options, default value, and description. This type should be subclassed for specific types of values.
See Also:
  • Field Details

    • OptionComparator

      private static Comparator<Option> OptionComparator
    • category

      private final Enum category
    • prefix

      private final String prefix
    • shortName

      private final String shortName
    • longName

      private final String longName
    • displayName

      private final String displayName
    • type

      protected final Class<T> type
    • options

      private final T[] options
    • defval

      protected final T defval
    • description

      private final String description
    • forced

      private String forced
    • specified

      private boolean specified
    • value

      private volatile T value
    • loaded

      private volatile boolean loaded
  • Constructor Details

    • Option

      public Option(String prefix, String shortName, Class<T> type, Enum category, T[] options, T defval, String description)
      Create a new option with the given values.
      Type Parameters:
      C - an enumeration type
      Parameters:
      prefix - the prefix used for loading this option from properties
      shortName - the rest of the property name
      type - the value type of the option
      category - the category to which this option belongs
      options - a list of supported for the option, or null if the set is not applicable
      defval - the default value for the option
      description - a description for the option
    • Option

      public Option(String longName, Class<T> type, Enum category, T[] options, T defval, String description)
      Create a new option with the given values.
      Type Parameters:
      C - an enumeration type
      Parameters:
      longName - the property name
      type - the value type of the option
      category - the category to which this option belongs
      options - a list of supported for the option, or null if the set is not applicable
      defval - the default value for the option
      description - a description for the option
  • Method Details

    • string

      public static Option<String> string(String prefix, String name, Enum category, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String longName, Enum category, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String prefix, String name, Enum category, String defval, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String longName, Enum category, String defval, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String prefix, String name, Enum category, String[] options, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String longName, Enum category, String[] options, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String prefix, String name, Enum category, String[] options, String defval, String description)
      Create a new String option with the given configuration.
    • string

      public static Option<String> string(String longName, Enum category, String[] options, String defval, String description)
      Create a new String option with the given configuration.
    • bool

      public static Option<Boolean> bool(String prefix, String name, Enum category, String description)
      Create a new Boolean option with the given configuration.
    • bool

      public static Option<Boolean> bool(String longName, Enum category, String description)
      Create a new Boolean option with the given configuration.
    • bool

      public static Option<Boolean> bool(String prefix, String name, Enum category, Boolean defval, String description)
      Create a new Boolean option with the given configuration.
    • bool

      public static Option<Boolean> bool(String longName, Enum category, Boolean defval, String description)
      Create a new Boolean option with the given configuration.
    • integer

      public static Option<Integer> integer(String prefix, String name, Enum category, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String prefix, String name, Enum category, Integer[] options, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String longName, Enum category, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String longName, Enum category, Integer[] options, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String prefix, String name, Enum category, Integer defval, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String longName, Enum category, Integer defval, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String prefix, String name, Enum category, Integer[] options, Integer defval, String description)
      Create a new Integer option with the given configuration.
    • integer

      public static Option<Integer> integer(String longName, Enum category, Integer[] options, Integer defval, String description)
      Create a new Integer option with the given configuration.
    • enumeration

      public static <T extends Enum<T>> Option<T> enumeration(String prefix, String name, Enum category, Class<T> enumClass, String description)
      Create a new Enumeration-based option with the given configuration.
    • enumeration

      public static <T extends Enum<T>> Option<T> enumeration(String longName, Enum category, Class<T> enumClass, String description)
      Create a new Enumeration-based option with the given configuration.
    • enumeration

      public static <T extends Enum<T>> Option<T> enumeration(String prefix, String name, Enum category, T defval, String description)
      Create a new Enumeration-based option with the given configuration.
    • enumeration

      public static <T extends Enum<T>> Option<T> enumeration(String longName, Enum category, T defval, String description)
      Create a new Enumeration-based option with the given configuration.
    • formatValues

      public static String formatValues(Option... options)
      Format the given options to show their loaded values in the current JVM.
    • formatValues

      public static String formatValues(Collection<Option> options)
      Format the given options to show their loaded values in the current JVM.
    • formatOptions

      public static String formatOptions(Option... options)
      Format the given options in a way suitable for use as a configuration file or documentation.
    • formatOptions

      public static String formatOptions(Collection<Option> options)
      Format the given options in a way suitable for use as a configuration file or documentation.
    • encodeWhitespace

      private static String encodeWhitespace(Object obj)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • loadProperty

      public String loadProperty()
      Load the option's property, as if by calling java.lang.System#getProperty
    • isSpecified

      public boolean isSpecified()
      Return true if the option's property was specified, false otherwise.
    • load

      public final T load()
      Return the value of the option, loading if it has not been already.
    • reload

      public final T reload()
      Force a load of the option's property and return the loaded value.
    • force

      public void force(String value)
      Force the property value to the given value for all future loads and reloads.
      Parameters:
      value -
    • unforce

      public void unforce()
      Undoes any previous force, and goes back to an unloaded state.
    • reloadValue

      protected abstract T reloadValue()
      Perform the appropriate load and conversion for the option's property.
    • shortName

      public String shortName()
      If the option has a short name, return it. Otherwise null.
    • propertyName

      public String propertyName()
      Return the full property name for the option.
    • prefix

      public String prefix()
      If the property has a prefix, return it. Otherwise null.
    • displayName

      public String displayName()
      The display name of the option's property, either the short name or the full name.
    • type

      public Class<T> type()
      The type returned for the option.
      Returns:
    • options

      public T[] options()
      The array of accepted values for the option, or null if any values are accepted.
    • defaultValue

      public T defaultValue()
      The default value for the option, or null if no default is provided.
    • description

      public String description()
      The long description of the property, as for documentation and configuration file templates.