Class Configuration
- java.lang.Object
-
- com.gurock.smartinspect.Configuration
-
public class Configuration extends Object
This class is responsible for handling the SmartInspect configuration and loading it from a file. It is primarily used for loading and reading values from a SmartInspect configuration file. For more information, please refer to the documentation of the SmartInspect.loadConfiguration method.Note: This class is not guaranteed to be thread-safe.
-
-
Constructor Summary
Constructors Constructor Description Configuration()Creates and initializes a Configuration instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all key/value pairs of the configuration.booleancontains(String key)Tests if the configuration contains a value for a given key.intgetCount()Returns the number of key/value pairs of this SmartInspect configuration.voidloadFromFile(String fileName)Loads the configuration from a file.booleanreadBoolean(String key, boolean defaultValue)Returns either the value converted to a boolean for the given key if an element with the given key exists or defaultValue otherwise.ColorreadColor(String key, Color defaultValue)Returns a Color value of an element for a given key.intreadInteger(String key, int defaultValue)Returns an int value of an element for a given key.StringreadKey(int index)Returns a key of this SmartInspect configuration for a given index.LevelreadLevel(String key, Level defaultValue)Returns a Level value of an element for a given key.StringreadString(String key, String defaultValue)Returns a String value of an element for a given key.
-
-
-
Method Detail
-
loadFromFile
public void loadFromFile(String fileName) throws IOException
Loads the configuration from a file.This method loads key/value pairs separated with a '=' character from a file. Empty, unrecognized lines or lines beginning with a ';' character are ignored.
- Parameters:
fileName- The name of the file to load the configuration from.- Throws:
IOException- If an I/O error occurred while trying to load the configuration or if the specified file does not exist.NullPointerException- If the fileName argument is null.
-
contains
public boolean contains(String key)
Tests if the configuration contains a value for a given key.- Parameters:
key- The key to test for.- Returns:
- True if a value exists for the given key and false otherwise.
-
clear
public void clear()
Removes all key/value pairs of the configuration.
-
getCount
public int getCount()
Returns the number of key/value pairs of this SmartInspect configuration.- Returns:
- The number of key/value pairs of this SmartInspect configuration.
-
readString
public String readString(String key, String defaultValue)
Returns a String value of an element for a given key.- Parameters:
key- The key whose value to return.defaultValue- The value to return if the given key is unknown.- Returns:
- Either the value for a given key if an element with the given key exists or defaultValue otherwise.
- Throws:
NullPointerException- If the key argument is null.
-
readBoolean
public boolean readBoolean(String key, boolean defaultValue)
Returns either the value converted to a boolean for the given key if an element with the given key exists or defaultValue otherwise. This method returns a boolean value of true if the found value of the given key matches either "true", "1" or "yes" and false otherwise. If the supplied key is unknown, the defaultValue argument is returned.- Parameters:
key- The key whose value to return.defaultValue- The value to return if the given key is unknown.- Returns:
- Either value converted to a boolean for the given key if an element with the given key exists or defaultValue otherwise.
- Throws:
NullPointerException- If the key argument is null.
-
readInteger
public int readInteger(String key, int defaultValue)
Returns an int value of an element for a given key.- Parameters:
key- The key whose value to return.defaultValue- The value to return if the given key is unknown.- Returns:
- Either the value converted to an int for the given key if an
element with the given key exists and the found value is a
valid int or defaultValue otherwise.
This method returns the defaultValue argument if either the supplied key is unknown or the found value is not a valid int. Only non-negative int values are recognized as valid.
- Throws:
NullPointerException- If the key argument is null.
-
readLevel
public Level readLevel(String key, Level defaultValue)
Returns a Level value of an element for a given key.- Parameters:
key- The key whose value to return.defaultValue- The value to return if the given key is unknown.- Returns:
- Either the value converted to the corresponding Level value for the given key if an element with the given key exists and the found value is a valid Level value or defaultValue otherwise. Note: This method returns the defaultValue argument if either the supplied key is unknown or the found value is not a valid Level value. Please see the Level enum for more information on the available values.
- Throws:
NullPointerException- If the key argument is null.
-
readColor
public Color readColor(String key, Color defaultValue)
Returns a Color value of an element for a given key.This method requires the element value to be specified as a hexadecimal string. To indicate that the element value represents a hexadecimal string, the element value must begin with "0x", "&H" or "$". A '0' nibble is appended if the hexadecimal string has an odd length.
The hexadecimal value must represent a three or four byte integer value. The hexadecimal values are handled as follows:
- 3 bytes: RRGGBB
- 4 bytes: AARRGGBB
- Other: Ignored
A stands for the alpha channel and R, G, and B represent the red, green and blue channels, respectively. If the value is not given as hexadecimal value with a length of 6 or 8 characters excluding the hexadecimal prefix identifier or if the value does not have a valid hexadecimal format, this method returns defaultValue.
- Parameters:
key- The key whose value to return.defaultValue- The value to return if the given key is unknown or if the found value has an invalid format.- Returns:
- Either the value converted to a Color value for the given key if an element with the given key exists and the found value has a valid format or defaultValue otherwise.
- Throws:
NullPointerException- if the key argument is null.
-
readKey
public String readKey(int index)
Returns a key of this SmartInspect configuration for a given index.To find out the total number of key/value pairs in this SmartInspect configuration, use
getCount(). To get the value for a given key, usereadString(String key, String defaultValue).- Parameters:
index- The index in this SmartInspect configuration.- Returns:
- A key of this SmartInspect configuration for the given index.
- Throws:
IndexOutOfBoundsException- if the index argument is not a valid index of this SmartInspect configuration.
-
-