Class AbstractPreferences
- All Implemented Interfaces:
IPreferences
- Direct Known Subclasses:
Preferences
- Author:
- ralph
-
Field Summary
Fields inherited from interface rs.baselib.prefs.IPreferences
CHILD_ADDED, CHILD_REMOVED, VALUES_CLEARED
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor.AbstractPreferences
(AbstractPreferences parent, String name) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionReturns this node's absolute path name.protected void
addNode
(AbstractPreferences child) Adds the given node.void
void
addPropertyChangeListener
(String propertyName, PropertyChangeListener listener) String[]
Returns the names of the children of this node.void
clear()
Removes all of the properties (key-value associations) in this node.protected abstract AbstractPreferences
createNode
(AbstractPreferences parent, String name) Subclasses must implement this to return a new child node.protected abstract Lock
Creates the appropriate read lock object.protected abstract Lock
Creates the appropriate write lock object.protected boolean
Fires a change event.protected boolean
firePropertyChange
(String propertyName, Object oldValue, Object newValue) Fires an event if property changed.void
flush()
Forces any changes in the contents of this node and its descendants to the persistent store.Returns the value associated with the specifiedkey
in this node.boolean
getBoolean
(String key, boolean def) Returns theboolean
value represented by theString
object associated with the specifiedkey
in this node.byte[]
getByteArray
(String key, byte[] def) Returns thebyte[]
value represented by theString
object associated with the specifiedkey
in this node.double
Returns thedouble
value represented by theString
object associated with the specifiedkey
in this node.float
Returns the floatvalue
represented by theString
object associated with the specifiedkey
in this node.int
Returns theint
value represented by theString
object associated with the specifiedkey
in this node.long
Returns thelong
value represented by theString
object associated with the specifiedkey
in this node.protected abstract AbstractPreferencesService
Returns the preferences service.Returns the readLock.protected AbstractPreferences
Returns the root node of this node.Returns the writeLock.protected boolean
isAbsolute
(String pathName) Returns true when the given pathname is absolute to root node of this node.protected boolean
isRelative
(String pathName) Returns true when the given pathname is relative to this node.protected boolean
Returns true when this is a root node.String[]
keys()
Returns all of the keys that have an associated value in this node.name()
Returns this node's name, relative to its parent.Returns a namedPreferences
object (node), creating it and any of its ancestors if they do not already exist.boolean
nodeExists
(String pathName) Returns true if the named node exists.parent()
Returns the parent of this node, ornull
if this is the root.void
Associates the specified value with the specified key in this node.void
putBoolean
(String key, boolean value) Associates aString
object representing the specifiedboolean
value with the specified key in this node.void
putByteArray
(String key, byte[] value) Associates aString
object representing the specifiedbyte[]
with the specifiedkey
in this node.void
Associates aString
object representing the specifieddouble
value with the specifiedkey
in this node.void
Associates aString
object representing the specifiedfloat
value with the specifiedkey
in this node.void
Associates aString
object representing the specifiedint
value with the specifiedkey
in this node.void
Associates aString
object representing the specifiedlong
value with the specifiedkey
in this node.protected void
readLock()
Aquires a read lock.protected void
Releases a read lock.void
Removes the value associated with the specifiedkey
in this node, if any.void
Removes this node and all of its descendants, invalidating any properties contained in the removed nodes.protected void
removeNode
(AbstractPreferences child) Removes the given node.void
void
removePropertyChangeListener
(String propertyName, PropertyChangeListener listener) void
sync()
Ensures that future reads from this node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to thesync
invocation.protected void
Aquires a write lock.protected void
Releases a write lock.
-
Constructor Details
-
AbstractPreferences
public AbstractPreferences()Constructor. Creates a root node. -
AbstractPreferences
Constructor.- Parameters:
parent
- the parent of this nodename
- the name of this preferences
-
-
Method Details
-
createReadLock
Creates the appropriate read lock object.- Returns:
- the read lock object
-
createWriteLock
Creates the appropriate write lock object.- Returns:
- the write lock object
-
getReadLock
Returns the readLock.- Returns:
- the readLock
-
readLock
protected void readLock()Aquires a read lock. -
readUnlock
protected void readUnlock()Releases a read lock. -
getWriteLock
Returns the writeLock.- Returns:
- the writeLock
-
writeLock
protected void writeLock()Aquires a write lock. -
writeUnlock
protected void writeUnlock()Releases a write lock. -
createNode
Subclasses must implement this to return a new child node.- Parameters:
parent
- the parent to be usedname
- the name to be used- Returns:
- the new node
-
addNode
Adds the given node. The scheduling policy will define whether this task is executed immediately or performed by a background task (which requires aflush()
call to access the node later).- Parameters:
child
- child to be added
-
removeNode
Removes the given node. The scheduling policy will define whether this task is executed immediately or performed by a background task (which requires aflush()
to see the result).- Parameters:
child
- child to be removed
-
put
Associates the specified value with the specified key in this node.- Specified by:
put
in interfaceIPreferences
- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.
-
get
Returns the value associated with the specifiedkey
in this node. Returns the specified default if there is no value associated with thekey
, or the backing store is inaccessible.- Specified by:
get
in interfaceIPreferences
- Parameters:
key
- key whose associated value is to be returned.def
- the value to be returned in the event that this node has no value associated withkey
or the backing store is inaccessible.- Returns:
- the value associated with
key
, ordef
if no value is associated withkey
.
-
remove
Removes the value associated with the specifiedkey
in this node, if any.- Specified by:
remove
in interfaceIPreferences
- Parameters:
key
- key whose mapping is to be removed from this node.- See Also:
-
clear
Removes all of the properties (key-value associations) in this node. This call has no effect on any descendants of this node.- Specified by:
clear
in interfaceIPreferences
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
-
putInt
Associates aString
object representing the specifiedint
value with the specifiedkey
in this node. The associated string is the one that would be returned if theint
value were passed toInteger.toString(int)
. This method is intended for use in conjunction withIPreferences.getInt(java.lang.String, int)
method.Implementor's note: it is not necessary that the property value be represented by a
String
object in the backing store. If the backing store supports integer values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as anint
(withgetInt
or aString
(withget
) type.- Specified by:
putInt
in interfaceIPreferences
- Parameters:
key
- key with which the string form of value is to be associated.value
-value
whose string form is to be associated withkey
.- See Also:
-
getInt
Returns theint
value represented by theString
object associated with the specifiedkey
in this node. TheString
object is converted to anint
as byInteger.parseInt(String)
. Returns the specified default if there is no value associated with thekey
, the backing store is inaccessible, or ifInteger.parseInt(String)
would throw aNumberFormatException
if the associatedvalue
were passed. This method is intended for use in conjunction with theIPreferences.putInt(java.lang.String, int)
method.- Specified by:
getInt
in interfaceIPreferences
- Parameters:
key
- key whose associated value is to be returned as anint
.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as anint
or the backing store is inaccessible.- Returns:
- the
int
value represented by theString
object associated withkey
in this node, ordef
if the associated value does not exist or cannot be interpreted as anint
type. - See Also:
-
putLong
Associates aString
object representing the specifiedlong
value with the specifiedkey
in this node. The associatedString
object is the one that would be returned if thelong
value were passed toLong.toString(long)
. This method is intended for use in conjunction with theIPreferences.getLong(java.lang.String, long)
method.Implementor's note: it is not necessary that the
value
be represented by aString
type in the backing store. If the backing store supportslong
values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as along
(withgetLong
or aString
(withget
) type.- Specified by:
putLong
in interfaceIPreferences
- Parameters:
key
-key
with which the string form ofvalue
is to be associated.value
-value
whose string form is to be associated withkey
.- See Also:
-
getLong
Returns thelong
value represented by theString
object associated with the specifiedkey
in this node. TheString
object is converted to along
as byLong.parseLong(String)
. Returns the specified default if there is no value associated with thekey
, the backing store is inaccessible, or ifLong.parseLong(String)
would throw aNumberFormatException
if the associatedvalue
were passed. This method is intended for use in conjunction with theIPreferences.putLong(java.lang.String, long)
method.- Specified by:
getLong
in interfaceIPreferences
- Parameters:
key
-key
whose associated value is to be returned as along
value.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as along
type or the backing store is inaccessible.- Returns:
- the
long
value represented by theString
object associated withkey
in this node, ordef
if the associated value does not exist or cannot be interpreted as along
type. - See Also:
-
putBoolean
Associates aString
object representing the specifiedboolean
value with the specified key in this node. The associated string is "true" if the value istrue
, and "false" if it isfalse
. This method is intended for use in conjunction with theIPreferences.getBoolean(java.lang.String, boolean)
method.Implementor's note: it is not necessary that the value be represented by a string in the backing store. If the backing store supports
boolean
values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as aboolean
(withgetBoolean
) or aString
(withget
) type.- Specified by:
putBoolean
in interfaceIPreferences
- Parameters:
key
-key
with which the string form of value is to be associated.value
- value whose string form is to be associated withkey
.- See Also:
-
getBoolean
Returns theboolean
value represented by theString
object associated with the specifiedkey
in this node. Valid strings are "true", which representstrue
, and "false", which representsfalse
. Case is ignored, so, for example, "TRUE" and "False" are also valid. This method is intended for use in conjunction with theIPreferences.putBoolean(java.lang.String, boolean)
method.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 case.- Specified by:
getBoolean
in interfaceIPreferences
- Parameters:
key
-key
whose associated value is to be returned as aboolean
.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as aboolean
or the backing store is inaccessible.- Returns:
- the
boolean
value represented by theString
object associated withkey
in this node, ornull
if the associated value does not exist or cannot be interpreted as aboolean
. - See Also:
-
putFloat
Associates aString
object representing the specifiedfloat
value with the specifiedkey
in this node. The associatedString
object is the one that would be returned if thefloat
value were passed toFloat.toString(float)
. This method is intended for use in conjunction with theIPreferences.getFloat(java.lang.String, float)
method.Implementor's note: it is not necessary that the value be represented by a string in the backing store. If the backing store supports
float
values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as afloat
(withgetFloat
) or aString
(withget
) type.- Specified by:
putFloat
in interfaceIPreferences
- Parameters:
key
-key
with which the string form of value is to be associated.value
- value whose string form is to be associated withkey
.- See Also:
-
getFloat
Returns the floatvalue
represented by theString
object associated with the specifiedkey
in this node. TheString
object is converted to afloat
value as byFloat.parseFloat(String)
. Returns the specified default if there is no value associated with thekey
, the backing store is inaccessible, or ifFloat.parseFloat(String)
would throw aNumberFormatException
if the associated value were passed. This method is intended for use in conjunction with theIPreferences.putFloat(java.lang.String, float)
method.- Specified by:
getFloat
in interfaceIPreferences
- Parameters:
key
-key
whose associated value is to be returned as afloat
value.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as afloat
type or the backing store is inaccessible.- Returns:
- the
float
value represented by the string associated withkey
in this node, ordef
if the associated value does not exist or cannot be interpreted as afloat
type. - See Also:
-
putDouble
Associates aString
object representing the specifieddouble
value with the specifiedkey
in this node. The associatedString
object is the one that would be returned if thedouble
value were passed toDouble.toString(double)
. This method is intended for use in conjunction with theIPreferences.getDouble(java.lang.String, double)
methodImplementor's note: it is not necessary that the value be represented by a string in the backing store. If the backing store supports
double
values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as adouble
(withgetDouble
) or aString
(withget
) type.- Specified by:
putDouble
in interfaceIPreferences
- Parameters:
key
-key
with which the string form of value is to be associated.value
- value whose string form is to be associated withkey
.- See Also:
-
getDouble
Returns thedouble
value represented by theString
object associated with the specifiedkey
in this node. TheString
object is converted to adouble
value as byDouble.parseDouble(String)
. Returns the specified default if there is no value associated with thekey
, the backing store is inaccessible, or ifDouble.parseDouble(String)
would throw aNumberFormatException
if the associated value were passed. This method is intended for use in conjunction with theIPreferences.putDouble(java.lang.String, double)
method.- Specified by:
getDouble
in interfaceIPreferences
- Parameters:
key
-key
whose associated value is to be returned as adouble
value.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as adouble
type or the backing store is inaccessible.- Returns:
- the
double
value represented by theString
object associated withkey
in this node, ordef
if the associated value does not exist or cannot be interpreted as adouble
type. - See Also:
-
putByteArray
Associates aString
object representing the specifiedbyte[]
with the specifiedkey
in this node. The associatedString
object the Base64 encoding of thebyte[]
, 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. This method is intended for use in conjunction with theIPreferences.getByteArray(java.lang.String, byte[])
method.Implementor's note: it is not necessary that the value be represented by a
String
type in the backing store. If the backing store supportsbyte[]
values, it is not unreasonable to use them. This implementation detail is not visible through thePreferences
API, which allows the value to be read as an abyte[]
object (withgetByteArray
) or aString
object (withget
).- Specified by:
putByteArray
in interfaceIPreferences
- Parameters:
key
-key
with which the string form ofvalue
is to be associated.value
-value
whose string form is to be associated withkey
.- See Also:
-
getByteArray
Returns thebyte[]
value represented by theString
object associated with the specifiedkey
in this node. ValidString
objects 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 with theIPreferences.putByteArray(java.lang.String, byte[])
method.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).- Specified by:
getByteArray
in interfaceIPreferences
- Parameters:
key
-key
whose associated value is to be returned as abyte[]
object.def
- the value to be returned in the event that this node has no value associated withkey
or the associated value cannot be interpreted as abyte[]
type, or the backing store is inaccessible.- Returns:
- the
byte[]
value represented by theString
object associated withkey
in this node, ordef
if the associated value does not exist or cannot be interpreted as abyte[]
. - See Also:
-
keys
Returns all of the keys that have an associated value in this node. (The returned array will be of size zero if this node has no preferences and notnull
!)- Specified by:
keys
in interfaceIPreferences
- Returns:
- an array of the keys that have an associated value in this node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
childrenNames
Returns the names of the children of this node. (The returned array will be of size zero if this node has no children and notnull
!)- Specified by:
childrenNames
in interfaceIPreferences
- Returns:
- the names of the children of this node.
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
-
parent
Returns the parent of this node, ornull
if this is the root.- Specified by:
parent
in interfaceIPreferences
- Returns:
- the parent of this node.
-
isRelative
Returns true when the given pathname is relative to this node. This means the path does not start with a slash ('/').- Parameters:
pathName
- name to be checked- Returns:
true
when path name is relative to this node
-
isAbsolute
Returns true when the given pathname is absolute to root node of this node. This means the path starts with a slash ('/').- Parameters:
pathName
- name to be checked- Returns:
true
when path name is absolute to root node of this node
-
isRootNode
protected boolean isRootNode()Returns true when this is a root node.- Returns:
true
when this is a root node
-
getRootNode
Returns the root node of this node.- Returns:
- root node
-
node
Returns a namedPreferences
object (node), creating it and any of its ancestors if they do not already exist. Accepts a relative or absolute pathname. Absolute pathnames (which begin with'/'
) are interpreted relative to the root of this node. Relative pathnames (which begin with any character other than'/'
) are interpreted relative to this node itself. The empty string (""
) is a valid relative pathname, referring to this node itself.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 persistent until the
flush
method is called on the returned node (or one of its descendants).- Specified by:
node
in interfaceIPreferences
- Parameters:
pathName
- the path name of thePreferences
object to return.- Returns:
- the specified
Preferences
object. - See Also:
-
nodeExists
Returns true if the named node exists. Accepts a relative or absolute pathname. Absolute pathnames (which begin with'/'
) are interpreted relative to the root of this node. Relative pathnames (which begin with any character other than'/'
) are interpreted relative to this node itself. The pathname""
is valid, and refers to this node itself.If this node (or an ancestor) has already been removed with the
IPreferences.removeNode()
method, it is legal to invoke this method, but only with the pathname""
; the invocation will returnfalse
. Thus, the idiomp.nodeExists("")
may be used to test whetherp
has been removed.- Specified by:
nodeExists
in interfaceIPreferences
- 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
Removes this node and all of its descendants, invalidating any properties contained in the removed nodes. Once a node has been removed, attempting any method other thanname()
,absolutePath()
ornodeExists("")
on the correspondingPreferences
instance will fail with anIllegalStateException
. (The methods defined onObject
can 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
flush
method is called on the parent of this node.- Specified by:
removeNode
in interfaceIPreferences
- 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
Returns this node's name, relative to its parent.- Specified by:
name
in interfaceIPreferences
- Returns:
- this node's name, relative to its parent.
-
absolutePath
Returns this node's absolute path name. Note that:- Root node - The path name of the root node is
"/"
. - Slash at end - Path names other than that of the root node may not
end in slash (
'/'
). - Unusual names -
"."
and".."
have no special significance in path names. - Illegal names - The only illegal path names are those that contain multiple consecutive slashes, or that end in slash and are not the root.
- Specified by:
absolutePath
in interfaceIPreferences
- Returns:
- this node's absolute path name.
- Root node - The path name of the root node is
-
getPreferencesService
Returns the preferences service.- Returns:
- the preferences service
-
flush
Forces any changes in the contents of this 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. Note however that any properties value changes in ancestors are not guaranteed to be made persistent.
- Specified by:
flush
in interfaceIPreferences
- 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
Ensures that future reads from this node and its descendants reflect any changes that were committed to the persistent store (from any VM) prior to thesync
invocation. As a side-effect, forces any changes in the contents of this node and its descendants to the persistent store, as if theflush
method had been invoked on this node.- Specified by:
sync
in interfaceIPreferences
- Throws:
BackingStoreException
- if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.- See Also:
-
addPropertyChangeListener
-
removePropertyChangeListener
-
addPropertyChangeListener
-
removePropertyChangeListener
-
firePropertyChange
Fires an event if property changed.- Parameters:
propertyName
- name of propertyoldValue
- old valuenewValue
- new value- Returns:
true
when the event was fired (because values were not equal)
-
firePropertyChange
Fires a change event.- Parameters:
event
- event to be fired- Returns:
true
when the event was fired (because values were not equal)
-