Class AbstractPreferences
public abstract class AbstractPreferences extends Preferences
Preferences provider's
implementation. This class defines nine abstract SPI methods, which must be
implemented by a preference provider.- Since:
- 1.4
- See Also:
Preferences
-
Field Summary
Fields Modifier and Type Field Description protected ObjectlockThe object used to lock this node.protected booleannewNodeThis field is true if this node is created while it doesn't exist in the backing store.Fields inherited from class java.util.prefs.Preferences
MAX_KEY_LENGTH, MAX_NAME_LENGTH, MAX_VALUE_LENGTH -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractPreferences(AbstractPreferences parent, String name)Constructs a newAbstractPreferencesinstance using the given parent node and node name. -
Method Summary
Modifier and Type Method Description StringabsolutePath()Gets the absolute path string of this preference node.voidaddNodeChangeListener(NodeChangeListener ncl)Registers aNodeChangeListenerinstance for this node, which will handleNodeChangeEvents.voidaddPreferenceChangeListener(PreferenceChangeListener pcl)Registers aPreferenceChangeListenerinstance for this node, which will handlePreferenceChangeEvents.protected AbstractPreferences[]cachedChildren()Returns an array of all cached child nodes.String[]childrenNames()Returns the names of all children of this node or an empty array if this node has no children.protected abstract String[]childrenNamesSpi()Returns the names of all of the child nodes of this node or an empty array if this node has no children.protected abstract AbstractPreferenceschildSpi(String name)Returns the child preference node with the given name, creating it if it does not exist.voidclear()Removes all preferences of this node.voidexportNode(OutputStream ostream)Exports all of the preferences of this node to a XML document using the given output stream.voidexportSubtree(OutputStream ostream)Exports all of the preferences of this node and all its descendants to a XML document using the given output stream.voidflush()Forces all pending updates to this node and its descendants to be persisted in the backing store.protected abstract voidflushSpi()Flushes changes of this node to the backing store.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.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.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.protected AbstractPreferencesgetChild(String name)Returns the child node with the specified name ornullif it doesn't exist.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.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.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.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.protected abstract StringgetSpi(String key)Gets the preference value mapped to the given key.protected booleanisRemoved()Returns whether this node has been removed by invoking the methodremoveNode().booleanisUserNode()Returns whether this is a user preference node.String[]keys()Returns all preference keys stored in this node or an empty array if no key was found.protected abstract String[]keysSpi()Returns an array of all preference keys of this node or an empty array if no preferences have been found.Stringname()Returns the name of this node.Preferencesnode(String name)Returns the preference node with the given path name.booleannodeExists(String name)Returns whether the preference node with the given path name exists.Preferencesparent()Returns the parent preference node of this node ornullif this node is the root node.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.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.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.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.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.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.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.protected abstract voidputSpi(String name, String value)Puts the given key-value pair into this node.voidremove(String key)Removes the preference mapped to the given key from this node.voidremoveNode()Removes this preference node with all its descendants.voidremoveNodeChangeListener(NodeChangeListener ncl)Removes the givenNodeChangeListenerinstance from this node.protected abstract voidremoveNodeSpi()Removes this node from the preference hierarchy tree.voidremovePreferenceChangeListener(PreferenceChangeListener pcl)Removes the givenPreferenceChangeListenerinstance from this node.protected abstract voidremoveSpi(String key)Removes the preference with the specified key.voidsync()Synchronizes the data of this preference node and its descendants with the back-end preference store.protected abstract voidsyncSpi()Synchronizes this node with the backing store.StringtoString()Returns a string representation of this node.Methods inherited from class java.util.prefs.Preferences
importPreferences, systemNodeForPackage, systemRoot, userNodeForPackage, userRoot
-
Field Details
-
lock
The object used to lock this node. -
newNode
protected boolean newNodeThis field is true if this node is created while it doesn't exist in the backing store. This field's default value is false, and it is checked when the node creation is completed, and if it is true, the node change event will be fired for this node's parent.
-
-
Constructor Details
-
AbstractPreferences
Constructs a newAbstractPreferencesinstance using the given parent node and node name.- Parameters:
parent- the parent node of the new node ornullto indicate that the new node is a root node.name- the name of the new node or an empty string to indicate that this node is called "root".- Throws:
IllegalArgumentException- if the name contains a slash character or is empty ifparentis notnull.
-
-
Method Details
-
cachedChildren
Returns an array of all cached child nodes.- Returns:
- the array of cached child nodes.
-
getChild
Returns the child node with the specified name ornullif it doesn't exist. Implementers can assume that the name supplied to this method will be a valid node name string (conforming to the node naming format) and will not correspond to a node that has been cached or removed.- Parameters:
name- the name of the desired child node.- Returns:
- the child node with the given name or
nullif it doesn't exist. - Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
isRemoved
protected boolean isRemoved()Returns whether this node has been removed by invoking the methodremoveNode().- Returns:
true, if this node has been removed,falseotherwise.
-
flushSpi
Flushes changes of this node to the backing store. This method should only flush this node and should not include the descendant nodes. Any implementation that wants to provide functionality to flush all nodes at once should override the methodflush().- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
childrenNamesSpi
Returns the names of all of the child nodes of this node or an empty array if this node has no children. The names of cached children are not required to be returned.- Returns:
- the names of this node's children.
- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
childSpi
Returns the child preference node with the given name, creating it if it does not exist. The caller of this method should ensure that the given name is valid and that this node has not been removed or cached. If the named node has just been removed, the implementation of this method must create a new one instead of reactivating the removed one.The new creation is not required to be persisted immediately until the flush method will be invoked.
- Parameters:
name- the name of the child preference to be returned.- Returns:
- the child preference node.
-
putSpi
Puts the given key-value pair into this node. Caller of this method should ensure that both of the given values are valid and that this node has not been removed.- Parameters:
name- the given preference key.value- the given preference value.
-
getSpi
Gets the preference value mapped to the given key. The caller of this method should ensure that the given key is valid and that this node has not been removed. This method should not throw any exceptions but if it does, the caller will ignore the exception, regarding it as anullreturn value.- Parameters:
key- the given key to be searched for.- Returns:
- the preference value mapped to the given key.
-
keysSpi
Returns an array of all preference keys of this node or an empty array if no preferences have been found. The caller of this method should ensure that this node has not been removed.- Returns:
- the array of all preference keys.
- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
removeNodeSpi
Removes this node from the preference hierarchy tree. The caller of this method should ensure that this node has no child nodes, which means the methodPreferences.removeNode()should invoke this method multiple-times in bottom-up pattern. The removal is not required to be persisted until after it is flushed.- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
removeSpi
Removes the preference with the specified key. The caller of this method should ensure that the given key is valid and that this node has not been removed.- Parameters:
key- the key of the preference that is to be removed.
-
syncSpi
Synchronizes this node with the backing store. This method should only synchronize this node and should not include the descendant nodes. An implementation that wants to provide functionality to synchronize all nodes at once should override the methodsync().- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
absolutePath
Description copied from class:PreferencesGets the absolute path string of this preference node.- Specified by:
absolutePathin classPreferences- Returns:
- the preference node's absolute path string.
-
childrenNames
Description copied from class:PreferencesReturns the names of all children of this node or an empty array if this node has no children.- Specified by:
childrenNamesin classPreferences- Returns:
- the names of all children of this node.
- Throws:
BackingStoreException- if backing store is unavailable or causes an operation failure.
-
clear
Description copied from class:PreferencesRemoves all preferences of this node.- Specified by:
clearin classPreferences- Throws:
BackingStoreException- if backing store is unavailable or causes an operation failure.
-
exportNode
Description copied from class:PreferencesExports 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.- Specified by:
exportNodein classPreferences- 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.
-
exportSubtree
Description copied from class:PreferencesExports 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.- Specified by:
exportSubtreein classPreferences- 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.
-
flush
Description copied from class:PreferencesForces 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.
- Specified by:
flushin classPreferences- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
get
Description copied from class:PreferencesGets 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.
- Specified by:
getin classPreferences- 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.
-
getBoolean
Description copied from class:PreferencesGets 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.
- Specified by:
getBooleanin classPreferences- 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.
-
getByteArray
Description copied from class:PreferencesGets 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.
- Specified by:
getByteArrayin classPreferences- 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.
-
getDouble
Description copied from class:PreferencesGets 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.
- Specified by:
getDoublein classPreferences- 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.
-
getFloat
Description copied from class:PreferencesGets 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.
- Specified by:
getFloatin classPreferences- 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.
-
getInt
Description copied from class:PreferencesGets 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.
- Specified by:
getIntin classPreferences- 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.
-
getLong
Description copied from class:PreferencesGets 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.
- Specified by:
getLongin classPreferences- 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.
-
isUserNode
public boolean isUserNode()Description copied from class:PreferencesReturns whether this is a user preference node.- Specified by:
isUserNodein classPreferences- Returns:
true, if this is a user preference node,falseif this is a system preference node.
-
keys
Description copied from class:PreferencesReturns all preference keys stored in this node or an empty array if no key was found.- Specified by:
keysin classPreferences- Returns:
- the list of all preference keys of this node.
- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
name
Description copied from class:PreferencesReturns the name of this node.- Specified by:
namein classPreferences- Returns:
- the name of this node.
-
node
Description copied from class:PreferencesReturns 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.
- Specified by:
nodein classPreferences- Parameters:
name- the path name of the requested preference node.- Returns:
- the requested preference node.
-
nodeExists
Description copied from class:PreferencesReturns 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.- Specified by:
nodeExistsin classPreferences- Parameters:
name- the path name of the preference node to query.- Returns:
true, if the queried preference node exists,falseotherwise.- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
parent
Description copied from class:PreferencesReturns the parent preference node of this node ornullif this node is the root node.- Specified by:
parentin classPreferences- Returns:
- the parent preference node of this node.
-
put
Description copied from class:PreferencesAdds 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.- Specified by:
putin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
putBoolean
Description copied from class:PreferencesAdds 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.- Specified by:
putBooleanin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preferencebooleanvalue for the given key.
-
putByteArray
Description copied from class:PreferencesAdds 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.
- Specified by:
putByteArrayin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
putDouble
Description copied from class:PreferencesAdds 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).- Specified by:
putDoublein classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
putFloat
Description copied from class:PreferencesAdds 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).- Specified by:
putFloatin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
putInt
Description copied from class:PreferencesAdds 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).- Specified by:
putIntin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
putLong
Description copied from class:PreferencesAdds 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).- Specified by:
putLongin classPreferences- Parameters:
key- the preference key to be added or updated.value- the preference value for the given key.
-
remove
Description copied from class:PreferencesRemoves the preference mapped to the given key from this node.- Specified by:
removein classPreferences- Parameters:
key- the key of the preference to be removed.
-
removeNode
Description copied from class:PreferencesRemoves this preference node with all its descendants. The removal won't necessarily be persisted until the methodflush()is invoked.- Specified by:
removeNodein classPreferences- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
addNodeChangeListener
Description copied from class:PreferencesRegisters aNodeChangeListenerinstance for this node, which will handleNodeChangeEvents.NodeChangeEvents will be fired when a child node has been added to or removed from this node.- Specified by:
addNodeChangeListenerin classPreferences- Parameters:
ncl- the listener to be registered.
-
addPreferenceChangeListener
Description copied from class:PreferencesRegisters 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.- Specified by:
addPreferenceChangeListenerin classPreferences- Parameters:
pcl- the listener to be registered.
-
removeNodeChangeListener
Description copied from class:PreferencesRemoves the givenNodeChangeListenerinstance from this node.- Specified by:
removeNodeChangeListenerin classPreferences- Parameters:
ncl- the listener to be removed.
-
removePreferenceChangeListener
Description copied from class:PreferencesRemoves the givenPreferenceChangeListenerinstance from this node.- Specified by:
removePreferenceChangeListenerin classPreferences- Parameters:
pcl- the listener to be removed.
-
sync
Description copied from class:PreferencesSynchronizes 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.- Specified by:
syncin classPreferences- Throws:
BackingStoreException- if the backing store is unavailable or causes an operation failure.
-
toString
Description copied from class:PreferencesReturns a string representation of this node. The format is "User/System Preference Node: " followed by this node's absolute path.- Specified by:
toStringin classPreferences- Returns:
- the string representation of this node.
-