Class DefaultPreferences
- All Implemented Interfaces:
PersistedPreferences
PersistedPreferences node.- See Also:
-
Field Summary
Fields inherited from class Preferences
MAX_KEY_LENGTH, MAX_NAME_LENGTH, MAX_VALUE_LENGTH -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns this preference node's absolute path name.voidRegisters the specified listener to receive node change events for this node.voidRegisters the specified listener to receive preference change events for this preference node.String[]Returns the names of the children of this preference node, relative to this node.voidclear()Removes all the preferences (key-value associations) in this preference node.protected StringconvertPathName(String pathName) Converts the given pathname if absolute.
For pathname starting with a slash the optional name of the root node will be prepended.voidvoidvoidflush()Forces any changes in the contents of this preference node and its descendants to the persistent store.Returns the value associated with the specified key in this preference node.booleangetBoolean(String key, boolean def) Returns the boolean value represented by the string associated with the specified key in this preference node.byte[]getByteArray(String key, byte[] def) Returns the byte array value represented by the string associated with the specified key in this preference node.doubleReturns the double value represented by the string associated with the specified key in this preference node.floatReturns the float value represented by the string associated with the specified key in this preference node.intReturns the int value represented by the string associated with the specified key in this preference node.longReturns the long value represented by the string associated with the specified key in this preference node.booleanReturnstrueif this preference node is in the user preference tree,falseif it's in the system preference tree.String[]keys()Returns all the keys that have an associated value in this preference node.name()Returns this preference node's name, relative to its parent.Returns the named preference node in the same tree as this node, creating it and any of its ancestors if they do not already exist.booleannodeExists(String pathName) Returns true if the named preference node exists in the same tree as this node.parent()Returns the parent of this preference node, ornullif this is the root.voidAssociates the specified value with the specified key in this preference node.voidputBoolean(String key, boolean value) Associates a string representing the specified boolean value with the specified key in this preference node.voidputByteArray(String key, byte[] value) Associates a string representing the specified byte array with the specified key in this preference node.voidAssociates a string representing the specified double value with the specified key in this preference node.voidAssociates a string representing the specified float value with the specified key in this preference node.voidAssociates a string representing the specified int value with the specified key in this preference node.voidAssociates a string representing the specified long value with the specified key in this preference node.voidRemoves the value associated with the specified key in this preference node, if any.voidRemoves this preference node and all of its descendants, invalidating any preferences contained in the removed nodes.voidRemoves the specifiedNodeChangeListener, so it no longer receives change events.voidRemoves the specified preference change listener, so it no longer receives preference change events.voidsync()Ensures that future reads from this preference node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to thesyncinvocation.toString()Methods inherited from class Preferences
importPreferences, systemNodeForPackage, systemRoot, userNodeForPackage, userRoot
-
Constructor Details
-
DefaultPreferences
Creates a preferences node.- Parameters:
preferences- the java util preferences node to wrap
-
-
Method Details
-
put
Description copied from interface:PersistedPreferencesAssociates the specified value with the specified key in this preference node.- Specified by:
putin interfacePersistedPreferences- Specified by:
putin classPreferences- Parameters:
key- key with which the specified value is to be associated.value- value to be associated with the specified key.
-
get
Description copied from interface:PersistedPreferencesReturns the value associated with the specified key in this preference node. Returns the specified default if there is no value associated with the key, or the backing store is inaccessible.Some implementations may store default values in their backing stores. If there is no value associated with the specified key but there is such a stored default, it is returned in preference to the specified default.
- Specified by:
getin interfacePersistedPreferences- Specified by:
getin classPreferences- Parameters:
key- key whose associated value is to be returned.def- the value to be returned in the event that this preference node has no value associated withkey.- Returns:
- the value associated with
key, ordefif no value is associated withkey, or the backing store is inaccessible.
-
remove
Description copied from interface:PersistedPreferencesRemoves the value associated with the specified key in this preference node, if any.If this implementation supports stored defaults, and there is such a default for the specified preference, the stored default will be "exposed" by this call, in the sense that it will be returned by a succeeding call to
get.- Specified by:
removein interfacePersistedPreferences- Specified by:
removein classPreferences- Parameters:
key- key whose mapping is to be removed from the preference node.
-
clear
Description copied from interface:PersistedPreferencesRemoves all the preferences (key-value associations) in this preference node. This call has no effect on any descendants of this node.If this implementation supports stored defaults, and this node in the preference hierarchy contains any such defaults, the stored defaults will be "exposed" by this call, in the sense that they will be returned by succeeding calls to
get.- Specified by:
clearin interfacePersistedPreferences- Specified by:
clearin classPreferences- Throws:
BackingStoreException- if this operation cannot be completed due to failure in the backing store, or inability to communicate with it.- See Also:
-
putInt
Description copied from interface:PersistedPreferencesAssociates a string representing the specified int value with the specified key in this preference node. The associated string is the one that would be returned if the int value were passed toInteger.toString(int). This method is intended for use in conjunction withPersistedPreferences.getInt(String, int).- Specified by:
putIntin interfacePersistedPreferences- Specified by:
putIntin classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getInt
Description copied from interface:PersistedPreferencesReturns the int value represented by the string associated with the specified key in this preference node. The string is converted to an integer as byInteger.parseInt(String). Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or ifInteger.parseInt(String)would throw aNumberFormatExceptionif the associated value were passed. This method is intended for use in conjunction withPersistedPreferences.putInt(String, int).If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to an int with
Integer.parseInt, this int is returned in preference to the specified default.- Specified by:
getIntin interfacePersistedPreferences- Specified by:
getIntin classPreferences- Parameters:
key- key whose associated value is to be returned as an int.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as an int, or the backing store is inaccessible.- Returns:
- the int value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as an int. - See Also:
-
putLong
Description copied from interface:PersistedPreferencesAssociates a string representing the specified long value with the specified key in this preference node. The associated string is the one that would be returned if the long value were passed toLong.toString(long). This method is intended for use in conjunction withPersistedPreferences.getLong(String, long).- Specified by:
putLongin interfacePersistedPreferences- Specified by:
putLongin classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getLong
Description copied from interface:PersistedPreferencesReturns the long value represented by the string associated with the specified key in this preference node. The string is converted to a long as byLong.parseLong(String). Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or ifLong.parseLong(String)would throw aNumberFormatExceptionif the associated value were passed. This method is intended for use in conjunction withPersistedPreferences.putLong(String, long).If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a long with
Long.parseLong, this long is returned in preference to the specified default.- Specified by:
getLongin interfacePersistedPreferences- Specified by:
getLongin classPreferences- Parameters:
key- key whose associated value is to be returned as a long value.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as a long value, or the backing store is inaccessible.- Returns:
- the long value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as a long value. - See Also:
-
putBoolean
Description copied from interface:PersistedPreferencesAssociates a string representing the specified boolean value with the specified key in this preference node. The associated string is"true"if the value is true, and"false"if it is false. This method is intended for use in conjunction withPersistedPreferences.getBoolean(String, boolean).- Specified by:
putBooleanin interfacePersistedPreferences- Specified by:
putBooleanin classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getBoolean
Description copied from interface:PersistedPreferencesReturns the boolean value represented by the string associated with the specified key in this preference node. Valid strings are"true", which represents true, and"false", which represents false. Case is ignored, so, for example,"TRUE"and"False"are also valid. This method is intended for use in conjunction withPersistedPreferences.putBoolean(String, boolean).Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or if the associated value is something other than
"true"or"false", ignoring the case.If the implementation supports stored defaults and such a default exists and is accessible, it is used in preference to the specified default, unless the stored default is something other than
"true"or"false", ignoring the case, in which case the specified default is used.- Specified by:
getBooleanin interfacePersistedPreferences- Specified by:
getBooleanin classPreferences- Parameters:
key- key whose associated value is to be returned as a boolean.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as a boolean, or the backing store is inaccessible.- Returns:
- the boolean value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as a boolean. - See Also:
-
putFloat
Description copied from interface:PersistedPreferencesAssociates a string representing the specified float value with the specified key in this preference node. The associated string is the one that would be returned if the float value were passed toFloat.toString(float). This method is intended for use in conjunction withPersistedPreferences.getFloat(String, float).- Specified by:
putFloatin interfacePersistedPreferences- Specified by:
putFloatin classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getFloat
Description copied from interface:PersistedPreferencesReturns the float value represented by the string associated with the specified key in this preference node. The string is converted to an integer as byFloat.parseFloat(String). Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or ifFloat.parseFloat(String)would throw aNumberFormatExceptionif the associated value were passed. This method is intended for use in conjunction withPersistedPreferences.putFloat(String, float).If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a float with
Float.parseFloat, this float is returned in preference to the specified default.- Specified by:
getFloatin interfacePersistedPreferences- Specified by:
getFloatin classPreferences- Parameters:
key- key whose associated value is to be returned as a float.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as a float, or the backing store is inaccessible.- Returns:
- the float value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as a float. - See Also:
-
putDouble
Description copied from interface:PersistedPreferencesAssociates a string representing the specified double value with the specified key in this preference node. The associated string is the one that would be returned if the double value were passed toDouble.toString(double). This method is intended for use in conjunction withPersistedPreferences.getDouble(String, double).- Specified by:
putDoublein interfacePersistedPreferences- Specified by:
putDoublein classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getDouble
Description copied from interface:PersistedPreferencesReturns the double value represented by the string associated with the specified key in this preference node. The string is converted to an integer as byDouble.parseDouble(String). Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or ifDouble.parseDouble(String)would throw aNumberFormatExceptionif the associated value were passed. This method is intended for use in conjunction withPersistedPreferences.putDouble(String, double).If the implementation supports stored defaults and such a default exists, is accessible, and could be converted to a double with
Double.parseDouble, this double is returned in preference to the specified default.- Specified by:
getDoublein interfacePersistedPreferences- Specified by:
getDoublein classPreferences- Parameters:
key- key whose associated value is to be returned as a double.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as a double, or the backing store is inaccessible.- Returns:
- the double value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as a double. - See Also:
-
putByteArray
Description copied from interface:PersistedPreferencesAssociates a string representing the specified byte array with the specified key in this preference node. The associated string is the Base64 encoding of the byte array, as defined in RFC 2045, Section 6.8, with one minor change: the string will consist solely of characters from the Base64 Alphabet; it will not contain any newline characters. Note that the maximum length of the byte array is limited to three quarters ofMAX_VALUE_LENGTHso that the length of the Base64 encoded String does not exceedMAX_VALUE_LENGTH. This method is intended for use in conjunction withPersistedPreferences.getByteArray(String, byte[]).- Specified by:
putByteArrayin interfacePersistedPreferences- Specified by:
putByteArrayin classPreferences- Parameters:
key- key with which the string form of value is to be associated.value- value whose string form is to be associated with the key.- See Also:
-
getByteArray
Description copied from interface:PersistedPreferencesReturns the byte array value represented by the string associated with the specified key in this preference node. Valid strings are Base64 encoded binary data, as defined in RFC 2045, Section 6.8, with one minor change: the string must consist solely of characters from the Base64 Alphabet; no newline characters or extraneous characters are permitted. This method is intended for use in conjunction withPersistedPreferences.putByteArray(String, byte[]).Returns the specified default if there is no value associated with the key, the backing store is inaccessible, or if the associated value is not a valid Base64 encoded byte array (as defined above).
If the implementation supports stored defaults and such a default exists and is accessible, it is used in preference to the specified default, unless the stored default is not a valid Base64 encoded byte array (as defined above), in which case the specified default is used.
- Specified by:
getByteArrayin interfacePersistedPreferences- Specified by:
getByteArrayin classPreferences- Parameters:
key- key whose associated value is to be returned as a byte array.def- the value to be returned in the event that this preference node has no value associated withkeyor the associated value cannot be interpreted as a byte array, or the backing store is inaccessible.- Returns:
- the byte array value represented by the string associated with
keyin this preference node, ordefif the associated value does not exist or cannot be interpreted as a byte array. - See Also:
-
keys
Description copied from interface:PersistedPreferencesReturns all the keys that have an associated value in this preference node. (The returned array will be of size zero if this node has no preferences.)If the implementation supports stored defaults and there are any such defaults at this node that have not been overridden, by explicit preferences, the defaults are returned in the array in addition to any explicit preferences.
- Specified by:
keysin interfacePersistedPreferences- Specified by:
keysin classPreferences- Returns:
- an array of the keys that have an associated value in this preference node.
- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
childrenNames
Description copied from interface:PersistedPreferencesReturns the names of the children of this preference node, relative to this node. (The returned array will be of size zero if this node has no children.)- Specified by:
childrenNamesin interfacePersistedPreferences- Specified by:
childrenNamesin classPreferences- Returns:
- the names of the children of this preference node.
- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
parent
Description copied from interface:PersistedPreferencesReturns the parent of this preference node, ornullif this is the root.- Specified by:
parentin interfacePersistedPreferences- Specified by:
parentin classPreferences- Returns:
- the parent of this preference node.
-
node
Description copied from interface:PersistedPreferencesReturns the named preference node in the same tree as this node, creating it and any of its ancestors if they do not already exist. Accepts a relative or absolute path name. Relative path names (which do not begin with the slash character('/')) are interpreted relative to this preference node.If the returned node did not exist prior to this call, this node and any ancestors that were created by this call are not guaranteed to become permanent until the
flushmethod is called on the returned node (or one of its ancestors or descendants).- Specified by:
nodein interfacePersistedPreferences- Specified by:
nodein classPreferences- Parameters:
pathName- the path name of the preference node to return.- Returns:
- the specified preference node.
- See Also:
-
nodeExists
Description copied from interface:PersistedPreferencesReturns true if the named preference node exists in the same tree as this node. Relative path names (which do not begin with the slash character('/')) are interpreted relative to this preference node.If this node (or an ancestor) has already been removed with the
PersistedPreferences.removeNode()method, it is legal to invoke this method, but only with the path name""; the invocation will returnfalse. Thus, the idiomp.nodeExists("")may be used to test whetherphas been removed.- Specified by:
nodeExistsin interfacePersistedPreferences- Specified by:
nodeExistsin classPreferences- Parameters:
pathName- the path name of the node whose existence is to be checked.- Returns:
- true if the specified node exists.
- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
removeNode
Description copied from interface:PersistedPreferencesRemoves this preference node and all of its descendants, invalidating any preferences contained in the removed nodes. Once a node has been removed, attempting any method other thanPersistedPreferences.name(),PersistedPreferences.absolutePath(),PersistedPreferences.isUserNode(),PersistedPreferences.flush()ornodeExists("")on the correspondingPreferencesinstance will fail with anIllegalStateException. (The methods defined onObjectcan still be invoked on a node after it has been removed; they will not throwIllegalStateException.)The removal is not guaranteed to be persistent until the
flushmethod is called on this node (or an ancestor).If this implementation supports stored defaults, removing a node exposes any stored defaults at or below this node. Thus, a subsequent call to
nodeExistson this node's path name may returntrue, and a subsequent call tonodeon this path name may return a (different)Preferencesinstance representing a non-empty collection of preferences and/or children.- Specified by:
removeNodein interfacePersistedPreferences- Specified by:
removeNodein classPreferences- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
-
name
Description copied from interface:PersistedPreferencesReturns this preference node's name, relative to its parent.- Specified by:
namein interfacePersistedPreferences- Specified by:
namein classPreferences- Returns:
- this preference node's name, relative to its parent.
-
absolutePath
Description copied from interface:PersistedPreferencesReturns this preference node's absolute path name.- Specified by:
absolutePathin interfacePersistedPreferences- Specified by:
absolutePathin classPreferences- Returns:
- this preference node's absolute path name.
-
isUserNode
public boolean isUserNode()Description copied from interface:PersistedPreferencesReturnstrueif this preference node is in the user preference tree,falseif it's in the system preference tree.- Specified by:
isUserNodein interfacePersistedPreferences- Specified by:
isUserNodein classPreferences- Returns:
trueif this preference node is in the user preference tree,falseif it's in the system preference tree.
-
toString
- Specified by:
toStringin classPreferences
-
flush
Description copied from interface:PersistedPreferencesForces any changes in the contents of this preference node and its descendants to the persistent store. Once this method returns successfully, it is safe to assume that all changes made in the subtree rooted at this node prior to the method invocation have become permanent.Implementations are free to flush changes into the persistent store at any time. They do not need to wait for this method to be called.
When a flush occurs on a newly created node, it is made persistent, as are any ancestors (and descendants) that have yet to be made persistent. Please notice that any preference value changes in ancestors are not guaranteed to be made persistent.
If this method is invoked on a node that has been removed with the
PersistedPreferences.removeNode()method, flushSpi() is invoked on this node, but not on others.- Specified by:
flushin interfacePersistedPreferences- Specified by:
flushin classPreferences- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
-
sync
Description copied from interface:PersistedPreferencesEnsures that future reads from this preference node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to thesyncinvocation. As a side effect, forces any changes in the contents of this preference node and its descendants to the persistent store, as if theflushmethod had been invoked on this node.- Specified by:
syncin interfacePersistedPreferences- Specified by:
syncin classPreferences- Throws:
BackingStoreException- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
-
addPreferenceChangeListener
Description copied from interface:PersistedPreferencesRegisters the specified listener to receive preference change events for this preference node. A preference change event is generated when a preference is added to this node, removed from this node, or when the value associated with a preference is changed. (Preference change events are not generated by thePersistedPreferences.removeNode()method, which generates a node change event. Preference change events are generated by theclearmethod.)Events are only guaranteed for changes made within the same JVM as the registered listener, though some implementations may generate events for changes made outside this JVM. Events may be generated before the changes have been made persistent. Events are not generated when preferences are modified in descendants of this node; a caller desiring such events must register with each descendant.
- Specified by:
addPreferenceChangeListenerin interfacePersistedPreferences- Specified by:
addPreferenceChangeListenerin classPreferences- Parameters:
pcl- The preference change listener to add.- See Also:
-
removePreferenceChangeListener
Description copied from interface:PersistedPreferencesRemoves the specified preference change listener, so it no longer receives preference change events.- Specified by:
removePreferenceChangeListenerin interfacePersistedPreferences- Specified by:
removePreferenceChangeListenerin classPreferences- Parameters:
pcl- The preference change listener to remove.- See Also:
-
addNodeChangeListener
Description copied from interface:PersistedPreferencesRegisters the specified listener to receive node change events for this node. A node change event is generated when a child node is added to or removed from this node. (A singlePersistedPreferences.removeNode()invocation results in multiple node change events, one for every node in the subtree rooted at the removed node.)Events are only guaranteed for changes made within the same JVM as the registered listener, though some implementations may generate events for changes made outside this JVM. Events may be generated before the changes have become permanent. Events are not generated when indirect descendants of this node are added or removed; a caller desiring such events must register with each descendant.
Few guarantees can be made regarding node creation. Because nodes are created implicitly upon access, it may not be feasible for an implementation to determine whether a child node existed in the backing store prior to access (for example, because the backing store is unreachable or cached information is out of date). Under these circumstances, implementations are neither required to generate node change events nor prohibited from doing so.
- Specified by:
addNodeChangeListenerin interfacePersistedPreferences- Specified by:
addNodeChangeListenerin classPreferences- Parameters:
ncl- TheNodeChangeListenerto add.- See Also:
-
removeNodeChangeListener
Description copied from interface:PersistedPreferencesRemoves the specifiedNodeChangeListener, so it no longer receives change events.- Specified by:
removeNodeChangeListenerin interfacePersistedPreferences- Specified by:
removeNodeChangeListenerin classPreferences- Parameters:
ncl- TheNodeChangeListenerto remove.- See Also:
-
exportNode
- Specified by:
exportNodein classPreferences- Throws:
IOExceptionBackingStoreException
-
exportSubtree
- Specified by:
exportSubtreein classPreferences- Throws:
IOExceptionBackingStoreException
-
convertPathName
Converts the given pathname if absolute.
For pathname starting with a slash the optional name of the root node will be prepended. By default, the name of the root node is the empty string, but applications may overrideDefaultPreferencesFactory.userRoot()and/orDefaultPreferencesFactory.systemRoot()to create an extra storage subfolder for the application.- Parameters:
pathName- the original pathname- Returns:
- the possibly converted pathname
-