Class PropertiesManager

java.lang.Object
com.jackmeng.halcyoninae.halcyon.utils.PropertiesManager

public final class PropertiesManager extends Object
This class is localized meaning that one should not use it for anything besides this program.

This program provides a simple Replace On Demand properties reader, instead of the default Properties class.

A Replace On Demand means that it will not let the the receiver decide what to do with the properties, but will go by rules. If a value is deemed unacceptable, it will be be replaced by the default value, and the original value in the file will be overwritten. Another thing to note is that, on demand means only checking the current value it is being called upon get(String). This class does not do a pre-check of all of the properties, unless the properties file does not exist or is empty, then everything will be replaced createWithDefaultVals().

Since:
2.1
Author:
Jack Meng
  • Field Details

  • Constructor Details

    • PropertiesManager

      public PropertiesManager(Map<String,String> defaultProperties, Map<String,PropertyValidator> allowedProperties, String location)
      Creates a new PropertiesManager instance with the defined rules and allowed-properties for the given properties file.
      Parameters:
      defaultProperties - Contains a key and a value with the value being the fallback value for that key if the key's value in the properties is not allowed. This is not an optional parameter to set as null or anything that makes it not have a value.
      allowedProperties - Contains a key and an array of allowed properties as rules, if the value from file's key does not match any of the given rules, the PropertiesManager will return the default property and alter the file. This is an optional parameter, which can be that the array can be empty (NOT NULL).
      location - The location of the properties file
  • Method Details

    • checkAllPropertiesExistence

      public void checkAllPropertiesExistence()
      Checks if the file has all the necessary properties' keys.

      As previously stated, this is an Replace On Demand method, meaning that this method does not care about the value of the properties, it only cares if the key exists.

      If a key does not exist, it will be created with the default value.

    • wipeContents

      private void wipeContents()
      Creates an empty file with the location given in the constructor.
    • createWithDefaultVals

      private void createWithDefaultVals()
      Writes to the file with the default values of all the properties.

      It will also first wipe the contents of the file in order to prevent overwrite.

    • getDefaultProperties

      public Map<String,String> getDefaultProperties()
      Returns the Map of default properties provided in the constructor.
      Returns:
      The Map of default properties provided in the constructor.
    • setDefaultProperties

      public void setDefaultProperties(Map<String,String> map)
      UNSAFE: Resets the default properties to something else.

      It is unadvised to use this method, as it does not do a pre-check of the properties of the file for all the properties after, forcing the receiver to check for all the properties.

      Parameters:
      map - The new default properties.
    • getLocation

      public String getLocation()
      Returns the current referenced properties file.
      Returns:
      The current referenced properties file.
    • contains

      public boolean contains(String key)
      Returns (true || false) based ont he existence of a key in the properties file.
      Parameters:
      key - A key to check for
      Returns:
      (true | | false) based on the existence of a key in the properties file.
    • allowed

      public boolean allowed(String key, String value)
      Consults if the given key and value are allowed to be paired or is allowed in general based on the rules provided in the constructor.

      If the allowed-properties' string array is empty, then it will always return true.

      Parameters:
      key - The key to check for
      value - The value to check for
      Returns:
      (true | | false) based the allowance of the value upon the key
    • get

      public String get(String key)
      Returns the value of the key in the properties file.
      Parameters:
      key - The key to get the value of
      Returns:
      The value of the key in the properties file.
    • set

      public void set(String key, String value, String comments)
      Sets the value of the key in the properties file.
      Parameters:
      key - The key to set the value of
      value - The value to set the key to
      comments - The comments to add to the file
    • save

      public void save(String comments)
      Saves the properties file with comments.
      Parameters:
      comments - The comments to add to the top of the file.
    • save

      public void save()
      Save with no comments parameter
    • open

      public boolean open()
      Opens the properties file for editing and consulting.
      Returns:
      (true | | false) based on if the file was able to be opened.
    • exposeProperties

      public Properties exposeProperties()
      Attempts to expose the underling properties object that holds the access points to the main properties config file.
      Returns:
      A Properties object (value copy)