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.
PreferencesFactory| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_KEY_LENGTH
Maximum size in characters allowed for a preferences key.
|
static int |
MAX_NAME_LENGTH
Maximum size in characters allowed for a preferences name.
|
static int |
MAX_VALUE_LENGTH
Maximum size in characters allowed for a preferences value.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Preferences()
Default constructor, for use by subclasses only.
|
| Modifier and Type | Method and Description |
|---|---|
abstract String |
absolutePath()
Gets the absolute path string of this preference node.
|
abstract void |
addNodeChangeListener(NodeChangeListener ncl)
Registers a
NodeChangeListener instance for this node, which will
handle NodeChangeEvents. |
abstract void |
addPreferenceChangeListener(PreferenceChangeListener pcl)
Registers a
PreferenceChangeListener instance for this node,
which will handle PreferenceChangeEvents. |
abstract String[] |
childrenNames()
Returns the names of all children of this node or an empty array if this
node has no children.
|
abstract void |
clear()
Removes all preferences of this node.
|
abstract void |
exportNode(OutputStream ostream)
Exports all of the preferences of this node to a XML document using the
given output stream.
|
abstract void |
exportSubtree(OutputStream ostream)
Exports all of the preferences of this node and all its descendants to a
XML document using the given output stream.
|
abstract void |
flush()
Forces all pending updates to this node and its descendants to be
persisted in the backing store.
|
abstract String |
get(String key,
String deflt)
Gets the
String value mapped to the given key or its default
value if no value is mapped or no backing store is available. |
abstract boolean |
getBoolean(String key,
boolean deflt)
Gets the
boolean 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 invalid. |
abstract byte[] |
getByteArray(String key,
byte[] deflt)
Gets the
byte array 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 double |
getDouble(String key,
double deflt)
Gets the
double 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 float |
getFloat(String key,
float deflt)
Gets the
float 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 int |
getInt(String key,
int deflt)
Gets the
int 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 long |
getLong(String key,
long deflt)
Gets the
long 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. |
static void |
importPreferences(InputStream istream)
Imports all the preferences from an XML document using the given input
stream.
|
abstract boolean |
isUserNode()
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 String |
name()
Returns the name of this node.
|
abstract Preferences |
node(String path)
Returns the preference node with the given path name.
|
abstract boolean |
nodeExists(String path)
Returns whether the preference node with the given path name exists.
|
abstract Preferences |
parent()
Returns the parent preference node of this node or
null if this
node is the root node. |
abstract void |
put(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 void |
putBoolean(String key,
boolean value)
Adds a new preference with a
boolean value to this node using the
given key and value or updates the value if a preference with the given
key already exists. |
abstract void |
putByteArray(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 void |
putDouble(String key,
double value)
Adds a new preference to this node using the given key and
double
value or updates the value if a preference with the
given key already exists. |
abstract void |
putFloat(String key,
float value)
Adds a new preference to this node using the given key and
float
value or updates the value if a preference with the
given key already exists. |
abstract void |
putInt(String key,
int value)
Adds a new preference to this node using the given key and
int
value or updates the value if a preference with the
given key already exists. |
abstract void |
putLong(String key,
long value)
Adds a new preference to this node using the given key and
long
value or updates the value if a preference with the
given key already exists. |
abstract void |
remove(String key)
Removes the preference mapped to the given key from this node.
|
abstract void |
removeNode()
Removes this preference node with all its descendants.
|
abstract void |
removeNodeChangeListener(NodeChangeListener ncl)
Removes the given
NodeChangeListener instance from this node. |
abstract void |
removePreferenceChangeListener(PreferenceChangeListener pcl)
Removes the given
PreferenceChangeListener instance from this
node. |
static PreferencesFactory |
setPreferencesFactory(PreferencesFactory pf) |
abstract void |
sync()
Synchronizes the data of this preference node and its descendants with
the back-end preference store.
|
static Preferences |
systemNodeForPackage(Class<?> c)
Legacy code; do not use. On Android, the Preference nodes
corresponding to the "system" and "user" preferences are stored in sections
of the file system that are inaccessible to apps.
|
static Preferences |
systemRoot()
Legacy code; do not use. On Android, the Preference nodes
corresponding to the "system" and "user" preferences are stored in sections
of the file system that are inaccessible to apps.
|
abstract String |
toString()
Returns a string representation of this node.
|
static Preferences |
userNodeForPackage(Class<?> c)
Legacy code; do not use. On Android, the Preference nodes
corresponding to the "system" and "user" preferences are stored in sections
of the file system that are inaccessible to apps.
|
static Preferences |
userRoot()
Legacy code; do not use. On Android, the Preference nodes
corresponding to the "system" and "user" preferences are stored in sections
of the file system that are inaccessible to apps.
|
public static final int MAX_KEY_LENGTH
public static final int MAX_NAME_LENGTH
public static final int MAX_VALUE_LENGTH
protected Preferences()
public static PreferencesFactory setPreferencesFactory(PreferencesFactory pf)
public abstract String absolutePath()
public abstract String[] childrenNames() throws BackingStoreException
BackingStoreException - if backing store is unavailable or causes an operation
failure.IllegalStateException - if this node has been removed.public abstract void clear()
throws BackingStoreException
BackingStoreException - if backing store is unavailable or causes an operation
failure.IllegalStateException - if this node has been removed.public abstract void exportNode(OutputStream ostream) throws IOException, BackingStoreException
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.
ostream - the output stream to write the XML-formatted data to.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.public abstract void exportSubtree(OutputStream ostream) throws IOException, BackingStoreException
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.
ostream - the output stream to write the XML-formatted data to.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.public abstract void flush()
throws BackingStoreException
If this node has been removed, the invocation of this method only flushes this node, not its descendants.
BackingStoreException - if the backing store is unavailable or causes an operation
failure.public abstract String get(String key, String deflt)
String value 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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract boolean getBoolean(String key, boolean deflt)
boolean 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 invalid.
The only valid values are the String "true", which represents
true and "false", which represents false, 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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract byte[] getByteArray(String key, byte[] deflt)
byte array 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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract double getDouble(String key, double deflt)
double 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 a string that can be converted to a
double by Double.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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract float getFloat(String key, float deflt)
float 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 a string that can be converted to a
float by Float.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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract int getInt(String key, int deflt)
int 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 a string that can be converted to
an int by Integer.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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public abstract long getLong(String key, long deflt)
long 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 a string that can be converted to a
long by Long.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.
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.IllegalStateException - if this node has been removed.NullPointerException - if the parameter key is null.public static void importPreferences(InputStream istream) throws InvalidPreferencesFormatException, IOException
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.
istream - the input stream to read the data from.InvalidPreferencesFormatException - if the data read from the given input stream is not from a
valid XML document.IOException - if an error occurs while importing.public abstract boolean isUserNode()
true, if this is a user preference node, false if
this is a system preference node.public abstract String[] keys() throws BackingStoreException
BackingStoreException - if the backing store is unavailable or causes an operation
failure.IllegalStateException - if this node has been removed.public abstract String name()
public abstract Preferences node(String path)
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.
path - the path name of the requested preference node.IllegalStateException - if this node has been removed.IllegalArgumentException - if the path name is invalid.NullPointerException - if the given path is null.public abstract boolean nodeExists(String path) throws BackingStoreException
Please note that if this node has been removed, an invocation of this
node will throw an IllegalStateException unless the given path is
an empty string, which will return false.
path - the path name of the preference node to query.true, if the queried preference node exists, false
otherwise.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 is null.BackingStoreException - if the backing store is unavailable or causes an operation
failure.public abstract Preferences parent()
null if this
node is the root node.IllegalStateException - if this node has been removed.public abstract void put(String key, String value)
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key or value is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH or the value's length is bigger than MAX_VALUE_LENGTH.IllegalStateException - if this node has been removed.public abstract void putBoolean(String key, boolean value)
boolean value to this node using the
given key and value or updates the value if a preference with the given
key already exists.key - the preference key to be added or updated.value - the preference boolean value for the given key.NullPointerException - if the given key is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void putByteArray(String key, byte[] value)
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.
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key or value is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH or value's length is bigger than three
quarters of MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void putDouble(String key, double value)
double
value 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).
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void putFloat(String key, float value)
float
value 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).
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void putInt(String key, int value)
int
value 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).
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void putLong(String key, long value)
long
value 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).
key - the preference key to be added or updated.value - the preference value for the given key.NullPointerException - if the given key is null.IllegalArgumentException - if the given key's length is bigger than MAX_KEY_LENGTH.IllegalStateException - if this node has been removed.public abstract void remove(String key)
key - the key of the preference to be removed.NullPointerException - if the given key is null.IllegalStateException - if this node has been removed.public abstract void removeNode()
throws BackingStoreException
flush() is invoked.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.public abstract void addNodeChangeListener(NodeChangeListener ncl)
NodeChangeListener instance for this node, which will
handle NodeChangeEvents. NodeChangeEvents will be fired
when a child node has been added to or removed from this node.ncl - the listener to be registered.NullPointerException - if the given listener is null.IllegalStateException - if this node has been removed.public abstract void addPreferenceChangeListener(PreferenceChangeListener pcl)
PreferenceChangeListener instance for this node,
which will handle PreferenceChangeEvents. PreferenceChangeEvents will be fired when a preference has been added
to, removed from, or updated for this node.pcl - the listener to be registered.NullPointerException - if the given listener is null.IllegalStateException - if this node has been removed.public abstract void removeNodeChangeListener(NodeChangeListener ncl)
NodeChangeListener instance from this node.ncl - the listener to be removed.IllegalArgumentException - if the given listener is null.IllegalStateException - if this node has been removed.public abstract void removePreferenceChangeListener(PreferenceChangeListener pcl)
PreferenceChangeListener instance from this
node.pcl - the listener to be removed.IllegalArgumentException - if the given listener is null.IllegalStateException - if this node has been removed.public abstract void sync()
throws BackingStoreException
BackingStoreException - if the backing store is unavailable or causes an operation
failure.IllegalStateException - if this node has been removed.public static Preferences systemNodeForPackage(Class<?> c)
flush() is invoked.c - the given class.NullPointerException - if the given class is null.public static Preferences systemRoot()
public static Preferences userNodeForPackage(Class<?> c)
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 method flush() is invoked.
NullPointerException - if the given class is null.public static Preferences userRoot()