Class ALcyProperties
- All Implemented Interfaces:
ILcdPropertyChangeSource
- Direct Known Subclasses:
ALcyPropertiesWrapper
,TLcyCompositeProperties
,TLcyProperties
,TLcyStringProperties
This abstract class represents a key-value pair collection. It is different from
java.util.Map
in that the keys are forced to be String
objects (not
null
) and that
events are fired whenever a key-value pair changes. Use
addPropertyChangeListener(java.beans.PropertyChangeListener)
to receive events about
changes in the key-value pairs. Inserting a null
value for some key has the same
effect as removing a key.
This class has utility methods to work with Strings, String[]'s, Colors, Color[]'s,
booleans, boolean[]'s, doubles, double[]'s, floats, float[]'s, ints, int[]'s, longs and
long[]'s. These methods convert the values to and from String
s by formatting and
parsing them. This allows to easily store those simple types in a text file, for example
by using TLcyStringPropertiesCodec
. As a side effect, property change events for
changes to these simple types contain a String
for the old and the new value.
The simple types are converted to a String
representation using their
toString
and valueOf
methods, for example Integer.toString
and Integer.valueOf
. When storing arrays, the values are separated by comma's. If a
comma is used in an element, it is escaped as \,
subset(String)
allows to create a subset view of all keys that start with
a given prefix. unmodifiable(ALcyProperties)
wraps the
ALcyProperties
so that they can't be modified.
Implementers of subclasses must fire property change events when values are added, removed
or changed. The property name is the key that was modified. When all values are changed at once
(e.g. clear), a property change event with a null
property name should be fired.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addPropertyChangeListener
(PropertyChangeListener aListener) Registers the givenPropertyChangeListener
to be notified when this object's properties change.void
addPropertyChangeListener
(String aPropertyName, PropertyChangeListener aListener) Adds a PropertyChangeListener to the listener list for a specific property.abstract void
clear()
Clears this key-value pair list so that it contains no keys.abstract boolean
containsKey
(String aKey) Returns true if the key-value pair with the specified key exists.protected void
firePropertyChange
(String aKey, Object aOldValue, Object aNewValue) Inform the PropertyChangeListeners that the property 'aKey' has changed from aOldValue to aNewValueRetrieves the value to which the specified key is mapped.Utility method to retrieve the value for the specified key and parse it into an object of the given type.boolean
getBoolean
(String aKey, boolean aDefault) Utility method to retrieve aString
for the specified key and parse it into aboolean
.boolean[]
getBooleanArray
(String aKey, boolean[] aDefault) Utility method to retrieve aString
for the specified key and parse it into aboolean[]
.Utility method to retrieve aString
for the specified key and parse it into aColor
.Color[]
getColorArray
(String aKey, Color[] aDefault) Utility method to retrieve aString
for the specified key and parse it into aColor[]
.double
Utility method to retrieve aString
for the specified key and parse it into adouble
.double[]
getDoubleArray
(String aKey, double[] aDefault) Utility method to retrieve aString
for the specified key and parse it into adouble[]
.float
Utility method to retrieve aString
for the specified key and parse it into afloat
.float[]
getFloatArray
(String aKey, float[] aDefault) Utility method to retrieve aString
for the specified key and parse it into afloat[]
.protected abstract Object
Implementation version of the get method, which retrieves the value from the underlying store.int
Utility method to retrieve aString
for the specified key and parse it into aint
.int[]
getIntArray
(String aKey, int[] aDefault) Utility method to retrieve aString
for the specified key and parse it into aint[]
.long
Utility method to retrieve aString
for the specified key and parse it into along
.long[]
getLongArray
(String aKey, long[] aDefault) Utility method to retrieve aString
for the specified key and parse it into along[]
.Returns an array of all the listeners that were added with either of theaddPropertyChangeListener
methods.getPropertyChangeListeners
(String aPropertyName) Returns an array of all the listeners associated with the named property.Get the value to which the specified key is mapped.String[]
getStringArray
(String aKey, String[] aDefault) Utility method to retrieve aString
for the specified key and parse it into aString[]
.boolean
isEmpty()
Returns true if there are no key-value pairs.keys()
Returns the keys of thisTLcyProperties
.void
Maps the specified key to the specified value in this key-value pair list.void
Utility method to format the given value of the given type as aString
and store it as the value of the specified key.void
putAll
(ALcyProperties aProperties) Copies all key value pairs from the given properties into this one.void
Copies all key value pairs from the given map into this one.void
putBoolean
(String aKey, boolean aValue) Utility method to format the givenboolean
as aString
and store it as the value of the specified key.void
putBooleanArray
(String aKey, boolean[] aValue) Utility method to format the givenboolean[]
as aString
and store it as the value of the specified key.void
Utility method to format the givenColor
as aString
and store it as the value of the specified key.void
putColorArray
(String aKey, Color[] aValue) Utility method to format the givenColor[]
as aString
and store it as the value of the specified key.void
Utility method to format the givendouble
as aString
and store it as the value of the specified key.void
putDoubleArray
(String aKey, double[] aValue) Utility method to format the givendouble[]
as aString
and store it as the value of the specified key.void
Utility method to format the givenfloat
as aString
and store it as the value of the specified key.void
putFloatArray
(String aKey, float[] aValue) Utility method to format the givenfloat[]
as aString
and store it as the value of the specified key.protected abstract void
Implementation version of the put method, which puts the value in the underlying store.void
Utility method to format the givenint
as aString
and store it as the value of the specified key.void
putIntArray
(String aKey, int[] aValue) Utility method to format the givenint[]
as aString
and store it as the value of the specified key.void
Utility method to format the givenlong
as aString
and store it as the value of the specified key.void
putLongArray
(String aKey, long[] aValue) Utility method to format the givenlong[]
as aString
and store it as the value of the specified key.void
Maps the specified key to the specified value in this key-value pair list.void
putStringArray
(String aKey, String[] aValue) Utility method to format the givenString[]
as aString
and store it as the value of the specified key.abstract void
Removes the key (and its corresponding value) from this key-value pair list.void
De-registers the givenPropertyChangeListener
from receiving property change events for this object.void
removePropertyChangeListener
(String aPropertyName, PropertyChangeListener aListener) Removes a PropertyChangeListener from the listener list for a specific property.abstract int
size()
Returns the number of key-value pairs.Returns aALcyProperties
that represents a subset of the key-value pairs of thisALcyProperties
.static void
synchronizeSFCT
(ALcyProperties aSource, ALcyProperties aDestinationSFCT) Makes the given destinationALcyProperties
identical to the given sourceALcyProperties
.static ALcyProperties
unmodifiable
(ALcyProperties aProperties) Returns a wrapper around the givenALcyProperties
that throwsUnsupportedOperationException
s for write operations (e.g. remove, put, ...).
-
Constructor Details
-
ALcyProperties
public ALcyProperties()
-
-
Method Details
-
subset
Returns a
For example, callingALcyProperties
that represents a subset of the key-value pairs of thisALcyProperties
. Only the keys that start with the given prefix are present in the returnedALcyProperties
. Also, all keys are modified to strip the prefix.subset("somePrefix.")
on these properties:
results in these properties:somePrefix.booleanProperty=true somePrefix.stringProperty=someValue other.someProperty=12
booleanProperty=true stringProperty=someValue
All operations on the returned subset should be reflected in the original
ALcyProperties
as well.For example, calling
subset("somePrefix.").putString("stringProperty")
on these properties:
changes these properties to:somePrefix.booleanProperty=true other.someProperty=12
somePrefix.booleanProperty=true somePrefix.stringProperty=someValue other.someProperty=12
Some extensions of
ALcyProperties
might want to override this method to provide a more efficient implementation if desired and possible.Note that there is no strong reference from this
ALcyProperties
to the created subset. This means that the life-time of the subset is not the same as that of the originalALcyProperties
. This affects you if you only create the subset for adding aPropertyChangeListener
to it. For example, in the following code snippet, the subset eventually will be garbage collected and no property change events will be received from it any more.ALcyProperties original = getOriginalProperties(); orginal.subset( "somePrefix" ).addPropertyChangeListener( createListener() );
- Parameters:
aKeyPrefix
- The prefix for the keys.- Returns:
- A
ALcyProperties
containing only a subset of these key-value pairs.
-
unmodifiable
Returns a wrapper around the givenALcyProperties
that throwsUnsupportedOperationException
s for write operations (e.g. remove, put, ...).- Parameters:
aProperties
- The properties to make unmodifiable.- Returns:
- The unmodifiable properties.
-
addPropertyChangeListener
Description copied from interface:ILcdPropertyChangeSource
Registers the given
PropertyChangeListener
to be notified when this object's properties change.In case you need to register a listener which keeps a reference to an object with a shorter life-time than this change source, you can use a
ALcdWeakPropertyChangeListener
instance as property change listener.- Specified by:
addPropertyChangeListener
in interfaceILcdPropertyChangeSource
- Parameters:
aListener
- The listener to be notified- See Also:
-
addPropertyChangeListener
Adds a PropertyChangeListener to the listener list for a specific property. The listener is only registered for the key-value pairs corresponding to the given aPropertyName. The listener will be invoked only when a call on firePropertyChange names that specific property. If listener is null, no exception is thrown and no action is performed.- Parameters:
aPropertyName
- The property to watch for changes.aListener
- the property change listener to be added
-
removePropertyChangeListener
Description copied from interface:ILcdPropertyChangeSource
De-registers the given
PropertyChangeListener
from receiving property change events for this object.If the listener was added more than once, it will be notified one less time after being removed. If the listener is
null
, or was never added, no exception is thrown and no action is taken.- Specified by:
removePropertyChangeListener
in interfaceILcdPropertyChangeSource
- Parameters:
aListener
- the listener that should no longer be notified of changes of this object's properties- See Also:
-
removePropertyChangeListener
Removes a PropertyChangeListener from the listener list for a specific property. This method should be used to remove PropertyChangeListeners that were registered for key-value pairs corresponding to the given aPropertyName. If listener is null, no exception is thrown and no action is performed.- Parameters:
aPropertyName
- The property to stop watching for changes.aListener
- the property change listener to be removed
-
getPropertyChangeListeners
Returns an array of all the listeners that were added with either of theaddPropertyChangeListener
methods.This method obeys the semantics of
java.beans.PropertyChangeSupport#getPropertyChangeListeners()
.- Returns:
- an array of all added listeners.
-
getPropertyChangeListeners
Returns an array of all the listeners associated with the named property.- Parameters:
aPropertyName
- The name of the property.- Returns:
- an array of all the listeners associated with the named property.
If no such listeners have been added, or if
aPropertyName
isnull
, an empty array is returned.
-
firePropertyChange
Inform the PropertyChangeListeners that the property 'aKey' has changed from aOldValue to aNewValue- Parameters:
aKey
- the property name to fireaOldValue
- the old valueaNewValue
- the new value
-
getString
Get the value to which the specified key is mapped. Returns the specified default value when the value is not of typeString
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putString
Maps the specified key to the specified value in this key-value pair list. The value can be retrieved by calling the getString method with a key that is equal to the original key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getStringArray
Utility method to retrieve aString
for the specified key and parse it into aString[]
. Returns the specified default value when the value cannot be parsed into aString[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putStringArray
Utility method to format the givenString[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the key.aValue
- the value.- See Also:
-
getColor
Utility method to retrieve aString
for the specified key and parse it into aColor
. Returns the specified default value when the value cannot be parsed into aColor
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putColor
Utility method to format the givenColor
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getColorArray
Utility method to retrieve aString
for the specified key and parse it into aColor[]
. Returns the specified default value when the value cannot be parsed into aColor[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putColorArray
Utility method to format the givenColor[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getBoolean
Utility method to retrieve aString
for the specified key and parse it into aboolean
. Returns the specified default value when the value cannot be parsed into aboolean
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putBoolean
Utility method to format the givenboolean
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getBooleanArray
Utility method to retrieve aString
for the specified key and parse it into aboolean[]
. Returns the specified default value when the value cannot be parsed into aboolean[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putBooleanArray
Utility method to format the givenboolean[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getDouble
Utility method to retrieve aString
for the specified key and parse it into adouble
. Returns the specified default value when the value cannot be parsed into adouble
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putDouble
Utility method to format the givendouble
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getDoubleArray
Utility method to retrieve aString
for the specified key and parse it into adouble[]
. Returns the specified default value when the value cannot be parsed into adouble[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putDoubleArray
Utility method to format the givendouble[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getFloat
Utility method to retrieve aString
for the specified key and parse it into afloat
. Returns the specified default value when the value cannot be parsed into afloat
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putFloat
Utility method to format the givenfloat
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getFloatArray
Utility method to retrieve aString
for the specified key and parse it into afloat[]
. Returns the specified default value when the value cannot be parsed into afloat[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putFloatArray
Utility method to format the givenfloat[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getInt
Utility method to retrieve aString
for the specified key and parse it into aint
. Returns the specified default value when the value cannot be parsed into aint
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putInt
Utility method to format the givenint
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getIntArray
Utility method to retrieve aString
for the specified key and parse it into aint[]
. Returns the specified default value when the value cannot be parsed into aint[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putIntArray
Utility method to format the givenint[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getLong
Utility method to retrieve aString
for the specified key and parse it into along
. Returns the specified default value when the value cannot be parsed into along
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putLong
Utility method to format the givenlong
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
getLongArray
Utility method to retrieve aString
for the specified key and parse it into along[]
. Returns the specified default value when the value cannot be parsed into along[]
.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
putLongArray
Utility method to format the givenlong[]
as aString
and store it as the value of the specified key.- Parameters:
aKey
- the keyaValue
- the value- See Also:
-
get
Utility method to retrieve the value for the specified key and parse it into an object of the given type.
This method is mostly useful when working with the properties in a general way, to avoid that general code needs to call the specific methods such as
getInt
,getColor
etc., but instead can callget( key, default, Integer.class )
andget( key, default, Color.class )
. It returns the specified default value when the value cannot be parsed into the requested type. It there is no specific parsing code for the given type (e.g.Object.class
), this method returns the value as is (without parsing it), but only if it is assignable to the given type.- Parameters:
aKey
- a key in this key-value pair list.aDefault
- a default value.aType
- The type of the object. For example String.class, Integer.class, etc.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
put
Utility method to format the given value of the given type as a
String
and store it as the value of the specified key.This method is mostly useful when working with the properties in a general way, to avoid that general code needs to call the specific methods such as
putInt
,putColor
etc., but instead can callput( key, default, Integer.class )
andput( key, default, Color.class )
. It there is no specific format support for the given class (e.g. Object.class), this method behaves similar toput(key,value)
.- Parameters:
aKey
- the key.aValue
- the value.aType
- The type of the object. For example String.class, Integer.class, etc.- See Also:
-
get
Retrieves the value to which the specified key is mapped. Returns the specified default value when the key is not found.
Note that because no type is specified, no parsing is performed (use
get(String, Object, Class)
for that). So if for example anInteger
is stored usingputInt
(which formats theint
as aString
) and retrieved using thisget
method, aString
is returned.- Parameters:
aKey
- a key in this key-value pair list, must not benull
.aDefault
- a default value, used if the key is not found.- Returns:
- the value of the key-value pair with the specified key.
- See Also:
-
put
Maps the specified key to the specified value in this key-value pair list. The value can be retrieved by calling the get method with a key that is equal to the original key.
Note that because no type is specified, no conversions are applied. So if for example an
Integer
is stored using thisput
method, it is not converted to a String.- Parameters:
aKey
- the key, must not benull
.aValue
- the value. Ifnull
, this call has a similar effect asremove(aKey)
.- See Also:
-
getImpl
Implementation version of the get method, which retrieves the value from the underlying store.- Parameters:
aKey
- The key, guaranteed to be notnull
.- Returns:
- The value associated with the key, or
null
if no value is associated.
-
putImpl
Implementation version of the put method, which puts the value in the underlying store.- Parameters:
aKey
- The key, guaranteed to be notnull
.aValue
- The value to associate to the key, guaranteed to be notnull
.
-
remove
Removes the key (and its corresponding value) from this key-value pair list. This method does nothing if the key is unknown.- Parameters:
aKey
- the key to be removed
-
size
public abstract int size()Returns the number of key-value pairs.- Returns:
- the number of key-value pairs.
-
isEmpty
public boolean isEmpty()Returns true if there are no key-value pairs.- Returns:
- true if there are no key-value pairs
- Since:
- 2021.0
-
containsKey
Returns true if the key-value pair with the specified key exists.- Parameters:
aKey
- key to look for- Returns:
- true if the key-value pair with the specified key exists.
-
keys
Returns the keys of thisTLcyProperties
.- Returns:
- the keys of this
TLcyProperties
.
-
clear
public abstract void clear()Clears this key-value pair list so that it contains no keys. Calling this method could result in anull-null-null
property change event indicating all properties have changed, or in a separate event for all removed properties (implementation specific). -
putAll
Copies all key value pairs from the given properties into this one.- Parameters:
aProperties
- The properties to copy from.
-
putAll
Copies all key value pairs from the given map into this one.- Parameters:
aMap
- The map to copy from. All keys must beString
s.
-
synchronizeSFCT
Makes the given destinationALcyProperties
identical to the given sourceALcyProperties
. It copies all key-value pairs from the given source to the given destinationALcyProperties
, and all keys in the source properties that are not in the destination properties are removed from the destination.- Parameters:
aSource
- The properties to synchronize from.aDestinationSFCT
- The properties to make identical to the source properties.
-