Package cdc.prefs

Class AbstractPreference<T>

java.lang.Object
cdc.prefs.AbstractPreference<T>
Type Parameters:
T - The preference type.
All Implemented Interfaces:
Preference<T>
Direct Known Subclasses:
BooleanPreference, BytePreference, DatePreference, DoublePreference, EnumPreference, FilePreference, FloatPreference, IntegerPreference, LongPreference, ShortPreference, StringPreference

public abstract class AbstractPreference<T> extends Object implements Preference<T>
Base implementation of Preference.
Author:
Damien Carbonne
  • Constructor Details

    • AbstractPreference

      protected AbstractPreference(Class<T> valueClass, Preferences node, String key, T def, Function<T,String> toString, Function<String,T> fromString)
      Creates an abstract preference.

      If toString is null, toString must be implemented.
      If fromString is null, fromString must be implemented.

      Parameters:
      valueClass - The value class.
      node - The Preferences node.
      key - The preferences key.
      def - The default value.
      toString - Optional function used to convert from value type to string.
      fromString - Optional function used to convert from string to value type.
    • AbstractPreference

      protected AbstractPreference(Class<T> valueClass, Preferences node, String key, T def)
      Creates an abstract preference.

      toString and fromString must be implemented.

      Parameters:
      valueClass - The value class.
      node - The Preferences node.
      key - The preferences key.
      def - The default value.
  • Method Details

    • toString

      protected String toString(T value)
      Converts a value to a string using toString.

      If value is null, returns "".

      Parameters:
      value - The value.
      Returns:
      The corresponding string.
      Throws:
      IllegalStateException - When toString is null and value is not null.
    • fromString

      protected T fromString(String s)
      Converts a string to a value using fomString.

      If s is null or empty, returns null.

      Parameters:
      s - The string.
      Returns:
      The corresponding value.
      Throws:
      IllegalStateException - When fromString is null ands is neither null or empty.
    • getValueClass

      public final Class<T> getValueClass()
      Specified by:
      getValueClass in interface Preference<T>
      Returns:
      The value class.
    • getNode

      public final Preferences getNode()
      Specified by:
      getNode in interface Preference<T>
      Returns:
      The associated node.
    • getKey

      public final String getKey()
      Specified by:
      getKey in interface Preference<T>
      Returns:
      The associated key.
    • getDefaultValue

      public final T getDefaultValue()
      Specified by:
      getDefaultValue in interface Preference<T>
      Returns:
      The default value.
    • put

      public final void put(T value)
      Description copied from interface: Preference
      Puts a value into this (node, key).
      Specified by:
      put in interface Preference<T>
      Parameters:
      value - The value.
    • get

      public final T get(T def)
      Description copied from interface: Preference
      Returns the value associated to this (node, key).
      Specified by:
      get in interface Preference<T>
      Parameters:
      def - The default value to return when there is no associated value or value processing raises an exception
      Returns:
      The associated value or def.