Class ALcyProperties

java.lang.Object
com.luciad.lucy.util.properties.ALcyProperties
All Implemented Interfaces:
ILcdPropertyChangeSource
Direct Known Subclasses:
ALcyPropertiesWrapper, TLcyCompositeProperties, TLcyProperties, TLcyStringProperties

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 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Registers the given PropertyChangeListener to be notified when this object's properties change.
    void
    Adds a PropertyChangeListener to the listener list for a specific property.
    abstract void
    Clears this key-value pair list so that it contains no keys.
    abstract boolean
    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
    get(String aKey, Object aDefault)
    Retrieves the value to which the specified key is mapped.
    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[].
    getColor(String aKey, Color aDefault)
    Utility method to retrieve a String for the specified key and parse it into a 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
    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[].
    Returns an array of all the listeners that were added with either of the addPropertyChangeListener methods.
    Returns an array of all the listeners associated with the named property.
    getString(String aKey, String aDefault)
    Get the value to which the specified key is mapped.
    getStringArray(String aKey, String[] aDefault)
    Utility method to retrieve a String for the specified key and parse it into a String[].
    boolean
    Returns true if there are no key-value pairs.
    abstract Iterator<String>
    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
    De-registers the given PropertyChangeListener from receiving property change events for this object.
    void
    Removes a PropertyChangeListener from the listener list for a specific property.
    abstract int
    Returns the number of key-value pairs.
    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.
    Returns a wrapper around the given ALcyProperties that throws UnsupportedOperationExceptions for write operations (e.g. remove, put, ...).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ALcyProperties

      public ALcyProperties()
  • Method Details

    • subset

      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.

      For example, calling 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() );
       
      Parameters:
      aKeyPrefix - The prefix for the keys.
      Returns:
      A ALcyProperties containing only a subset of these key-value pairs.
    • unmodifiable

      public static ALcyProperties unmodifiable(ALcyProperties aProperties)
      Returns a wrapper around the given ALcyProperties that throws UnsupportedOperationExceptions for write operations (e.g. remove, put, ...).
      Parameters:
      aProperties - The properties to make unmodifiable.
      Returns:
      The unmodifiable properties.
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener aListener)
      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 interface ILcdPropertyChangeSource
      Parameters:
      aListener - The listener to be notified
      See Also:
    • addPropertyChangeListener

      public void addPropertyChangeListener(String aPropertyName, PropertyChangeListener aListener)
      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

      public void removePropertyChangeListener(PropertyChangeListener aListener)
      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 interface ILcdPropertyChangeSource
      Parameters:
      aListener - the listener that should no longer be notified of changes of this object's properties
      See Also:
    • removePropertyChangeListener

      public void removePropertyChangeListener(String aPropertyName, PropertyChangeListener aListener)
      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

      public PropertyChangeListener[] getPropertyChangeListeners()
      Returns an array of all the listeners that were added with either of the addPropertyChangeListener methods.

      This method obeys the semantics of java.beans.PropertyChangeSupport#getPropertyChangeListeners().

      Returns:
      an array of all added listeners.
    • getPropertyChangeListeners

      public PropertyChangeListener[] getPropertyChangeListeners(String aPropertyName)
      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 is null, an empty array is returned.
    • firePropertyChange

      protected void firePropertyChange(String aKey, Object aOldValue, Object aNewValue)
      Inform the PropertyChangeListeners that the property 'aKey' has changed from aOldValue to aNewValue
      Parameters:
      aKey - the property name to fire
      aOldValue - the old value
      aNewValue - the new value
    • getString

      public String getString(String aKey, String aDefault)
      Get the value to which the specified key is mapped. Returns the specified default value when the value is not of type String.
      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

      public void putString(String aKey, String aValue)
      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 key
      aValue - the value
      See Also:
    • getStringArray

      public String[] getStringArray(String aKey, String[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key.
      aValue - the value.
      See Also:
    • getColor

      public Color getColor(String aKey, Color aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getColorArray

      public Color[] getColorArray(String aKey, Color[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getBoolean

      public boolean getBoolean(String aKey, boolean aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getBooleanArray

      public boolean[] getBooleanArray(String aKey, boolean[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getDouble

      public double getDouble(String aKey, double aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getDoubleArray

      public double[] getDoubleArray(String aKey, double[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getFloat

      public float getFloat(String aKey, float aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getFloatArray

      public float[] getFloatArray(String aKey, float[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getInt

      public int getInt(String aKey, int aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getIntArray

      public int[] getIntArray(String aKey, int[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getLong

      public long getLong(String aKey, long aDefault)
      Utility method to retrieve a 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.
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • getLongArray

      public long[] getLongArray(String aKey, long[] aDefault)
      Utility method to retrieve a 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[].
      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

      public 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.
      Parameters:
      aKey - the key
      aValue - the value
      See Also:
    • get

      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.

      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

      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).

      Parameters:
      aKey - the key.
      aValue - the value.
      aType - The type of the object. For example String.class, Integer.class, etc.
      See Also:
    • get

      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.

      Parameters:
      aKey - a key in this key-value pair list, must not be null.
      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

      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.

      Parameters:
      aKey - the key, must not be null.
      aValue - the value. If null, this call has a similar effect as remove(aKey).
      See Also:
    • getImpl

      protected abstract Object getImpl(String aKey)
      Implementation version of the get method, which retrieves the value from the underlying store.
      Parameters:
      aKey - The key, guaranteed to be not null.
      Returns:
      The value associated with the key, or null if no value is associated.
    • putImpl

      protected abstract void putImpl(String aKey, Object aValue)
      Implementation version of the put method, which puts the value in the underlying store.
      Parameters:
      aKey - The key, guaranteed to be not null.
      aValue - The value to associate to the key, guaranteed to be not null.
    • remove

      public abstract void remove(String aKey)
      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

      public abstract boolean containsKey(String aKey)
      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

      public abstract Iterator<String> keys()
      Returns the keys of this TLcyProperties.
      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 a null-null-null property change event indicating all properties have changed, or in a separate event for all removed properties (implementation specific).
    • putAll

      public void putAll(ALcyProperties aProperties)
      Copies all key value pairs from the given properties into this one.
      Parameters:
      aProperties - The properties to copy from.
    • putAll

      public void putAll(Map aMap)
      Copies all key value pairs from the given map into this one.
      Parameters:
      aMap - The map to copy from. All keys must be Strings.
    • synchronizeSFCT

      public static void synchronizeSFCT(ALcyProperties aSource, ALcyProperties aDestinationSFCT)
      Makes the given destination 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.
      Parameters:
      aSource - The properties to synchronize from.
      aDestinationSFCT - The properties to make identical to the source properties.