Class JKUserPreferences

java.lang.Object
com.jk.core.util.JKUserPreferences

public class JKUserPreferences extends Object
This class is a utility class used for managing user preferences using the Java Preferences API.

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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    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
    get(String key, String def)
    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
    getFloat(String key, float def)
    This method retrieves the float value associated with the specified preference key.
    This method retrieves a Hash table of key-value pairs associated with the specified preference name.
    static int
    getInt(String key, int def)
    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[]
    This method retrieves an array of all preference keys stored in the user preferences.
    static void
    put(String key, String value)
    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
    putFloat(String key, float value)
    This method stores a floating-point value in the user preferences under the specified key.
    static void
    This method stores a Hash table of key-value pairs in the user preferences under the specified name.
    static void
    putInt(String key, int value)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JKUserPreferences

      public JKUserPreferences()
  • Method Details

    • clear

      public static void clear() throws BackingStoreException
      This method clears all preferences stored using the Java Preferences API.
      Throws:
      BackingStoreException - if the preferences could not be cleared.
    • fixKey

      protected static String fixKey(String key)
      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

      public static String get(String key, String def)
      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

      public static boolean getBoolean(String key, boolean def)
      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

      public static float getFloat(String key, float def)
      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

      public static Hashtable<String,String> getHashtable(String name)
      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

      public static int getInt(String key, int def)
      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

      public static String getKeyPrefix()
      This method retrieves the prefix used for preference keys.
      Returns:
      the prefix used for preference keys.
    • keys

      public static String[] keys() throws BackingStoreException
      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

      public static void put(String key, String value)
      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

      public static void putBoolean(String key, boolean value)
      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

      public static void putFloat(String key, float value)
      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

      public 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.
      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

      public static void putInt(String key, int value)
      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

      public static void setKeyPrefix(String keyPrefix)
      This method sets the key prefix used for storing preferences.
      Parameters:
      keyPrefix - Specifies the new key prefix to be used.
    • getByteArray

      public byte[] getByteArray(String key, byte[] def)
      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

      public void putByteArray(String key, byte[] value)
      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.