net.sourceforge.drpetrea.jsettings
Class FstabSettings

Object
  extended by FstabSettings

public class FstabSettings
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 an internal bi-dimensional string array (implemented as an ArrayList of ArrayList) with the values and an uni-dimensional string array with the regular expression defining the legal values

This class is meant to handle settings that are presented as rows of tabularly ordered fields e.g.

Head row Field 1 Field 2 Field 3
Value1-1 Value1-2 Value1-3
Value2-1 Value2-2 Value2-3
I call this fstab-style settings because it is inspired by the fstab file in the Unix systems. In case a reminder is needed, the fstab file contains informations about the diferent file systems. Each filesystem is presented on a different line, with fields separated by blanks (or tabs). A line has the format

fs_spec fs_file fs_vfstype fs_mntops fs_freq fs_passno
with each field having a special meaning.

Unlike the ini-style settings, the fstab doesn't have keys and values, but all the lines describe the same kind of information. Another difference lies in the fact that for each entry, several fields of data are possible.

For flexibility, we will allow for multiplicity of instances of values in each of the fields to be defined by user; i.e. the user will decide if in the example above Value1-1 can or not be equal to Value2-1

This class also implements an ArrayList with default values for the different fields - the mechanism is that in case the value of the field is blank (a blank string - ""), the default value should be used instead. Nothing stops the default value from being blank itself, if that is wanted. This is applicable to the case of having values added to the object after its construction - if the values ArrayList passed to the constructor does not contain all required values, then the constructor will fail. This is because if, considering the example above, the third ArrayList does contain only one value instead of two, it is dangerous to assume that either Value3-1 or Value3-2 are missing and that should be replaced by the default.

In this class, like in the ini-style one, the entered values will be checked against regular expression for validity

Author:
Dragos Petre
See Also:
IniSettings

Field Summary
static LocaleTools lt
          The LocaleTools object.
 
Constructor Summary
FstabSettings()
          The default constructor - in this case the expandable flag will be true and the members will be initialized.
FstabSettings(ArrayList<String> names)
          The constructor that only declares the field names.
FstabSettings(ArrayList<String> names, ArrayList<ArrayList<String>> values, ArrayList<String> defaults, ArrayList<String> laws)
          The constructor that populates the names, values, defaults and the laws.
FstabSettings(ArrayList<String> names, ArrayList<ArrayList<String>> values, ArrayList<String> defaults, ArrayList<String> laws, boolean expandable)
          The constructor that populates the values, laws and sets the expandable flag
FstabSettings(ArrayList<String> names, ArrayList<String> defaults)
          The constructor that only declares the field names and the defaults.
FstabSettings(ArrayList<String> names, ArrayList<String> defaults, ArrayList<String> laws)
          The constructor that populates the names, defaults and laws.
 
Method Summary
 boolean containsField(String field)
          The method returns true if the object contains a specific field
 boolean deleteRow(ArrayList<String> row)
          This method deletes one whole row of settings.
 int fieldIndex(String field)
          The method returns the index of the field
 String fieldName(int index)
          The method returns the name of the field for a known index
 ArrayList<Integer> getIndexesForValue(String value, int field)
          This method returns the indexes of the rows containing the requested value in the specified field.
 ArrayList<Integer> getIndexesForValue(String value, String field)
          This method returns the indexes of the rows corresponding to the requested value in the specified field
 ArrayList<ArrayList<String>> getRows()
          This method returns all the rows in the current object.
 ArrayList<ArrayList<String>> getRows(ArrayList<Integer> rows)
          This method returns the rows corresponding to the requested indexes.
 ArrayList<ArrayList<String>> getRowsForValue(String value, int field)
          This method returns the row corresponding to the requested value in the specified field
 ArrayList<ArrayList<String>> getRowsForValue(String value, String field)
          This method returns the row corresponding to the requested value in the specified field
 int getRowsNumber()
          The method returns the number of rows stored
 ArrayList<String> getValuesForValue(String value_known, int field_known, int field_unknown)
          This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.
 ArrayList<String> getValuesForValue(String value_known, int field_known, String field_unknown)
          This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.
 ArrayList<String> getValuesForValue(String value_known, String field_known, int field_unknown)
          This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.
 ArrayList<String> getValuesForValue(String value_known, String field_known, String field_unknown)
          This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.
 boolean isExpandable()
          This methods lets the user check the 'expandability' of this object
 String putValue(ArrayList<String> row, int field, String value)
          This methods changes the value of a field in an existing row.
 String putValue(ArrayList<String> row, String field, String value)
          This methods changes the value of a field in an existing row.
 boolean putValues(ArrayList<String> values)
          This methods adds a new 'line' of values to the class, in case the expandable flag is set to true.
 ArrayList<Integer> rowIndexes(ArrayList<String> row)
          The method returns the indexes where row is present in the database.
 
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

FstabSettings

public FstabSettings(ArrayList<String> names,
                     ArrayList<ArrayList<String>> values,
                     ArrayList<String> defaults,
                     ArrayList<String> laws,
                     boolean expandable)
The constructor that populates the values, laws and sets the expandable flag

Parameters:
names - An ArrayList of String values, identifying the names of the fields
values - An ArrayList of ArrayList of String values, with the rows as rows and the fields as columns
defaults - An ArrayList of Strings with the default values for the respective field
laws - An ArrayList of Strings with the regular expresions for checking the values
expandable - The boolean flag that lets the values expand after initialization

FstabSettings

public FstabSettings(ArrayList<String> names,
                     ArrayList<ArrayList<String>> values,
                     ArrayList<String> defaults,
                     ArrayList<String> laws)
The constructor that populates the names, values, defaults and the laws. By default the expandable flag will be false.

Parameters:
names - An ArrayList of String values, identifying the names of the fields
values - An ArrayList of ArrayList of String values, with the rows as rows and the fields as columns
defaults - An ArrayList of Strings with the default values for the respective field
laws - An ArrayList of Strings with the regular expresions for checking the values

FstabSettings

public FstabSettings(ArrayList<String> names,
                     ArrayList<String> defaults,
                     ArrayList<String> laws)
The constructor that populates the names, defaults and laws. By default in this case the expandable flag will be true.

Parameters:
names - An ArrayList of String values, identifying the names of the fields.
defaults - An ArrayList of Strings with the default values for the respective field.
laws - An ArrayList of Strings with the regular expresions for checking the values.

FstabSettings

public FstabSettings(ArrayList<String> names,
                     ArrayList<String> defaults)
The constructor that only declares the field names and the defaults. By default in this case the expandable flag will be true

Parameters:
names - An ArrayList of String values, identifying the names of the fields
defaults - An ArrayList of Strings with the default values for the respective field

FstabSettings

public FstabSettings(ArrayList<String> names)
The constructor that only declares the field names. By default in this case the expandable flag will be true

Parameters:
names - The names of the fields

FstabSettings

public FstabSettings()
The default constructor - in this case the expandable flag will be true and the members will be initialized.

Method Detail

fieldIndex

public int fieldIndex(String field)
The method returns the index of the field

Parameters:
field - The name of the field
Returns:
The index of the field; -1 if the field does not exist

fieldName

public String fieldName(int index)
The method returns the name of the field for a known index

Parameters:
index - The index of the desired field
Returns:
The name of the field

putValues

public boolean putValues(ArrayList<String> values)
This methods adds a new 'line' of values to the class, in case the expandable flag is set to true. It also checks the values against the regular expressions. Practically it adds an element in each ArrayList in values

Parameters:
values - The array of Strings that represent a line of settings in 'fstab' format
Returns:
true if the operation succeded; false if not

rowIndexes

public ArrayList<Integer> rowIndexes(ArrayList<String> row)
The method returns the indexes where row is present in the database.

Parameters:
row - The sought row
Returns:
An ArrayList containing the indexes of all the occurances of row in the current Fstab object.

putValue

public String putValue(ArrayList<String> row,
                       String field,
                       String value)
This methods changes the value of a field in an existing row.

Parameters:
row - The index of the row
field - The name of the field
value - The new value
Returns:
The old value if the operation is successful or null if the operation fails

putValue

public String putValue(ArrayList<String> row,
                       int field,
                       String value)
This methods changes the value of a field in an existing row.

Parameters:
row - The index of the row
field - The index of the field
value - The new value
Returns:
The old value if the operation is successful or null if the operation fails

getRows

public ArrayList<ArrayList<String>> getRows(ArrayList<Integer> rows)
This method returns the rows corresponding to the requested indexes. For the example in the beginning, if invoked as getRow({0,1}) - yes, I am aware it's not correct, it's just an example - the result would be an ArrayList(2), with

ArrayList(0) = ArrayList({Value1-1,Value1-2,Value1-3})

ArrayList(1) = ArrayList({Value2-1,Value2-2,Value2-3})

Parameters:
rows - The ArrayList containing the index of the requested row
Returns:
An ArrayList of String ArrayLists containing the values in the requested row

getRows

public ArrayList<ArrayList<String>> getRows()
This method returns all the rows in the current object.

Returns:
An ArrayList of String ArrayLists containing the values in all the rows

getIndexesForValue

public ArrayList<Integer> getIndexesForValue(String value,
                                             int field)
This method returns the indexes of the rows containing the requested value in the specified field.

Parameters:
value - The requested value
field - The index of the field
Returns:
An int ArrayList containing the indexes where the sought value was found

getIndexesForValue

public ArrayList<Integer> getIndexesForValue(String value,
                                             String field)
This method returns the indexes of the rows corresponding to the requested value in the specified field

Parameters:
value - The requested value
field - The name of the field
Returns:
An int array containing the indexes where the sought value was found

getRowsForValue

public ArrayList<ArrayList<String>> getRowsForValue(String value,
                                                    int field)
This method returns the row corresponding to the requested value in the specified field

Parameters:
value - The requested value
field - The index of the field
Returns:
An ArrayList containing ArrayList

getRowsForValue

public ArrayList<ArrayList<String>> getRowsForValue(String value,
                                                    String field)
This method returns the row corresponding to the requested value in the specified field

Parameters:
value - The requested value
field - The name of the field
Returns:
A string array containing the values in the requested row

getValuesForValue

public ArrayList<String> getValuesForValue(String value_known,
                                           int field_known,
                                           int field_unknown)
This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.

Parameters:
value_known - The value that is known
field_known - The field in which the known value is present
field_unknown - The field in which the requested value is present
Returns:
The ArrayList containing the values from the unkown field and from the same rows as the known value (for the case there are several occurances of the known value).

getValuesForValue

public ArrayList<String> getValuesForValue(String value_known,
                                           String field_known,
                                           int field_unknown)
This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.

Parameters:
value_known - The value that is known
field_known - The field in which the known value is present
field_unknown - The field in which the requested value is present
Returns:
The ArrayList containing the values from the unkown field and from the same rows as the known value (for the case there are several occurances of the known value).

getValuesForValue

public ArrayList<String> getValuesForValue(String value_known,
                                           int field_known,
                                           String field_unknown)
This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.

Parameters:
value_known - The value that is known
field_known - The field in which the known value is present
field_unknown - The field in which the requested value is present
Returns:
The ArrayList containing the values from the unkown field and from the same rows as the known value (for the case there are several occurances of the known value).

getValuesForValue

public ArrayList<String> getValuesForValue(String value_known,
                                           String field_known,
                                           String field_unknown)
This metod returns the values, when another value from the same rows (in case of multiple occurances of the known value) is known.

Parameters:
value_known - The value that is known
field_known - The field in which the known value is present
field_unknown - The field in which the requested value is present
Returns:
The ArrayList containing the values from the unkown field and from the same rows as the known value (for the case there are several occurances of the known value).

deleteRow

public boolean deleteRow(ArrayList<String> row)
This method deletes one whole row of settings.

Parameters:
row - The row to delete.
Returns:
True if delete successful (if i is smaller than the total number of rows)

getRowsNumber

public int getRowsNumber()
The method returns the number of rows stored

Returns:
The number of rows stored as int.

containsField

public boolean containsField(String field)
The method returns true if the object contains a specific field

Parameters:
field - The name of the field as String.
Returns:
true if the object contains a specific field

isExpandable

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

Returns:
true if the object is expandable.