Class PropertiesManager
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<String,
PropertyValidator> private FileReader
private final String
private final Properties
-
Constructor Summary
ConstructorsConstructorDescriptionPropertiesManager
(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. -
Method Summary
Modifier and TypeMethodDescriptionboolean
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.void
Checks if the file has all the necessary properties' keys.boolean
Returns (true || false) based ont he existence of a key in the properties file.private void
Writes to the file with the default values of all the properties.Attempts to expose the underling properties object that holds the access points to the main properties config file.Returns the value of the key in the properties file.Returns the Map of default properties provided in the constructor.Returns the current referenced properties file.boolean
open()
Opens the properties file for editing and consulting.void
save()
Save with no comments parametervoid
Saves the properties file with comments.void
Sets the value of the key in the properties file.void
setDefaultProperties
(Map<String, String> map) UNSAFE: Resets the default properties to something else.private void
Creates an empty file with the location given in the constructor.
-
Field Details
-
allowedProperties
-
util
-
location
-
map
-
fr
-
-
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
Returns the Map of default properties provided in the constructor.- Returns:
- The Map of default properties provided in the constructor.
-
setDefaultProperties
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
Returns the current referenced properties file.- Returns:
- The current referenced properties file.
-
contains
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
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 forvalue
- The value to check for- Returns:
- (true | | false) based the allowance of the value upon the key
-
get
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
Sets the value of the key in the properties file.- Parameters:
key
- The key to set the value ofvalue
- The value to set the key tocomments
- The comments to add to the file
-
save
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
Attempts to expose the underling properties object that holds the access points to the main properties config file.- Returns:
- A Properties object (value copy)
-