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 Strings 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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPropertyChangeListener(PropertyChangeListener aListener) Registers the givenPropertyChangeListenerto be notified when this object's properties change.voidaddPropertyChangeListener(String aPropertyName, PropertyChangeListener aListener) Adds a PropertyChangeListener to the listener list for a specific property.abstract voidclear()Clears this key-value pair list so that it contains no keys.abstract booleancontainsKey(String aKey) Returns true if the key-value pair with the specified key exists.protected voidfirePropertyChange(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.booleangetBoolean(String aKey, boolean aDefault) Utility method to retrieve aStringfor the specified key and parse it into aboolean.boolean[]getBooleanArray(String aKey, boolean[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into aboolean[].Utility method to retrieve aStringfor the specified key and parse it into aColor.Color[]getColorArray(String aKey, Color[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into aColor[].doubleUtility method to retrieve aStringfor the specified key and parse it into adouble.double[]getDoubleArray(String aKey, double[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into adouble[].floatUtility method to retrieve aStringfor the specified key and parse it into afloat.float[]getFloatArray(String aKey, float[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into afloat[].protected abstract ObjectImplementation version of the get method, which retrieves the value from the underlying store.intUtility method to retrieve aStringfor the specified key and parse it into aint.int[]getIntArray(String aKey, int[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into aint[].longUtility method to retrieve aStringfor the specified key and parse it into along.long[]getLongArray(String aKey, long[] aDefault) Utility method to retrieve aStringfor the specified key and parse it into along[].Returns an array of all the listeners that were added with either of theaddPropertyChangeListenermethods.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 aStringfor the specified key and parse it into aString[].booleanisEmpty()Returns true if there are no key-value pairs.keys()Returns the keys of thisTLcyProperties.voidMaps the specified key to the specified value in this key-value pair list.voidUtility method to format the given value of the given type as aStringand store it as the value of the specified key.voidputAll(ALcyProperties aProperties) Copies all key value pairs from the given properties into this one.voidCopies all key value pairs from the given map into this one.voidputBoolean(String aKey, boolean aValue) Utility method to format the givenbooleanas aStringand store it as the value of the specified key.voidputBooleanArray(String aKey, boolean[] aValue) Utility method to format the givenboolean[]as aStringand store it as the value of the specified key.voidUtility method to format the givenColoras aStringand store it as the value of the specified key.voidputColorArray(String aKey, Color[] aValue) Utility method to format the givenColor[]as aStringand store it as the value of the specified key.voidUtility method to format the givendoubleas aStringand store it as the value of the specified key.voidputDoubleArray(String aKey, double[] aValue) Utility method to format the givendouble[]as aStringand store it as the value of the specified key.voidUtility method to format the givenfloatas aStringand store it as the value of the specified key.voidputFloatArray(String aKey, float[] aValue) Utility method to format the givenfloat[]as aStringand store it as the value of the specified key.protected abstract voidImplementation version of the put method, which puts the value in the underlying store.voidUtility method to format the givenintas aStringand store it as the value of the specified key.voidputIntArray(String aKey, int[] aValue) Utility method to format the givenint[]as aStringand store it as the value of the specified key.voidUtility method to format the givenlongas aStringand store it as the value of the specified key.voidputLongArray(String aKey, long[] aValue) Utility method to format the givenlong[]as aStringand store it as the value of the specified key.voidMaps the specified key to the specified value in this key-value pair list.voidputStringArray(String aKey, String[] aValue) Utility method to format the givenString[]as aStringand store it as the value of the specified key.abstract voidRemoves the key (and its corresponding value) from this key-value pair list.voidDe-registers the givenPropertyChangeListenerfrom receiving property change events for this object.voidremovePropertyChangeListener(String aPropertyName, PropertyChangeListener aListener) Removes a PropertyChangeListener from the listener list for a specific property.abstract intsize()Returns the number of key-value pairs.Returns aALcyPropertiesthat represents a subset of the key-value pairs of thisALcyProperties.static voidsynchronizeSFCT(ALcyProperties aSource, ALcyProperties aDestinationSFCT) Makes the given destinationALcyPropertiesidentical to the given sourceALcyProperties.static ALcyPropertiesunmodifiable(ALcyProperties aProperties) Returns a wrapper around the givenALcyPropertiesthat throwsUnsupportedOperationExceptions for write operations (e.g. remove, put, ...).
-
Constructor Details
-
ALcyProperties
public ALcyProperties()
-
-
Method Details
-
subset
Returns a
For example, callingALcyPropertiesthat 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=12booleanProperty=true stringProperty=someValueAll operations on the returned subset should be reflected in the original
ALcyPropertiesas well.For example, calling
subset("somePrefix.").putString("stringProperty")on these properties:
changes these properties to:somePrefix.booleanProperty=true other.someProperty=12somePrefix.booleanProperty=true somePrefix.stringProperty=someValue other.someProperty=12Some extensions of
ALcyPropertiesmight want to override this method to provide a more efficient implementation if desired and possible.Note that there is no strong reference from this
ALcyPropertiesto 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 aPropertyChangeListenerto 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
ALcyPropertiescontaining only a subset of these key-value pairs.
-
unmodifiable
Returns a wrapper around the givenALcyPropertiesthat throwsUnsupportedOperationExceptions for write operations (e.g. remove, put, ...).- Parameters:
aProperties- The properties to make unmodifiable.- Returns:
- The unmodifiable properties.
-
addPropertyChangeListener
Description copied from interface:ILcdPropertyChangeSourceRegisters the given
PropertyChangeListenerto 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
ALcdWeakPropertyChangeListenerinstance as property change listener.- Specified by:
addPropertyChangeListenerin 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:ILcdPropertyChangeSourceDe-registers the given
PropertyChangeListenerfrom 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:
removePropertyChangeListenerin 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 theaddPropertyChangeListenermethods.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
aPropertyNameisnull, 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 aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the key.aValue- the value.- See Also:
-
getColor
Utility method to retrieve aStringfor 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 givenColoras aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getColorArray
Utility method to retrieve aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getBoolean
Utility method to retrieve aStringfor 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 givenbooleanas aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getBooleanArray
Utility method to retrieve aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getDouble
Utility method to retrieve aStringfor 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 givendoubleas aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getDoubleArray
Utility method to retrieve aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getFloat
Utility method to retrieve aStringfor 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 givenfloatas aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getFloatArray
Utility method to retrieve aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getInt
Utility method to retrieve aStringfor 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 givenintas aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getIntArray
Utility method to retrieve aStringfor 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 aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getLong
Utility method to retrieve aStringfor 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 givenlongas aStringand store it as the value of the specified key.- Parameters:
aKey- the keyaValue- the value- See Also:
-
getLongArray
Utility method to retrieve aStringfor 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 aStringand 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,getColoretc., 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
Stringand 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,putColoretc., 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 anIntegeris stored usingputInt(which formats theintas aString) and retrieved using thisgetmethod, aStringis 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
Integeris stored using thisputmethod, 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
nullif 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-nullproperty 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 beStrings.
-
synchronizeSFCT
Makes the given destinationALcyPropertiesidentical 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.
-