Package tv.phantombot

Class CaselessProperties

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>
Direct Known Subclasses:
CaselessCommandLineArguments

public class CaselessProperties extends Properties
Provides an implementation of Properties where the key is case-insensitive and thread-safe

Provides methods which allow casting values as a char, int, long, double, or boolean

Provides the ability to perform a thread-safe update of multiple values as a transaction

Author:
gmt2001
See Also:
  • Field Details

  • Constructor Details

    • CaselessProperties

      public CaselessProperties()
  • Method Details

    • instance

      public static CaselessProperties instance()
    • put

      public Object put(Object key, Object value)
      Specified by:
      put in interface Map<Object,Object>
      Overrides:
      put in class Properties
    • getProperty

      public String getProperty(String key)
      Overrides:
      getProperty in class Properties
    • getProperty

      public String getProperty(String key, String defaultValue)
      Overrides:
      getProperty in class Properties
    • getPropertyAsChar

      public char getPropertyAsChar(String key) throws IndexOutOfBoundsException
      Returns the specified property as a char
      Parameters:
      key - the hashtable key
      Returns:
      the first char of the value returned by getProperty(String)
      Throws:
      IndexOutOfBoundsException - if the property does not exist or is empty
    • getPropertyAsChar

      public char getPropertyAsChar(String key, char defaultValue)
      Returns the specified property as a char
      Parameters:
      key - the hashtable key
      defaultValue - a default value
      Returns:
      the first char of the value returned by getProperty(String); defaultValue if the property does not exist or is empty
    • getPropertyAsInt

      public int getPropertyAsInt(String key) throws NumberFormatException
      Returns the specified property as an int
      Parameters:
      key - the hashtable key
      Returns:
      the value returned by getProperty(String) cast as an int
      Throws:
      NumberFormatException - if the property does not exist or is empty
    • getPropertyAsInt

      public int getPropertyAsInt(String key, int defaultValue)
      Returns the specified property as an int
      Parameters:
      key - the hashtable key
      defaultValue - a default value
      Returns:
      the value returned by getProperty(String) cast as an int; defaultValue if the property does not exist or is not parsable as an int
    • getPropertyAsLong

      public long getPropertyAsLong(String key) throws NumberFormatException
      Returns the specified property as a long
      Parameters:
      key - the hashtable key
      Returns:
      the value returned by getProperty(String) cast as a long
      Throws:
      NumberFormatException - if the property does not exist or is empty
    • getPropertyAsLong

      public long getPropertyAsLong(String key, long defaultValue)
      Returns the specified property as a long
      Parameters:
      key - the hashtable key
      defaultValue - a default value
      Returns:
      the value returned by getProperty(String) cast as a long; defaultValue if the property does not exist or is not parsable as a long
    • getPropertyAsDouble

      public double getPropertyAsDouble(String key) throws NumberFormatException
      Returns the specified property as a double
      Parameters:
      key - the hashtable key
      Returns:
      the value returned by getProperty(String) cast as a double
      Throws:
      NumberFormatException - if the property does not exist or is empty
    • getPropertyAsDouble

      public double getPropertyAsDouble(String key, double defaultValue)
      Returns the specified property as a double
      Parameters:
      key - the hashtable key
      defaultValue - a default value
      Returns:
      the value returned by getProperty(String) cast as a double; defaultValue if the property does not exist or is not parsable as a double
    • getPropertyAsBoolean

      public boolean getPropertyAsBoolean(String key)
      Retruns the specified property as a boolean

      Accepted truthy values:

      • 1
      • true
      • yes
      Parameters:
      key - the hashtable key
      Returns:
      the value returned by getProperty(String) cast as a boolean; false if the property does not exist or does not match an accepted truthy value
    • getPropertyAsBoolean

      public boolean getPropertyAsBoolean(String key, boolean defaultValue)
      Retruns the specified property as a boolean

      Accepted truthy values:

      • 1
      • true
      • yes
      Parameters:
      key - the hashtable key
      Returns:
      the value returned by getProperty(String) cast as a boolean; defaultValue if the property does not exist; false if the value does not match an accepted truthy value
    • putIfAbsent

      public Object putIfAbsent(String key, Object value)
      Searches for the property with the specified key in this property list. If the property is found, returns the current value; otherwise, adds the property to the property list
      Parameters:
      key - the property key
      value - the value to put if the property key does not exist
      Returns:
      the current value if key already exists; else null
    • keySet

      public Set<Object> keySet()
      Specified by:
      keySet in interface Map<Object,Object>
      Overrides:
      keySet in class Properties
    • entrySet

      public Set<Map.Entry<Object,Object>> entrySet()
      Specified by:
      entrySet in interface Map<Object,Object>
      Overrides:
      entrySet in class Properties
    • keys

      public Enumeration<Object> keys()
      Overrides:
      keys in class Properties
    • startTransaction

      public CaselessProperties.Transaction startTransaction()
      Starts a transaction for updating multiple values simultaneously at CaselessProperties.Transaction.PRIORITY_NORMAL
      Returns:
      a new CaselessProperties.Transaction
    • startTransaction

      public CaselessProperties.Transaction startTransaction(int priority)
      Starts a transaction for updating multiple values simultaneously

      If multiple transactions editing the same values are comitted within 15 seconds of each other, the transaction with the highest priority wins. Transactions with the same priority are first-come first-served

      Parameters:
      priority - the priority level, between 0 and 100
      Returns:
      a new CaselessProperties.Transaction
    • store

      public void store()
      Saves the current property set, then reloads the current state
    • store

      public void store(boolean reload)
      Saves the current property set
      Parameters:
      reload - if true, the state is also reloaded and PropertiesReloadedEvent is fired