net.sourceforge.drpetrea.jsettings
Class IniSettings

Object
  extended by IniSettings

public class IniSettings
extends Object

This is a generic class for handling settings. It behaves like the Properties and Preferences class from the java library, but it extends the functionality by offering also default values and checking against legal values. For this purpose it will maintain two internal maps, both having as keys the setting's name; one having as values the actual values of the setting (be they default or user defined) and the other one holding the regexps to validate the value pushed by the user.

This class is meant to handle settings that are presented as a key-value pair, e.g.

key = value (syntax is not important here).

Note that the key and the value can be separated by anything, including blanks. This is governed by the rules in the SettingsFile class.

I call this ini-style settings, because they are mostly found in .ini configuration files. The most popular example may be the Linux kernel config file (please hold the flames for calling the kernel config file by an M$-consacrated name - only the logic of the file is similar)

Another feature is that if specified so in the constructor (or alternatively by default), the dimension of the respective settings collection is fixed after construction, meaning the user cannot add new settings to the collection.

Author:
Dragos Petre
See Also:
FstabSettings

Field Summary
static LocaleTools lt
          The LocaleTools object.
 
Constructor Summary
IniSettings()
          The default constructor is not initializing the maps, but just setting the expandable parameter to true - it's not possible to have the maps uninitialized AND not be able to extend them afterwards.
IniSettings(ArrayList<String> names)
          The user can also call a constructor with only one array of strings, in which case the regular expression will admit any values and the default values will be set to empty strings.
IniSettings(ArrayList<String> names, ArrayList<String> values)
          The user can also call a constructor with only two arrays of strings, in which case the regular expression will admit any values.
IniSettings(ArrayList<String> names, ArrayList<String> values, ArrayList<String> laws)
          The user can also call a constructor without the boolean param, in this case the default value is FALSE.
IniSettings(ArrayList<String> names, ArrayList<String> values, ArrayList<String> laws, boolean expandable)
          The main constructor function, inputing the string arrays into the internal maps.
IniSettings(ArrayList<String> names, ArrayList<String> values, boolean expandable)
          The user can also call a constructor with only two arrays of strings, in which case the regular expression will admit any values.
IniSettings(ArrayList<String> names, boolean expandable)
          The user can also call a constructor with only one array of strings, in which case the regular expression will admit any values and the default values will be set to empty strings.
 
Method Summary
 boolean containsName(String name)
          This method checks if the specified setting is defined.
 String getValue(String name)
          This method returns the stored value for setting name
 boolean isExpandable()
          This methods lets the user check the 'expandability' of this object
 Set<String> names()
          This method returns a String Set containing all the defined settings
 void putName(String name, String law)
          This method extends the settings class with a new name
 String putValue(String name, String value)
          This method first checks that the value matches the regular expression and if it does stores it.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lt

public static LocaleTools lt
The LocaleTools object.

Constructor Detail

IniSettings

public IniSettings(ArrayList<String> names,
                   ArrayList<String> values,
                   ArrayList<String> laws,
                   boolean expandable)
The main constructor function, inputing the string arrays into the internal maps.

Parameters:
names - The string array containing the names of the settings to be handled by this object
values - The string array containing the default values for the corresponding settings
laws - The regular expression that any value should match to be considered legal
expandable - True if the user would like to be able to later add new settings to this object

IniSettings

public IniSettings(ArrayList<String> names,
                   ArrayList<String> values,
                   ArrayList<String> laws)
The user can also call a constructor without the boolean param, in this case the default value is FALSE.

Parameters:
names - The string array containing the names of the settings to be handled by this object
values - The string array containing the default values for the corresponding settings
laws - The regular expression that any value should match to be considered legal

IniSettings

public IniSettings(ArrayList<String> names,
                   ArrayList<String> values,
                   boolean expandable)
The user can also call a constructor with only two arrays of strings, in which case the regular expression will admit any values. This is the version with a boolean parameter

Parameters:
names - The string array containing the names of the settings to be handled by this object
values - The string array containing the default values for the corresponding settings
expandable - True if the user would like to be able to later add new settings to this object

IniSettings

public IniSettings(ArrayList<String> names,
                   ArrayList<String> values)
The user can also call a constructor with only two arrays of strings, in which case the regular expression will admit any values. This is the version without a boolean parameter

Parameters:
names - The string array containing the names of the settings to be handled by this object
values - The string array containing the default values for the corresponding settings

IniSettings

public IniSettings(ArrayList<String> names,
                   boolean expandable)
The user can also call a constructor with only one array of strings, in which case the regular expression will admit any values and the default values will be set to empty strings. This is the version with the boolean parameter

Parameters:
names - The string array containing the names of the settings to be handled by this object
expandable - True if the user would like to be able to later add new settings to this object

IniSettings

public IniSettings(ArrayList<String> names)
The user can also call a constructor with only one array of strings, in which case the regular expression will admit any values and the default values will be set to empty strings. This is the version without the boolean parameter

Parameters:
names - The string array containing the names of the settings to be handled by this object

IniSettings

public IniSettings()
The default constructor is not initializing the maps, but just setting the expandable parameter to true - it's not possible to have the maps uninitialized AND not be able to extend them afterwards.

Method Detail

putValue

public String putValue(String name,
                       String value)
This method first checks that the value matches the regular expression and if it does stores it.

Parameters:
name - The name of the setting
value - The correspondig value for setting name
Returns:
The previously stored value for setting name, if any. In case of error returns a blank string

putName

public void putName(String name,
                    String law)
This method extends the settings class with a new name

Parameters:
name - The name of the new setting
law - The regular expression corresponding to the new setting

getValue

public String getValue(String name)
This method returns the stored value for setting name

Parameters:
name - The setting whose value it is querried
Returns:
The string containing the value for name. In case of error returns a blank string

containsName

public boolean containsName(String name)
This method checks if the specified setting is defined. This is equivalent to Names().contains(name)

Parameters:
name - The setting whose presence is checked
Returns:
true if it exists, false if not

isExpandable

public boolean isExpandable()
This methods lets the user check the 'expandability' of this object

Returns:
true if the object is expandable.

names

public Set<String> names()
This method returns a String Set containing all the defined settings

Returns:
All the defined settings.