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 Summary
Constructors Modifier Constructor Description protectedAbstractPreference(Class<T> valueClass, Preferences node, String key, T def)Creates an abstract preference.protectedAbstractPreference(Class<T> valueClass, Preferences node, String key, T def, Function<T,String> toString, Function<String,T> fromString)Creates an abstract preference.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected TfromString(String s)Converts a string to a value usingfomString.Tget(T def)Returns the value associated to this (node, key).TgetDefaultValue()StringgetKey()PreferencesgetNode()Class<T>getValueClass()voidput(T value)Puts a value into this (node, key).protected StringtoString(T value)Converts a value to a string usingtoString.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface cdc.prefs.Preference
get
-
-
-
-
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
toStringisnull,toStringmust be implemented.
IffromStringisnull,fromStringmust 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.toStringandfromStringmust 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 usingtoString.If
valueisnull, returns"".- Parameters:
value- The value.- Returns:
- The corresponding string.
- Throws:
IllegalStateException- WhentoStringisnullandvalueis notnull.
-
fromString
protected T fromString(String s)
Converts a string to a value usingfomString.If
sisnullor empty, returnsnull.- Parameters:
s- The string.- Returns:
- The corresponding value.
- Throws:
IllegalStateException- WhenfromStringisnullandsis neithernullor empty.
-
getValueClass
public final Class<T> getValueClass()
- Specified by:
getValueClassin interfacePreference<T>- Returns:
- The value class.
-
getNode
public final Preferences getNode()
- Specified by:
getNodein interfacePreference<T>- Returns:
- The associated node.
-
getKey
public final String getKey()
- Specified by:
getKeyin interfacePreference<T>- Returns:
- The associated key.
-
getDefaultValue
public final T getDefaultValue()
- Specified by:
getDefaultValuein interfacePreference<T>- Returns:
- The default value.
-
put
public final void put(T value)
Description copied from interface:PreferencePuts a value into this (node, key).- Specified by:
putin interfacePreference<T>- Parameters:
value- The value.
-
get
public final T get(T def)
Description copied from interface:PreferenceReturns the value associated to this (node, key).- Specified by:
getin interfacePreference<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.
-
-