org.lsst.ccs.config.utilities
Class PersistentProperties

java.lang.Object
  extended by org.lsst.ccs.config.utilities.PersistentProperties
All Implemented Interfaces:
Serializable, Cloneable

public class PersistentProperties
extends Object
implements Serializable, Cloneable

Stores as String various types in a property store.
The way these properties are stored is abstract: though the usual (and default) way is to use a file, it can be modified to use something else (such as a database).

The type of the stored data is not obvious and should be known the the caller code. That's why there are different method to store/retrieve data (each method for each type).

important note : once you have stored some data with a key you cannot re-use this key for another type. Or if you want to do that then put the property to null before.

Important note : for the time being (this may change later) instances of this class are not garbage collected

Author:
bamade
See Also:
Serialized Form

Nested Class Summary
static class PersistentProperties.FileStore
           
static interface PersistentProperties.Store
          this class defines the way the properties are made persistent and read.
 
Constructor Summary
PersistentProperties(String name)
          uses a fileStore.
PersistentProperties(String name, PersistentProperties.Store store)
          creates a PersistenProperties with a specific store.
 
Method Summary
<T> T
getArray(String key, Class<T> arrayClass)
          returns a value stored in the properties as an array.
 Boolean getBoolean(String key)
          gets a Boolean value stored with this key
 Double getDouble(String key)
          get a double value associated with the key
 Float getFloat(String key)
          gets a Float from a value in the properties
 Integer getInteger(String key)
          get a Integer value from the Properties
 List getList(String key)
          returns a list stored in the properties.
 Map getMap(String key)
          returns a Map stored in the properties.
 Object getObject(String key)
          tries to return an object out of a String.
 String getString(String key)
          gets a String associated with a key
<T> T
getStruct(String key, Class<T> structClass)
          returns an object stored with the struct convention from the properties.
<T,V extends T>
T
putArray(String key, Class<T> arrayClass, V value)
          registers an array in the properties.
 Boolean putBoolean(String key, boolean bool)
          put a Boolean value in the properties
 Double putDouble(String key, double val)
          puts a Double value in the properties
 Float putFloat(String key, float val)
          registers a Float as a Property
 Integer putInteger(String key, int value)
          put an Integer value in the Properties
 List putList(String key, List list)
          registers a List to the properties.
 Map putMap(String key, Map map)
          stores a Map as a String in the properties.
 Object putObject(String key, Object obj)
          Puts an object with no type specification in the properties.
 String putString(String key, String value)
          registers a String in the properties
<T> T
putStruct(String key, Class<T> structClass, T value)
          This will store objects that stick to a convention (and known as struct.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistentProperties

public PersistentProperties(String name,
                            PersistentProperties.Store store)
creates a PersistenProperties with a specific store.

Parameters:
name - (beware should be consistent with that are legal for your store!)
store -

PersistentProperties

public PersistentProperties(String name)
uses a fileStore.

Parameters:
name - (beware should be consistent with a fileName)
Method Detail

putInteger

public Integer putInteger(String key,
                          int value)
put an Integer value in the Properties

Parameters:
key -
value -
Returns:
the previous Integer value or null

getInteger

public Integer getInteger(String key)
get a Integer value from the Properties

Parameters:
key -
Returns:
the value or null if value is not set (or set to null)

putBoolean

public Boolean putBoolean(String key,
                          boolean bool)
put a Boolean value in the properties

Parameters:
key -
bool -
Returns:
the previous Boolean value or null

getBoolean

public Boolean getBoolean(String key)
gets a Boolean value stored with this key

Parameters:
key -
Returns:

putDouble

public Double putDouble(String key,
                        double val)
puts a Double value in the properties

Parameters:
key -
val -
Returns:
previous double value or null

getDouble

public Double getDouble(String key)
get a double value associated with the key

Parameters:
key -
Returns:
value or null if no such property

putFloat

public Float putFloat(String key,
                      float val)
registers a Float as a Property

Parameters:
key -
val -
Returns:
previous value or null

getFloat

public Float getFloat(String key)
gets a Float from a value in the properties

Parameters:
key -
Returns:

putString

public String putString(String key,
                        String value)
registers a String in the properties

Parameters:
key -
value -
Returns:
previous value or null if there was none

getString

public String getString(String key)
gets a String associated with a key

Parameters:
key -
Returns:
value or null if none was found

putArray

public <T,V extends T> T putArray(String key,
                                  Class<T> arrayClass,
                                  V value)
registers an array in the properties. You'll have to declare an array class which is compatible with the value argument : if the result is not null the returned Object will be of this type.

So for example :

      Double[] arrDbl = {2.44, 6.66} ;
      Number[] arrNumber = myProperties.putArray("valueArray", Number[].class, arrDbl) ;
  
Doing this:
      Number[] arrNumber = myProperties.putArray("valueArray", Double[].class, arrDbl) ;
  
will work but you may end up with an ArrayStoreException when dealing later with arrNumber

Type Parameters:
T - type of Array with elements of type X
V - array of any type Y that is assignment compatible to X
Parameters:
key -
arrayClass - such as String[].class
value - could be an array which is "assignable to" this array elements should be of a primitive type, wrapper types, String, BigDecimal or Arrays of arrays of these types. Arrays of other types of Objects are not supported (some "structs" -see below-would work)
Returns:

getArray

public <T> T getArray(String key,
                      Class<T> arrayClass)
returns a value stored in the properties as an array.

Type Parameters:
T -
Parameters:
key -
arrayClass -
Returns:
the array which is represented by the String stored or null if there is none

putStruct

public <T> T putStruct(String key,
                       Class<T> structClass,
                       T value)
This will store objects that stick to a convention (and known as struct. The values are stored as a String version of a List or of a Map depending on the definition of the Object: In both cases the toString method of these object should exactly produce a List or Map litteral (with the conventions [1,'world',2.8] for lists and [number:1, name:'world', max:2.8] for Maps.

Type Parameters:
T -
Parameters:
key -
structClass - the name of the class that will be used to analyse previous value
value - any object that sticks to this struct convention
Returns:

getStruct

public <T> T getStruct(String key,
                       Class<T> structClass)
returns an object stored with the struct convention from the properties.

Type Parameters:
T -
Parameters:
key -
structClass -
Returns:

putList

public List putList(String key,
                    List list)
registers a List to the properties. Note that the elements of the list can be only wrappers (Double, Integer,...), and String objects or List of these lists (or Maps of this simple data).

Warning : a floating point value stored will be returned as a BigDecimal this may change in future releases (known feature)

Parameters:
key -
list -
Returns:

getList

public List getList(String key)
returns a list stored in the properties.

Warning : a floating point value stored will be returned as a BigDecimal this may change in future releases (known feature)

Parameters:
key -
Returns:

putMap

public Map putMap(String key,
                  Map map)
stores a Map as a String in the properties. Note that the values of the map can be only wrappers (Double, Integer,...), and String objects (or lists or Maps of such objects). Warning : a floating point value stored will be returned as a BigDecimal this may change in future releases (known feature)

Parameters:
key -
map -
Returns:

getMap

public Map getMap(String key)
returns a Map stored in the properties. Warning : a floating point value stored will be returned as a BigDecimal this may change in future releases (known feature)

Parameters:
key -
Returns:

putObject

public Object putObject(String key,
                        Object obj)
Puts an object with no type specification in the properties. Without type specification some unwanted effect might happen when reading the previous value:

Parameters:
key -
obj -
Returns:

getObject

public Object getObject(String key)
tries to return an object out of a String. See remarks with method putObject

Parameters:
key -
Returns:


Copyright © 2013 LSST. All Rights Reserved.