Class JKUserPreferences
This class provides methods to read and write preferences, including basic types such as strings, booleans, floats, integers, and byte arrays.
- Version:
- 1.0
- Author:
- Dr. Jalal H. Kiswani
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
clear()
This method clears all preferences stored using the Java Preferences API.protected static String
This method fixes the provided preference key by appending the key prefix if it doesn't already start with it.static String
This method retrieves the value associated with the specified preference key.static boolean
getBoolean
(String key, boolean def) This method retrieves the boolean value associated with the specified preference key.byte[]
getByteArray
(String key, byte[] def) This method retrieves the byte array value associated with the specified key.static float
This method retrieves the float value associated with the specified preference key.getHashtable
(String name) This method retrieves a Hash table of key-value pairs associated with the specified preference name.static int
This method retrieves an integer value associated with the specified preference key.static String
This method retrieves the prefix used for preference keys.static String[]
keys()
This method retrieves an array of all preference keys stored in the user preferences.static void
This method stores a string value in the user preferences under the specified key.static void
putBoolean
(String key, boolean value) This method stores a boolean value in the user preferences under the specified key.void
putByteArray
(String key, byte[] value) This method associates the specified byte array value with the given key in the user preferences.static void
This method stores a floating-point value in the user preferences under the specified key.static void
putHashTable
(String name, Hashtable hash) This method stores a Hash table of key-value pairs in the user preferences under the specified name.static void
This method stores an integer value in the user preferences under the specified key.static void
setKeyPrefix
(String keyPrefix) This method sets the key prefix used for storing preferences.
-
Constructor Details
-
JKUserPreferences
public JKUserPreferences()
-
-
Method Details
-
clear
This method clears all preferences stored using the Java Preferences API.- Throws:
BackingStoreException
- if the preferences could not be cleared.
-
fixKey
This method fixes the provided preference key by appending the key prefix if it doesn't already start with it.This method ensures that the preference key is properly formatted to avoid conflicts and follows the application's naming convention.
- Parameters:
key
- Specifies the preference key to be fixed.- Returns:
- the fixed preference key with the appropriate prefix.
-
get
This method retrieves the value associated with the specified preference key.If the key is not found or an exception occurs during retrieval, the default value provided will be returned.
- Parameters:
key
- Specifies the preference key to retrieve the value for.def
- Specifies the default value to return if the key is not found or retrieval fails.- Returns:
- the value associated with the preference key, or the default value if not found.
-
getBoolean
This method retrieves the boolean value associated with the specified preference key.If the key is not found or an exception occurs during retrieval, the default value provided will be returned.
- Parameters:
key
- Specifies the preference key to retrieve the boolean value for.def
- Specifies the default boolean value to return if the key is not found or retrieval fails.- Returns:
- the boolean value associated with the preference key, or the default value if not found.
-
getFloat
This method retrieves the float value associated with the specified preference key.If the key is not found or an exception occurs during retrieval, the default value provided will be returned.
- Parameters:
key
- Specifies the preference key to retrieve the float value for.def
- Specifies the default float value to return if the key is not found or retrieval fails.- Returns:
- the float value associated with the preference key, or the default value if not found.
-
getHashtable
This method retrieves a Hash table of key-value pairs associated with the specified preference name.The method parses the stored configuration string and populates the Hash table accordingly.
If the preference name is not found or an exception occurs during parsing, an empty Hash table is returned.
- Parameters:
name
- Specifies the name of the preference containing the stored configuration string.- Returns:
- a Hash table containing key-value pairs from the stored configuration, or an empty Hash table if not found or parsing fails.
-
getInt
This method retrieves an integer value associated with the specified preference key.If the preference key is not found or an exception occurs during retrieval, the default value is returned.
- Parameters:
key
- Specifies the key of the preference for which the integer value is to be retrieved.def
- Specifies the default integer value to be returned if the key is not found or retrieval fails.- Returns:
- the retrieved integer value or the default value if the key is not found or retrieval fails.
-
getKeyPrefix
This method retrieves the prefix used for preference keys.- Returns:
- the prefix used for preference keys.
-
keys
This method retrieves an array of all preference keys stored in the user preferences.- Returns:
- an array of preference keys.
- Throws:
BackingStoreException
- if an error occurs while accessing the preferences.
-
put
This method stores a string value in the user preferences under the specified key.- Parameters:
key
- Specifies the key under which the value will be stored.value
- Specifies the string value to be stored.
-
putBoolean
This method stores a boolean value in the user preferences under the specified key.- Parameters:
key
- Specifies the key under which the value will be stored.value
- Specifies the boolean value to be stored.
-
putFloat
This method stores a floating-point value in the user preferences under the specified key.- Parameters:
key
- Specifies the key under which the value will be stored.value
- Specifies the floating-point value to be stored.
-
putHashTable
This method stores a Hash table of key-value pairs in the user preferences under the specified name.- Parameters:
name
- Specifies the name under which the Hash table will be stored.hash
- Specifies the Hash table containing key-value pairs to be stored.
-
putInt
This method stores an integer value in the user preferences under the specified key.- Parameters:
key
- Specifies the key under which the integer value will be stored.value
- Specifies the integer value to be stored.
-
setKeyPrefix
This method sets the key prefix used for storing preferences.- Parameters:
keyPrefix
- Specifies the new key prefix to be used.
-
getByteArray
This method retrieves the byte array value associated with the specified key.- Parameters:
key
- Specifies the key for which to retrieve the byte array value.def
- Specifies the default byte array value to return if the key is not found.- Returns:
- the byte array value associated with the key, or the default value if not found.
-
putByteArray
This method associates the specified byte array value with the given key in the user preferences.- Parameters:
key
- Specifies the key with which the byte array value is to be associated.value
- Specifies the byte array value to be associated with the key.
-