Package cdc.prefs

Class AbstractPreference<T>

    • Constructor Detail

      • 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 Detail

      • 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.
      • 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.