Preferences
class, greatly easing the task of implementing it.
This class is for Preferences implementers only.
Normal users of the Preferences facility should have no need to
consult this documentation. The Preferences documentation
should suffice.
Implementors must override the nine abstract service-provider interface
(SPI) methods: getSpi(String), putSpi(String, String),
removeSpi(String), childSpi(String), removeNodeSpi(), keysSpi(), childrenNamesSpi(), syncSpi() and flushSpi(). All of the concrete methods specify
precisely how they are implemented atop these SPI methods. The implementor
may, at his discretion, override one or more of the concrete methods if the
default implementation is unsatisfactory for any reason, such as
performance.
The SPI methods fall into three groups concerning exception
behavior. The getSpi method should never throw exceptions, but it
doesn't really matter, as any exception thrown by this method will be
intercepted by get(String, String), which will return the specified
default value to the caller. The removeNodeSpi, keysSpi,
childrenNamesSpi, syncSpi and flushSpi methods are specified
to throw BackingStoreException, and the implementation is required
to throw this checked exception if it is unable to perform the operation.
The exception propagates outward, causing the corresponding API method
to fail.
The remaining SPI methods putSpi(String, String), removeSpi(String) and childSpi(String) have more complicated
exception behavior. They are not specified to throw
BackingStoreException, as they can generally obey their contracts
even if the backing store is unavailable. This is true because they return
no information and their effects are not required to become permanent until
a subsequent call to Preferences#flush() or
Preferences#sync(). Generally speaking, these SPI methods should not
throw exceptions. In some implementations, there may be circumstances
under which these calls cannot even enqueue the requested operation for
later processing. Even under these circumstances it is generally better to
simply ignore the invocation and return, rather than throwing an
exception. Under these circumstances, however, subsequently invoking
flush() or sync would not imply that all previous
operations had successfully been made permanent.
There is one circumstance under which putSpi, removeSpi and
childSpi should throw an exception: if the caller lacks
sufficient privileges on the underlying operating system to perform the
requested operation. This will, for instance, occur on most systems
if a non-privileged user attempts to modify system preferences.
(The required privileges will vary from implementation to
implementation. On some implementations, they are the right to modify the
contents of some directory in the file system; on others they are the right
to modify contents of some key in a registry.) Under any of these
circumstances, it would generally be undesirable to let the program
continue executing as if these operations would become permanent at a later
time. While implementations are not required to throw an exception under
these circumstances, they are encouraged to do so. A SecurityException would be appropriate.
Most of the SPI methods require the implementation to read or write information at a preferences node. The implementor should beware of the fact that another VM may have concurrently deleted this node from the backing store. It is the implementation's responsibility to recreate the node if it has been deleted.
Implementation Note
In Sun's default Preferences
implementations, the user's identity is inherited from the underlying
operating system and does not change for the lifetime of the virtual
machine. It is recognized that server-side Preferences
implementations may have the user identity change from request to request,
implicitly passed to Preferences methods via the use of a
static ThreadLocal instance. Authors of such implementations are
strongly encouraged to determine the user at the time preferences
are accessed (for example by the get(String, String) or put(String, String) method) rather than permanently associating a user
with each Preferences instance. The latter behavior conflicts
with normal Preferences usage and would lead to great confusion.
Preferences
| Modifier and Type | Class and Description |
|---|---|
| private static class | AbstractPreferences.
A single background thread ("the event notification thread") monitors the event queue and delivers events that are placed on the queue. |
| private static class | AbstractPreferences.
These two classes are used to distinguish NodeChangeEvents on eventQueue so the event dispatch thread knows whether to call childAdded or childRemoved. |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final String | absolutePath
Our absolute path name. |
| pack-priv static final int | CODE_POINT_U0000
The code point U+0000, assigned to the null control character, is the only character encoded in Unicode and ISO/IEC 10646 that is always invalid in any XML 1.0 and 1.1 document. |
| private static final AbstractPreferences[] | |
| private static final String[] | |
| private static Thread | |
| private static final List | eventQueue
Queue of pending notification events. |
| private Map | kidCache
All known unremoved children of this node. |
| protected final Object | lock
An object whose monitor is used to lock this node. |
| private final String | name
Our name relative to parent. |
| protected boolean | newNode
This field should be |
| private NodeChangeListener[] | nodeListeners
Registered node change listeners. |
| pack-priv final AbstractPreferences | parent
Our parent node. |
| private PreferenceChangeListener[] | prefListeners
Registered preference change listeners. |
| private boolean | removed
This field is used to keep track of whether or not this node has been removed. |
| private final AbstractPreferences | root
Our root node. |
| Access | Constructor and Description |
|---|---|
| protected | AbstractPreferences(AbstractPreferences
the parent of this preference node, or null if this
is the root. parent, String the name of this preference node, relative to its parent,
or name)"" if this is the root.Creates a preference node with the specified parent and the specified name relative to its parent. |
| Modifier and Type | Method and Description |
|---|---|
| public String | Returns: this preference node's absolute path name.Implements abstract java. Implements the |
| public void | addNodeChangeListener(NodeChangeListener ncl)
Implements abstract java. Registers the specified listener to receive node change events for this node. |
| public void | addPreferenceChangeListener(PreferenceChangeListener pcl)
Implements abstract java. Registers the specified listener to receive preference change events for this preference node. |
| protected final AbstractPreferences[] | Returns: all known unremoved children of this node.Returns all known unremoved children of this node. |
| public String[] | Returns: the names of the children of this preference node.Implements abstract java. Implements the |
| protected abstract String[] | Returns: an array containing the names of the children of this preference node.Returns the names of the children of this preference node. |
| protected abstract AbstractPreferences | |
| public void | clear()
Implements abstract java. Implements the |
| private void | enqueueNodeAddedEvent(Preferences child)
Enqueue a "node added" event for delivery to registered node change listeners unless there are no registered listeners. |
| private void | enqueueNodeRemovedEvent(Preferences child)
Enqueue a "node removed" event for delivery to registered node change listeners unless there are no registered listeners. |
| private void | enqueuePreferenceChangeEvent(String key, String newValue)
Enqueue a preference change event for delivery to registered preference change listeners unless there are no registered listeners. |
| public void | exportNode(OutputStream
the output stream on which to emit the XML document. os)Implements abstract java. Implements the |
| public void | exportSubtree(OutputStream
the output stream on which to emit the XML document. os)Implements abstract java. Implements the |
| public void | flush()
Implements abstract java. Implements the |
| private void | |
| protected abstract void | |
| public String | Returns: the value associated withkey, or def
if no value is associated with key.key whose associated value is to be returned. key, String the value to be returned in the event that this
preference node has no value associated with def)key.Implements abstract java. Implements the |
| public boolean | Returns: the boolean value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
a boolean.key whose associated value is to be returned as a boolean. key, boolean the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as a boolean.Implements abstract java. Implements the |
| public byte[] | Returns: the byte array value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
a byte array.key whose associated value is to be returned as a byte array. key, byte[] the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as a byte array.Implements abstract java. Implements the |
| protected AbstractPreferences | |
| public double | Returns: the double value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
a double.key whose associated value is to be returned as a double. key, double the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as a double.Implements abstract java. Implements the |
| public float | Returns: the float value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
a float.key whose associated value is to be returned as a float. key, float the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as a float.Implements abstract java. Implements the |
| public int | Returns: the int value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
an int.key whose associated value is to be returned as an int. key, int the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as an int.Implements abstract java. Implements the |
| public long | Returns: the long value represented by the string associated withkey in this preference node, or def if the
associated value does not exist or cannot be interpreted as
a long.key whose associated value is to be returned as a long. key, long the value to be returned in the event that this
preference node has no value associated with def)key
or the associated value cannot be interpreted as a long.Implements abstract java. Implements the |
| protected abstract String | Returns: the value associated with the specified key at this preference node, ornull if there is no association for this
key, or the association cannot be determined at this time.the key key)Return the value associated with the specified key at this preference
node, or |
| protected boolean | isRemoved()
Returns |
| public boolean | Returns: true if this preference node is in the user
preference tree, false if it's in the system
preference tree.Implements abstract java. Implements the |
| public String[] | Returns: an array of the keys that have an associated value in this preference node.Implements abstract java. Implements the |
| protected abstract String[] | Returns: an array of the keys that have an associated value in this preference node.Returns all of the keys that have an associated value in this preference node. |
| public String | Returns: this preference node's name, relative to its parent.Implements abstract java. Implements the |
| public Preferences | Returns: the specified preference node.the path name of the preference node to return. path)Implements abstract java. Implements the |
| private Preferences | |
| public boolean | Returns: true if the specified node exists.the path name of the node whose existence is to be checked. path)Implements abstract java. Implements the |
| private boolean | |
| pack-priv NodeChangeListener[] | |
| public Preferences | Returns: the parent of this preference node.Implements abstract java. Implements the |
| pack-priv PreferenceChangeListener[] | |
| public void | put(String
key with which the specified value is to be associated. key, String value to be associated with the specified key. value)Implements abstract java. Implements the |
| public void | putBoolean(String
key with which the string form of value is to be associated. key, boolean value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| public void | putByteArray(String
key with which the string form of value is to be associated. key, byte[] value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| public void | putDouble(String
key with which the string form of value is to be associated. key, double value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| public void | putFloat(String
key with which the string form of value is to be associated. key, float value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| public void | putInt(String
key with which the string form of value is to be associated. key, int value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| public void | putLong(String
key with which the string form of value is to be associated. key, long value whose string form is to be associated with key. value)Implements abstract java. Implements the |
| protected abstract void | |
| public void | remove(String
key whose mapping is to be removed from the preference node. key)Implements abstract java. Implements the |
| public void | removeNode()
Implements abstract java. Implements the |
| private void | |
| public void | removeNodeChangeListener(NodeChangeListener ncl)
Implements abstract java. Removes the specified |
| protected abstract void | |
| public void | removePreferenceChangeListener(PreferenceChangeListener pcl)
Implements abstract java. Removes the specified preference change listener, so it no longer receives preference change events. |
| protected abstract void | |
| private static synchronized void | startEventDispatchThreadIfNecessary()
This method starts the event dispatch thread the first time it is called. |
| public void | sync()
Implements abstract java. Implements the |
| private void | |
| protected abstract void | |
| public String | toString()
Implements abstract java. Returns the absolute path name of this preferences node. |