Class Preferences
- Direct Known Subclasses:
AbstractPreferences
public abstract class Preferences extends Object
Preferences represents one node in a
preference tree, which provides a mechanism to store and access configuration
data in a hierarchical way. Two hierarchy trees are maintained, one for
system preferences shared by all users and the other for user preferences
specific to the user. Preferences hierarchy trees and data are stored
in an implementation-dependent back-end.
Every node has one name and one unique absolute path following the same notational conventions as directories in a file system. The root node's name is "", and other node name strings cannot contain the slash character and cannot be empty. The root node's absolute path is "/", and all other nodes' absolute paths are constructed in the standard way: <parent's absolute path> + "/" + <node's name>. Since the set of nodes forms a tree with the root node at its base, all absolute paths start with the slash character. Every node has one relative path to each of its ancestors. The relative path doesn't start with slash: it equals the node's absolute path with leading substring removed corresponding to the ancestor's absolute path and a slash.
Modification to preferences data may be asynchronous, which means that
preference update method calls may return immediately instead of blocking.
The flush() and sync() methods force the back-end to
synchronously perform all pending updates, but the implementation is
permitted to perform the modifications on the underlying back-end data
at any time between the moment the request is made and the moment the
flush() or sync() method returns. Please note that if the JVM
exits normally, the implementation must assure all modifications are
persisted implicitly.
When invoking a method that retrieves preferences, the user must provide
a default value. The default value is returned when the preferences cannot
be found or the back-end is unavailable. Some other methods will throw
BackingStoreException when the back-end is unavailable.
Preferences can be exported to and imported from an XML files. These documents must have an XML DOCTYPE declaration:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
This system URI is not really accessed by network, it is only a
identification string. Visit the DTD location to see the actual format
permitted.
There must be a concrete PreferencesFactory type for every concrete
Preferences type developed. Every J2SE implementation must provide a
default implementation for every supported platform, and must also provide a
means of replacing the default implementation. This implementation uses the
system property java.util.prefs.PreferencesFactory to determine which
preferences implementation to use.
The methods of this class are thread-safe. If multiple JVMs are using the same back-end concurrently, the back-end won't be corrupted, but no other behavior guarantees are made.
- Since:
- 1.4
- See Also:
PreferencesFactory
-
Field Summary
Fields Modifier and Type Field Description static intMAX_KEY_LENGTHMaximum size in characters allowed for a preferences key.static intMAX_NAME_LENGTHMaximum size in characters allowed for a preferences name.static intMAX_VALUE_LENGTHMaximum size in characters allowed for a preferences value. -
Constructor Summary
Constructors Modifier Constructor Description protectedPreferences()Default constructor, for use by subclasses only. -
Method Summary
Modifier and Type Method Description abstract StringabsolutePath()Gets the absolute path string of this preference node.abstract voidaddNodeChangeListener(NodeChangeListener ncl)Registers aNodeChangeListenerinstance for this node, which will handleNodeChangeEvents.abstract voidaddPreferenceChangeListener(PreferenceChangeListener pcl)Registers aPreferenceChangeListenerinstance for this node, which will handlePreferenceChangeEvents.abstract String[]childrenNames()Returns the names of all children of this node or an empty array if this node has no children.abstract voidclear()Removes all preferences of this node.abstract voidexportNode(OutputStream ostream)Exports all of the preferences of this node to a XML document using the given output stream.abstract voidexportSubtree(OutputStream ostream)Exports all of the preferences of this node and all its descendants to a XML document using the given output stream.abstract voidflush()Forces all pending updates to this node and its descendants to be persisted in the backing store.abstract Stringget(String key, String deflt)Gets theStringvalue mapped to the given key or its default value if no value is mapped or no backing store is available.abstract booleangetBoolean(String key, boolean deflt)Gets thebooleanvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is invalid.abstract byte[]getByteArray(String key, byte[] deflt)Gets thebytearray value mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.abstract doublegetDouble(String key, double deflt)Gets thedoublevalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.abstract floatgetFloat(String key, float deflt)Gets thefloatvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.abstract intgetInt(String key, int deflt)Gets theintvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.abstract longgetLong(String key, long deflt)Gets thelongvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.static voidimportPreferences(InputStream istream)Imports all the preferences from an XML document using the given input stream.abstract booleanisUserNode()Returns whether this is a user preference node.abstract String[]keys()Returns all preference keys stored in this node or an empty array if no key was found.abstract Stringname()Returns the name of this node.abstract Preferencesnode(String path)Returns the preference node with the given path name.abstract booleannodeExists(String path)Returns whether the preference node with the given path name exists.abstract Preferencesparent()Returns the parent preference node of this node ornullif this node is the root node.abstract voidput(String key, String value)Adds a new preference to this node using the given key and value or updates the value if a preference with the given key already exists.abstract voidputBoolean(String key, boolean value)Adds a new preference with abooleanvalue to this node using the given key and value or updates the value if a preference with the given key already exists.abstract voidputByteArray(String key, byte[] value)Adds a new preference to this node using the given key and the string form of the given value or updates the value if a preference with the given key already exists.abstract voidputDouble(String key, double value)Adds a new preference to this node using the given key anddoublevalue or updates the value if a preference with the given key already exists.abstract voidputFloat(String key, float value)Adds a new preference to this node using the given key andfloatvalue or updates the value if a preference with the given key already exists.abstract voidputInt(String key, int value)Adds a new preference to this node using the given key andintvalue or updates the value if a preference with the given key already exists.abstract voidputLong(String key, long value)Adds a new preference to this node using the given key andlongvalue or updates the value if a preference with the given key already exists.abstract voidremove(String key)Removes the preference mapped to the given key from this node.abstract voidremoveNode()Removes this preference node with all its descendants.abstract voidremoveNodeChangeListener(NodeChangeListener ncl)Removes the givenNodeChangeListenerinstance from this node.abstract voidremovePreferenceChangeListener(PreferenceChangeListener pcl)Removes the givenPreferenceChangeListenerinstance from this node.abstract voidsync()Synchronizes the data of this preference node and its descendants with the back-end preference store.static PreferencessystemNodeForPackage(Class<?> c)Returns the system preference node for the package of the given class.static PreferencessystemRoot()Returns the root node of the system preference hierarchy.abstract StringtoString()Returns a string representation of this node.static PreferencesuserNodeForPackage(Class<?> c)Returns the user preference node for the package of the given class.static PreferencesuserRoot()Returns the root node of the user preference hierarchy.
-
Field Details
-
MAX_KEY_LENGTH
public static final int MAX_KEY_LENGTHMaximum size in characters allowed for a preferences key.- See Also:
- Constant Field Values
-
MAX_NAME_LENGTH
public static final int MAX_NAME_LENGTHMaximum size in characters allowed for a preferences name.- See Also:
- Constant Field Values
-
MAX_VALUE_LENGTH
public static final int MAX_VALUE_LENGTHMaximum size in characters allowed for a preferences value.- See Also:
- Constant Field Values
-
-
Constructor Details
-
Preferences
protected Preferences()Default constructor, for use by subclasses only.
-
-
Method Details
-
absolutePath
Gets the absolute path string of this preference node.- Returns:
- the preference node's absolute path string.
-
childrenNames
Returns the names of all children of this node or an empty array if this node has no children.- Returns:
- the names of all children of this node.
- Throws:
BackingStoreException- if backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
clear
Removes all preferences of this node.- Throws:
BackingStoreException- if backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
exportNode
Exports all of the preferences of this node to a XML document using the given output stream.This XML document uses the UTF-8 encoding and is written according to the DTD in its DOCTYPE declaration, which is the following:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe.- Parameters:
ostream- the output stream to write the XML-formatted data to.- Throws:
IOException- if an error occurs while exporting.BackingStoreException- if the backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
exportSubtree
Exports all of the preferences of this node and all its descendants to a XML document using the given output stream.This XML document uses the UTF-8 encoding and is written according to the DTD in its DOCTYPE declaration, which is the following:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe.- Parameters:
ostream- the output stream to write the XML-formatted data to.- Throws:
IOException- if an error occurs while exporting.BackingStoreException- if the backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
flush
Forces all pending updates to this node and its descendants to be persisted in the backing store.If this node has been removed, the invocation of this method only flushes this node, not its descendants.
- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
get
Gets theStringvalue mapped to the given key or its default value if no value is mapped or no backing store is available.Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key or no backing store is available.- Returns:
- the preference value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getBoolean
Gets thebooleanvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is invalid.The only valid values are the
String"true", which representstrueand "false", which representsfalse, ignoring case.Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the boolean value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getByteArray
Gets thebytearray value mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.To be valid, the value string must be Base64-encoded binary data. The Base64 encoding is as defined in RFC 2045, section 6.8.
Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the byte array value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getDouble
Gets thedoublevalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.To be valid, the value string must be a string that can be converted to a
doublebyDouble.parseDouble(String).Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the double value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getFloat
Gets thefloatvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.To be valid, the value string must be a string that can be converted to a
floatbyFloat.parseFloat(String).Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the float value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getInt
Gets theintvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.To be valid, the value string must be a string that can be converted to an
intbyInteger.parseInt(String).Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the integer value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
getLong
Gets thelongvalue mapped to the given key or its default value if no value is mapped, if the backing store is unavailable, or if the value is an invalid string.To be valid, the value string must be a string that can be converted to a
longbyLong.parseLong(String).Some implementations may store default values in backing stores. In this case, if there is no value mapped to the given key, the stored default value is returned.
- Parameters:
key- the preference key.deflt- the default value, which will be returned if no value is mapped to the given key, if the backing store is unavailable, or if the value is invalid.- Returns:
- the long value mapped to the given key.
- Throws:
IllegalStateException- if this node has been removed.NullPointerException- if the parameterkeyisnull.
-
importPreferences
public static void importPreferences(InputStream istream) throws InvalidPreferencesFormatException, IOExceptionImports all the preferences from an XML document using the given input stream.This XML document uses the UTF-8 encoding and must be written according to the DTD in its DOCTYPE declaration, which must be the following:
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe.- Parameters:
istream- the input stream to read the data from.- Throws:
InvalidPreferencesFormatException- if the data read from the given input stream is not from a valid XML document.IOException- if an error occurs while importing.
-
isUserNode
public abstract boolean isUserNode()Returns whether this is a user preference node.- Returns:
true, if this is a user preference node,falseif this is a system preference node.
-
keys
Returns all preference keys stored in this node or an empty array if no key was found.- Returns:
- the list of all preference keys of this node.
- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
name
Returns the name of this node.- Returns:
- the name of this node.
-
node
Returns the preference node with the given path name. The path name can be relative or absolute. The requested node and its ancestors will be created if they do not exist.The path is treated as relative to this node if it doesn't start with a slash, otherwise it will be treated as an absolute path.
- Parameters:
path- the path name of the requested preference node.- Returns:
- the requested preference node.
- Throws:
IllegalStateException- if this node has been removed.IllegalArgumentException- if the path name is invalid.NullPointerException- if the given path isnull.
-
nodeExists
Returns whether the preference node with the given path name exists. The path is treated as relative to this node if it doesn't start with a slash, otherwise it is treated as an absolute path.Please note that if this node has been removed, an invocation of this node will throw an
IllegalStateExceptionunless the given path is an empty string, which will returnfalse.- Parameters:
path- the path name of the preference node to query.- Returns:
true, if the queried preference node exists,falseotherwise.- Throws:
IllegalStateException- if this node has been removed and the path is not an empty string.IllegalArgumentException- if the path name is invalid.NullPointerException- if the given path isnull.BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
parent
Returns the parent preference node of this node ornullif this node is the root node.- Returns:
- the parent preference node of this node.
- Throws:
IllegalStateException- if this node has been removed.
-
put
Adds a new preference to this node using the given key and value or updates the value if a preference with the given key already exists.- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key or value isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTHor the value's length is bigger thanMAX_VALUE_LENGTH.IllegalStateException- if this node has been removed.
-
putBoolean
Adds a new preference with abooleanvalue to this node using the given key and value or updates the value if a preference with the given key already exists.- Parameters:
key- the preference key to be added or updated.value- the preferencebooleanvalue for the given key.- Throws:
NullPointerException- if the given key isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
putByteArray
Adds a new preference to this node using the given key and the string form of the given value or updates the value if a preference with the given key already exists.The string form of the value is the Base64-encoded binary data of the given byte array. The Base64 encoding is as defined in RFC 2045, section 6.8.
- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key or value isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTHor value's length is bigger than three quarters ofMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
putDouble
Adds a new preference to this node using the given key anddoublevalue or updates the value if a preference with the given key already exists.The value is stored in its string form, which is the result of invoking
Double.toString(double).- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
putFloat
Adds a new preference to this node using the given key andfloatvalue or updates the value if a preference with the given key already exists.The value is stored in its string form, which is the result of invoking
Float.toString(float).- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
putInt
Adds a new preference to this node using the given key andintvalue or updates the value if a preference with the given key already exists.The value is stored in its string form, which is the result of invoking
Integer.toString(int).- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
putLong
Adds a new preference to this node using the given key andlongvalue or updates the value if a preference with the given key already exists.The value is stored in its string form, which is the result of invoking
Long.toString(long).- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.- Throws:
NullPointerException- if the given key isnull.IllegalArgumentException- if the given key's length is bigger thanMAX_KEY_LENGTH.IllegalStateException- if this node has been removed.
-
remove
Removes the preference mapped to the given key from this node.- Parameters:
key- the key of the preference to be removed.- Throws:
NullPointerException- if the given key isnull.IllegalStateException- if this node has been removed.
-
removeNode
Removes this preference node with all its descendants. The removal won't necessarily be persisted until the methodflush()is invoked.- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.UnsupportedOperationException- if this is a root node.
-
addNodeChangeListener
Registers aNodeChangeListenerinstance for this node, which will handleNodeChangeEvents.NodeChangeEvents will be fired when a child node has been added to or removed from this node.- Parameters:
ncl- the listener to be registered.- Throws:
NullPointerException- if the given listener isnull.IllegalStateException- if this node has been removed.
-
addPreferenceChangeListener
Registers aPreferenceChangeListenerinstance for this node, which will handlePreferenceChangeEvents.PreferenceChangeEvents will be fired when a preference has been added to, removed from, or updated for this node.- Parameters:
pcl- the listener to be registered.- Throws:
NullPointerException- if the given listener isnull.IllegalStateException- if this node has been removed.
-
removeNodeChangeListener
Removes the givenNodeChangeListenerinstance from this node.- Parameters:
ncl- the listener to be removed.- Throws:
IllegalArgumentException- if the given listener isnull.IllegalStateException- if this node has been removed.
-
removePreferenceChangeListener
Removes the givenPreferenceChangeListenerinstance from this node.- Parameters:
pcl- the listener to be removed.- Throws:
IllegalArgumentException- if the given listener isnull.IllegalStateException- if this node has been removed.
-
sync
Synchronizes the data of this preference node and its descendants with the back-end preference store. Any changes found in the back-end data should be reflected in this node and its descendants, and at the same time any local changes to this node and descendants should be persisted.- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.IllegalStateException- if this node has been removed.
-
systemNodeForPackage
Returns the system preference node for the package of the given class. The absolute path of the returned node is one slash followed by the given class's full package name, replacing each period character ('.') with a slash. For example, the absolute path of the preference associated with the class Object would be "/java/lang". As a special case, the unnamed package is associated with a preference node "/<unnamed>". This method will create the node and its ancestors as needed. Any nodes created by this method won't necessarily be persisted until the methodflush()is invoked.- Parameters:
c- the given class.- Returns:
- the system preference node for the package of the given class.
- Throws:
NullPointerException- if the given class isnull.
-
systemRoot
Returns the root node of the system preference hierarchy.- Returns:
- the system preference hierarchy root node.
-
userNodeForPackage
Returns the user preference node for the package of the given class. The absolute path of the returned node is one slash followed by the given class's full package name, replacing each period character ('.') with a slash. For example, the absolute path of the preference associated with the class Object would be "/java/lang". As a special case, the unnamed package is associated with a preference node "/<unnamed>". This method will create the node and its ancestors as needed. Any nodes created by this method won't necessarily be persisted until the methodflush()is invoked.- Parameters:
c- the given class.- Returns:
- the user preference node for the package of the given class.
- Throws:
NullPointerException- if the given class isnull.
-
userRoot
Returns the root node of the user preference hierarchy.- Returns:
- the user preference hierarchy root node.
-
toString
Returns a string representation of this node. The format is "User/System Preference Node: " followed by this node's absolute path.
-