public abstract class ALcyProperties extends Object implements ILcdPropertyChangeSource
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 and Description |
---|
ALcyProperties() |
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener aListener)
Registers the given
PropertyChangeListener 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 aNewValue
|
Object |
get(String aKey,
Object aDefault)
Retrieves the value to which the specified key is mapped.
|
Object |
get(String aKey,
Object aDefault,
Class aType)
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 a
String for the specified key and parse it into a
boolean . |
boolean[] |
getBooleanArray(String aKey,
boolean[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
boolean[] . |
Color |
getColor(String aKey,
Color aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
Color . |
Color[] |
getColorArray(String aKey,
Color[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
Color[] . |
double |
getDouble(String aKey,
double aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
double . |
double[] |
getDoubleArray(String aKey,
double[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
double[] . |
float |
getFloat(String aKey,
float aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
float . |
float[] |
getFloatArray(String aKey,
float[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
float[] . |
protected abstract Object |
getImpl(String aKey)
Implementation version of the get method, which retrieves the value from the underlying store.
|
int |
getInt(String aKey,
int aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
int . |
int[] |
getIntArray(String aKey,
int[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
int[] . |
long |
getLong(String aKey,
long aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
long . |
long[] |
getLongArray(String aKey,
long[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
long[] . |
PropertyChangeListener[] |
getPropertyChangeListeners()
Returns an array of all the listeners that were added with either of the
addPropertyChangeListener
methods. |
PropertyChangeListener[] |
getPropertyChangeListeners(String aPropertyName)
Returns an array of all the listeners associated with the named property.
|
String |
getString(String aKey,
String aDefault)
Get the value to which the specified key is mapped.
|
String[] |
getStringArray(String aKey,
String[] aDefault)
Utility method to retrieve a
String for the specified key and parse it into a
String[] . |
boolean |
isEmpty()
Returns true if there are no key-value pairs.
|
abstract Iterator<String> |
keys()
Returns the keys of this
TLcyProperties . |
void |
put(String aKey,
Object aValue)
Maps the specified key to the specified value in this key-value pair list.
|
void |
put(String aKey,
Object aValue,
Class aType)
Utility method to format the given value of the given type as a
String 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 |
putAll(Map aMap)
Copies all key value pairs from the given map into this one.
|
void |
putBoolean(String aKey,
boolean aValue)
Utility method to format the given
boolean as a String and store it
as the value of the specified key. |
void |
putBooleanArray(String aKey,
boolean[] aValue)
Utility method to format the given
boolean[] as a String and store it
as the value of the specified key. |
void |
putColor(String aKey,
Color aValue)
Utility method to format the given
Color as a String and store it
as the value of the specified key. |
void |
putColorArray(String aKey,
Color[] aValue)
Utility method to format the given
Color[] as a String and store it
as the value of the specified key. |
void |
putDouble(String aKey,
double aValue)
Utility method to format the given
double as a String and store it
as the value of the specified key. |
void |
putDoubleArray(String aKey,
double[] aValue)
Utility method to format the given
double[] as a String and store it
as the value of the specified key. |
void |
putFloat(String aKey,
float aValue)
Utility method to format the given
float as a String and store it
as the value of the specified key. |
void |
putFloatArray(String aKey,
float[] aValue)
Utility method to format the given
float[] as a String and store it
as the value of the specified key. |
protected abstract void |
putImpl(String aKey,
Object aValue)
Implementation version of the put method, which puts the value in the underlying store.
|
void |
putInt(String aKey,
int aValue)
Utility method to format the given
int as a String and store it
as the value of the specified key. |
void |
putIntArray(String aKey,
int[] aValue)
Utility method to format the given
int[] as a String and store it
as the value of the specified key. |
void |
putLong(String aKey,
long aValue)
Utility method to format the given
long as a String and store it
as the value of the specified key. |
void |
putLongArray(String aKey,
long[] aValue)
Utility method to format the given
long[] as a String and store it
as the value of the specified key. |
void |
putString(String aKey,
String aValue)
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 given
String[] as a String and store it
as the value of the specified key. |
abstract void |
remove(String aKey)
Removes the key (and its corresponding value) from this key-value pair list.
|
void |
removePropertyChangeListener(PropertyChangeListener aListener)
De-registers the given
PropertyChangeListener 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.
|
ALcyProperties |
subset(String aKeyPrefix)
Returns a
ALcyProperties that represents a subset of the key-value pairs of
this ALcyProperties . |
static void |
synchronizeSFCT(ALcyProperties aSource,
ALcyProperties aDestinationSFCT)
Makes the given destination
ALcyProperties identical to the given source
ALcyProperties . |
static ALcyProperties |
unmodifiable(ALcyProperties aProperties)
Returns a wrapper around the given
ALcyProperties that throws UnsupportedOperationException s
for write operations (e.g. remove, put, ...). |
public ALcyProperties subset(String aKeyPrefix)
Returns a ALcyProperties
that represents a subset of the key-value pairs of
this ALcyProperties
. Only the keys that start with the given prefix are present in
the returned ALcyProperties
. Also, all keys are modified to strip the prefix.
subset("somePrefix.")
on these properties:
somePrefix.booleanProperty=true
somePrefix.stringProperty=someValue
other.someProperty=12
results in these properties:
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:
somePrefix.booleanProperty=true
other.someProperty=12
changes these properties to:
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
original ALcyProperties
. This affects you if you only create the subset
for adding a PropertyChangeListener
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() );
aKeyPrefix
- The prefix for the keys.ALcyProperties
containing only a subset of these key-value pairs.public static ALcyProperties unmodifiable(ALcyProperties aProperties)
ALcyProperties
that throws UnsupportedOperationException
s
for write operations (e.g. remove, put, ...).aProperties
- The properties to make unmodifiable.public void addPropertyChangeListener(PropertyChangeListener aListener)
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.
addPropertyChangeListener
in interface ILcdPropertyChangeSource
aListener
- The listener to be notifiedALcdWeakPropertyChangeListener
,
ILcdPropertyChangeSource.removePropertyChangeListener(java.beans.PropertyChangeListener)
public void addPropertyChangeListener(String aPropertyName, PropertyChangeListener aListener)
aPropertyName
- The property to watch for changes.aListener
- the property change listener to be addedpublic void removePropertyChangeListener(PropertyChangeListener aListener)
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.
removePropertyChangeListener
in interface ILcdPropertyChangeSource
aListener
- the listener that should no longer be notified of changes of
this object's propertiesILcdPropertyChangeSource.addPropertyChangeListener(java.beans.PropertyChangeListener)
public void removePropertyChangeListener(String aPropertyName, PropertyChangeListener aListener)
aPropertyName
- The property to stop watching for changes.aListener
- the property change listener to be removedpublic PropertyChangeListener[] getPropertyChangeListeners()
addPropertyChangeListener
methods.
This method obeys the semantics of java.beans.PropertyChangeSupport#getPropertyChangeListeners()
.
public PropertyChangeListener[] getPropertyChangeListeners(String aPropertyName)
aPropertyName
- The name of the property.aPropertyName
is null
,
an empty array is returned.protected void firePropertyChange(String aKey, Object aOldValue, Object aNewValue)
aKey
- the property name to fireaOldValue
- the old valueaNewValue
- the new valuepublic String getString(String aKey, String aDefault)
String
.aKey
- a key in this key-value pair list.aDefault
- a default value.putString(java.lang.String, java.lang.String)
public void putString(String aKey, String aValue)
aKey
- the keyaValue
- the valuegetString(java.lang.String, java.lang.String)
public String[] getStringArray(String aKey, String[] aDefault)
String
for the specified key and parse it into a
String[]
. Returns the specified default value when the value cannot be parsed
into a String[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putStringArray(java.lang.String, java.lang.String[])
public void putStringArray(String aKey, String[] aValue)
String[]
as a String
and store it
as the value of the specified key.aKey
- the key.aValue
- the value.getStringArray(java.lang.String, java.lang.String[])
public Color getColor(String aKey, Color aDefault)
String
for the specified key and parse it into a
Color
. Returns the specified default value when the value cannot be parsed
into a Color
.aKey
- a key in this key-value pair list.aDefault
- a default value.putColor(java.lang.String, java.awt.Color)
public void putColor(String aKey, Color aValue)
Color
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetColor(java.lang.String, java.awt.Color)
public Color[] getColorArray(String aKey, Color[] aDefault)
String
for the specified key and parse it into a
Color[]
. Returns the specified default value when the value cannot be parsed
into a Color[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putColorArray(java.lang.String, java.awt.Color[])
public void putColorArray(String aKey, Color[] aValue)
Color[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetColorArray(java.lang.String, java.awt.Color[])
public boolean getBoolean(String aKey, boolean aDefault)
String
for the specified key and parse it into a
boolean
. Returns the specified default value when the value cannot be parsed
into a boolean
.aKey
- a key in this key-value pair list.aDefault
- a default value.putBoolean(java.lang.String, boolean)
public void putBoolean(String aKey, boolean aValue)
boolean
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetBoolean(java.lang.String, boolean)
public boolean[] getBooleanArray(String aKey, boolean[] aDefault)
String
for the specified key and parse it into a
boolean[]
. Returns the specified default value when the value cannot be parsed
into a boolean[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putBooleanArray(java.lang.String, boolean[])
public void putBooleanArray(String aKey, boolean[] aValue)
boolean[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetBooleanArray(java.lang.String, boolean[])
public double getDouble(String aKey, double aDefault)
String
for the specified key and parse it into a
double
. Returns the specified default value when the value cannot be parsed
into a double
.aKey
- a key in this key-value pair list.aDefault
- a default value.putDouble(java.lang.String, double)
public void putDouble(String aKey, double aValue)
double
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetDouble(java.lang.String, double)
public double[] getDoubleArray(String aKey, double[] aDefault)
String
for the specified key and parse it into a
double[]
. Returns the specified default value when the value cannot be parsed
into a double[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putDoubleArray(java.lang.String, double[])
public void putDoubleArray(String aKey, double[] aValue)
double[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetDoubleArray(java.lang.String, double[])
public float getFloat(String aKey, float aDefault)
String
for the specified key and parse it into a
float
. Returns the specified default value when the value cannot be parsed
into a float
.aKey
- a key in this key-value pair list.aDefault
- a default value.putFloat(java.lang.String, float)
public void putFloat(String aKey, float aValue)
float
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetFloat(java.lang.String, float)
public float[] getFloatArray(String aKey, float[] aDefault)
String
for the specified key and parse it into a
float[]
. Returns the specified default value when the value cannot be parsed
into a float[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putFloatArray(java.lang.String, float[])
public void putFloatArray(String aKey, float[] aValue)
float[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetFloatArray(java.lang.String, float[])
public int getInt(String aKey, int aDefault)
String
for the specified key and parse it into a
int
. Returns the specified default value when the value cannot be parsed
into a int
.aKey
- a key in this key-value pair list.aDefault
- a default value.putInt(java.lang.String, int)
public void putInt(String aKey, int aValue)
int
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetInt(java.lang.String, int)
public int[] getIntArray(String aKey, int[] aDefault)
String
for the specified key and parse it into a
int[]
. Returns the specified default value when the value cannot be parsed
into a int[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putIntArray(java.lang.String, int[])
public void putIntArray(String aKey, int[] aValue)
int[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetIntArray(java.lang.String, int[])
public long getLong(String aKey, long aDefault)
String
for the specified key and parse it into a
long
. Returns the specified default value when the value cannot be parsed
into a long
.aKey
- a key in this key-value pair list.aDefault
- a default value.putLong(java.lang.String, long)
public void putLong(String aKey, long aValue)
long
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetLong(java.lang.String, long)
public long[] getLongArray(String aKey, long[] aDefault)
String
for the specified key and parse it into a
long[]
. Returns the specified default value when the value cannot be parsed
into a long[]
.aKey
- a key in this key-value pair list.aDefault
- a default value.putLongArray(java.lang.String, long[])
public void putLongArray(String aKey, long[] aValue)
long[]
as a String
and store it
as the value of the specified key.aKey
- the keyaValue
- the valuegetLongArray(java.lang.String, long[])
public Object get(String aKey, Object aDefault, Class aType)
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 call get( key, default, Integer.class
)
and get( 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.
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.put(String, Object, Class)
public void put(String aKey, Object aValue, Class aType)
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 call put( key, default, Integer.class
)
and put( key, default, Color.class )
. It there is no specific format
support for the given class (e.g. Object.class), this method behaves similar to
put(key,value)
.
aKey
- the key.aValue
- the value.aType
- The type of the object. For example String.class, Integer.class, etc.get(String, Object, Class)
public Object get(String aKey, Object aDefault)
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 an Integer
is stored using
putInt
(which formats the int
as a String
) and retrieved
using this get
method, a String
is returned.
aKey
- a key in this key-value pair list, must not be null
.aDefault
- a default value, used if the key is not found.put(java.lang.String, java.lang.Object, java.lang.Class)
public void put(String aKey, Object aValue)
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 this put
method, it is not converted to a
String.
aKey
- the key, must not be null
.aValue
- the value. If null
, this call has a similar effect as
remove(aKey)
.get(java.lang.String, java.lang.Object, java.lang.Class)
protected abstract Object getImpl(String aKey)
aKey
- The key, guaranteed to be not null
.null
if no value is associated.protected abstract void putImpl(String aKey, Object aValue)
aKey
- The key, guaranteed to be not null
.aValue
- The value to associate to the key, guaranteed to be not null
.public abstract void remove(String aKey)
aKey
- the key to be removedpublic abstract int size()
public boolean isEmpty()
public abstract boolean containsKey(String aKey)
aKey
- key to look forpublic abstract Iterator<String> keys()
TLcyProperties
.TLcyProperties
.public abstract void clear()
null-null-null
property change event indicating all properties have changed,
or in a separate event for all removed properties (implementation specific).public void putAll(ALcyProperties aProperties)
aProperties
- The properties to copy from.public void putAll(Map aMap)
aMap
- The map to copy from. All keys must be String
s.public static void synchronizeSFCT(ALcyProperties aSource, ALcyProperties aDestinationSFCT)
ALcyProperties
identical to the given source
ALcyProperties
. It copies all key-value pairs from the given source to the given
destination ALcyProperties
, and all keys in the source properties that are not in
the destination properties are removed from the destination.aSource
- The properties to synchronize from.aDestinationSFCT
- The properties to make identical to the source properties.