APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.prefs

public abstract Class AbstractPreferences

extends Preferences
Class Inheritance
Known Direct Subclasses
java.util.prefs.FileSystemPreferences
Imports
java.util.*, java.io.*

This class provides a skeletal implementation of the 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.

Author
Josh Bloch
Since
1.4
See Also
Preferences

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
AbstractPreferences.EventDispatchThread

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

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

Field Summary

Modifier and TypeField 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<EventObject>
eventQueue

Queue of pending notification events.

private Map<String, AbstractPreferences>
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 true if this node did not exist in the backing store prior to the creation of this object.

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.

Inherited from java.util.prefs.Preferences:
MAX_KEY_LENGTHMAX_NAME_LENGTHMAX_VALUE_LENGTH

Constructor Summary

AccessConstructor 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 "" if this is the root.
name
)

Creates a preference node with the specified parent and the specified name relative to its parent.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

this preference node's absolute path name.
absolutePath
()

Implements abstract java.util.prefs.Preferences.absolutePath.

Implements the absolutePath method as per the specification in Preferences#absolutePath().

public void
addNodeChangeListener(NodeChangeListener ncl)

Implements abstract java.util.prefs.Preferences.addNodeChangeListener.

Registers the specified listener to receive node change events for this node.

public void
addPreferenceChangeListener(PreferenceChangeListener pcl)

Implements abstract java.util.prefs.Preferences.addPreferenceChangeListener.

Registers the specified listener to receive preference change events for this preference node.

protected final AbstractPreferences[]

Returns:

all known unremoved children of this node.
cachedChildren
()

Returns all known unremoved children of this node.

public String[]

Returns:

the names of the children of this preference node.
childrenNames
()

Implements abstract java.util.prefs.Preferences.childrenNames.

Implements the children method as per the specification in Preferences#childrenNames().

protected abstract String[]

Returns:

an array containing the names of the children of this preference node.
childrenNamesSpi
()

Returns the names of the children of this preference node.

protected abstract AbstractPreferences

Returns:

The named child node.
childSpi
(String
The name of the child node to return, relative to this preference node.
name
)

Returns the named child of this preference node, creating it if it does not already exist.

public void
clear()

Implements abstract java.util.prefs.Preferences.clear.

Implements the clear method as per the specification in Preferences#clear().

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.util.prefs.Preferences.exportNode.

Implements the exportNode method as per the specification in Preferences#exportNode(OutputStream).

public void
exportSubtree(OutputStream
the output stream on which to emit the XML document.
os
)

Implements abstract java.util.prefs.Preferences.exportSubtree.

Implements the exportSubtree method as per the specification in Preferences#exportSubtree(OutputStream).

public void
flush()

Implements abstract java.util.prefs.Preferences.flush.

Implements the flush method as per the specification in Preferences#flush().

private void
protected abstract void
flushSpi()

This method is invoked with this node locked.

public String

Returns:

the value associated with key, or def if no value is associated with key.
get
(String
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 key.
def
)

Implements abstract java.util.prefs.Preferences.get.

Implements the get method as per the specification in Preferences#get(String, String).

public boolean

Returns:

the boolean value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as a boolean.
getBoolean
(String
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 key or the associated value cannot be interpreted as a boolean.
def
)

Implements abstract java.util.prefs.Preferences.getBoolean.

Implements the getBoolean method as per the specification in Preferences#getBoolean(String, boolean).

public byte[]

Returns:

the byte array value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as a byte array.
getByteArray
(String
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 key or the associated value cannot be interpreted as a byte array.
def
)

Implements abstract java.util.prefs.Preferences.getByteArray.

Implements the getByteArray method as per the specification in Preferences#getByteArray(String, byte[]).

protected AbstractPreferences

Returns:

the named child if it exists, or null if it does not.
getChild
(String
name of the child to be searched for.
nodeName
)

Returns the named child if it exists, or null if it does not.

public double

Returns:

the double value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as a double.
getDouble
(String
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 key or the associated value cannot be interpreted as a double.
def
)

Implements abstract java.util.prefs.Preferences.getDouble.

Implements the getDouble method as per the specification in Preferences#getDouble(String, double).

public float

Returns:

the float value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as a float.
getFloat
(String
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 key or the associated value cannot be interpreted as a float.
def
)

Implements abstract java.util.prefs.Preferences.getFloat.

Implements the getFloat method as per the specification in Preferences#getFloat(String, float).

public int

Returns:

the int value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as an int.
getInt
(String
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 key or the associated value cannot be interpreted as an int.
def
)

Implements abstract java.util.prefs.Preferences.getInt.

Implements the getInt method as per the specification in Preferences#getInt(String, int).

public long

Returns:

the long value represented by the string associated with key in this preference node, or def if the associated value does not exist or cannot be interpreted as a long.
getLong
(String
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 key or the associated value cannot be interpreted as a long.
def
)

Implements abstract java.util.prefs.Preferences.getLong.

Implements the getLong method as per the specification in Preferences#getLong(String, long).

protected abstract String

Returns:

the value associated with the specified key at this preference node, or null if there is no association for this key, or the association cannot be determined at this time.
getSpi
(String
the key
key
)

Return the value associated with the specified key at this preference node, or null if there is no association for this key, or the association cannot be determined at this time.

protected boolean

Returns:

true iff this node (or an ancestor) has been removed with the removeNode() method.
isRemoved
()

Returns true iff this node (or an ancestor) has been removed with the removeNode() method.

public boolean

Returns:

true if this preference node is in the user preference tree, false if it's in the system preference tree.
isUserNode
()

Implements abstract java.util.prefs.Preferences.isUserNode.

Implements the isUserNode method as per the specification in Preferences#isUserNode().

public String[]

Returns:

an array of the keys that have an associated value in this preference node.
keys
()

Implements abstract java.util.prefs.Preferences.keys.

Implements the keys method as per the specification in Preferences#keys().

protected abstract String[]

Returns:

an array of the keys that have an associated value in this preference node.
keysSpi
()

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.
name
()

Implements abstract java.util.prefs.Preferences.name.

Implements the name method as per the specification in Preferences#name().

public Preferences

Returns:

the specified preference node.
node
(String
the path name of the preference node to return.
path
)

Implements abstract java.util.prefs.Preferences.node.

Implements the node method as per the specification in Preferences#node(String).

private Preferences
node(StringTokenizer path)

tokenizer contains <name> {'/' <name>}*

public boolean

Returns:

true if the specified node exists.
nodeExists
(String
the path name of the node whose existence is to be checked.
path
)

Implements abstract java.util.prefs.Preferences.nodeExists.

Implements the nodeExists method as per the specification in Preferences#nodeExists(String).

private boolean
nodeExists(StringTokenizer path)

tokenizer contains <name> {'/' <name>}*

pack-priv NodeChangeListener[]
public Preferences

Returns:

the parent of this preference node.
parent
()

Implements abstract java.util.prefs.Preferences.parent.

Implements the parent method as per the specification in Preferences#parent().

pack-priv PreferenceChangeListener[]
prefListeners()

Return this node's preference/node change listeners.

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.util.prefs.Preferences.put.

Implements the put method as per the specification in Preferences#put(String, String).

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.util.prefs.Preferences.putBoolean.

Implements the putBoolean method as per the specification in Preferences#putBoolean(String, boolean).

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.util.prefs.Preferences.putByteArray.

Implements the putByteArray method as per the specification in Preferences#putByteArray(String, byte[]).

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.util.prefs.Preferences.putDouble.

Implements the putDouble method as per the specification in Preferences#putDouble(String, double).

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.util.prefs.Preferences.putFloat.

Implements the putFloat method as per the specification in Preferences#putFloat(String, float).

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.util.prefs.Preferences.putInt.

Implements the putInt method as per the specification in Preferences#putInt(String, int).

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.util.prefs.Preferences.putLong.

Implements the putLong method as per the specification in Preferences#putLong(String, long).

protected abstract void
putSpi(String
the key
key
,
String
the value
value
)

Put the given key-value association into this preference node.

public void
remove(String
key whose mapping is to be removed from the preference node.
key
)

Implements abstract java.util.prefs.Preferences.remove.

Implements the remove(String) method as per the specification in Preferences#remove(String).

public void
removeNode()

Implements abstract java.util.prefs.Preferences.removeNode.

Implements the removeNode() method as per the specification in Preferences#removeNode().

private void
public void
removeNodeChangeListener(NodeChangeListener ncl)

Implements abstract java.util.prefs.Preferences.removeNodeChangeListener.

Removes the specified NodeChangeListener, so it no longer receives change events.

protected abstract void
removeNodeSpi()

Removes this preference node, invalidating it and any preferences that it contains.

public void
removePreferenceChangeListener(PreferenceChangeListener pcl)

Implements abstract java.util.prefs.Preferences.removePreferenceChangeListener.

Removes the specified preference change listener, so it no longer receives preference change events.

protected abstract void
removeSpi(String
the key
key
)

Remove the association (if any) for the specified key at this preference node.

private static synchronized void
startEventDispatchThreadIfNecessary()

This method starts the event dispatch thread the first time it is called.

public void
sync()

Implements abstract java.util.prefs.Preferences.sync.

Implements the sync method as per the specification in Preferences#sync().

private void
protected abstract void
syncSpi()

This method is invoked with this node locked.

public String
toString()

Implements abstract java.util.prefs.Preferences.toString.

Returns the absolute path name of this preferences node.

Inherited from java.util.prefs.Preferences:
importPreferencessystemNodeForPackagesystemRootuserNodeForPackageuserRoot