com.bluemarsh.jswat
Class AppSettings

java.lang.Object
  |
  +--com.bluemarsh.jswat.AppSettings

public class AppSettings
extends java.lang.Object

Implements a properties storage class. This is used to store a variety of properties, each associated with a key string. Each key string must be unique or otherwise you will overwrite existing properties in the table. The properties can be saved to a file and restored at a later time, allowing persistent data storage.

This class implements the Singleton design pattern to ensure that only one instance of this class exists in the system. To get the single instance you can call the instanceOf() method.

Author:
Nathan Fiedler
See Also:
Properties

Field Summary
protected  java.io.File iniFile
          Ini file passed to load().
protected static AppSettings instance
          Reference to the singular instance of this class.
protected  java.util.Properties table
          Table that stores our properties.
 
Constructor Summary
protected AppSettings()
          No-arg constructor; sets up the hash table used to store the properites.
 
Method Summary
 void commit()
          This saves the current settings to the file given in load().
 boolean contains(java.lang.String key)
          Tests if the specified key maps to an entry in the table.
 boolean getBoolean(java.lang.String key)
          Get the value of the given boolean.
 double getDouble(java.lang.String key)
          Get the value of the given double.
 int getInteger(java.lang.String key)
          Get the value of the given integer.
 java.lang.String getString(java.lang.String key)
          Get the value of the given string.
static AppSettings instanceOf()
          Returns a reference to the single instance of this class.
 boolean load(java.io.File file)
          Loads a new set of properties from the given file.
 void remove(java.lang.String key)
          Removes the key and value from the properties table.
 void setBoolean(java.lang.String key, boolean value)
          Set the value of a boolean, given the name of the property and the new boolean value.
 void setDouble(java.lang.String key, double value)
          Set the value of a double, given the name of the property and the new double value.
 void setInteger(java.lang.String key, int value)
          Set the value of a integer, given the name of the property and the new integer value.
 void setString(java.lang.String key, java.lang.String value)
          Set the value of a string, given the name of the property and the new string value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

table

protected java.util.Properties table
Table that stores our properties.

instance

protected static AppSettings instance
Reference to the singular instance of this class.

iniFile

protected java.io.File iniFile
Ini file passed to load(). Saved for the commit() call.
Constructor Detail

AppSettings

protected AppSettings()
No-arg constructor; sets up the hash table used to store the properites. This is protected because this class is a Singleton. You gain access to the instance of this class via the instanceOf() method.
Method Detail

commit

public void commit()
This saves the current settings to the file given in load(). After making any changes to the settings you should call this method. Ideally you should make all the necessary changes and then call this method to save the changes.
Throws:
java.lang.IllegalStateException - Thrown if load() method has not already been called to set the INI file.

contains

public boolean contains(java.lang.String key)
Tests if the specified key maps to an entry in the table.
Parameters:
key - key to look up in the table
Returns:
true if the key exists in the table; false otherwise

getBoolean

public boolean getBoolean(java.lang.String key)
Get the value of the given boolean. If the key does not exist this method will return false.
Parameters:
key - name of the boolean property to retrieve
Returns:
the value of the boolean

getDouble

public double getDouble(java.lang.String key)
Get the value of the given double. If the key does not exist this method will return zero.
Parameters:
key - name of the double property to retrieve
Returns:
the value of the double

getInteger

public int getInteger(java.lang.String key)
Get the value of the given integer. If the key does not exist this method will return zero.
Parameters:
key - name of the integer property to retrieve
Returns:
the value of the integer

getString

public java.lang.String getString(java.lang.String key)
Get the value of the given string. If the key does not exist this method will return an empty string.
Parameters:
key - name of the string property to retrieve
Returns:
the value of the string

instanceOf

public static AppSettings instanceOf()
Returns a reference to the single instance of this class. If one does not exist it will be created.
Returns:
instance of class AppSettings

load

public boolean load(java.io.File file)
Loads a new set of properties from the given file. The current set will be overwritten with the contents of the file.
Parameters:
file - file containing properties
Returns:
false if error

remove

public void remove(java.lang.String key)
Removes the key and value from the properties table. After calling this method you should make sure the change is committed by calling commit().
Parameters:
key - key to remove from table

setBoolean

public void setBoolean(java.lang.String key,
                       boolean value)
Set the value of a boolean, given the name of the property and the new boolean value. Be sure to call commit() to save the change to file.
Parameters:
key - name of the boolean property to set
value - new value for the boolean property

setDouble

public void setDouble(java.lang.String key,
                      double value)
Set the value of a double, given the name of the property and the new double value. Be sure to call commit() to save the change to file.
Parameters:
key - name of the boolean property to set
value - new value for the double property

setInteger

public void setInteger(java.lang.String key,
                       int value)
Set the value of a integer, given the name of the property and the new integer value. Be sure to call commit() to save the change to file.
Parameters:
key - name of the boolean property to set
value - new value for the integer property

setString

public void setString(java.lang.String key,
                      java.lang.String value)
Set the value of a string, given the name of the property and the new string value. Be sure to call commit() to save the change to file.
Parameters:
key - name of the boolean property to set
value - new value for the string property