Package openbook.util
Class PropertyHelper
- java.lang.Object
-
- openbook.util.PropertyHelper
-
public class PropertyHelper extends java.lang.ObjectA set of static utility functions to read properties from file, manage properties with multi-part keys, array style properties etc.- Author:
- Pinaki Poddar
-
-
Constructor Summary
Constructors Constructor Description PropertyHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanFind(java.lang.String resource)Affirm if the given resource is available either as a resource in the current thread's context classpath or as a file.static java.util.Map<java.lang.String,java.lang.Object>filter(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String name, boolean prefix, boolean includeArrays)Filter the properties by the given name.static java.util.Map<java.lang.String,java.util.List<java.lang.Object>>filterArrayKeys(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String name, boolean prefix)Select only those property keys which ends with an array marker such asopenjpa.DataCache[1].static doublegetDouble(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, double def)static intgetInteger(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, int def)static java.util.Map<java.lang.String,java.lang.String>getMap(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key)static java.util.Map<java.lang.String,java.lang.String>getMap(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.util.Map<java.lang.String,java.lang.String> def)static java.lang.StringgetPrefix(java.lang.String s)static java.util.Map<java.lang.String,java.lang.Object>getSection(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String section)Get the portion of the given map whose key has the given section at prefix.static java.util.Map<java.lang.String,java.lang.Object>getSection(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String section, boolean retain)Get the portion of the given map whose key has the given section at prefix.static java.lang.StringgetString(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.lang.String def)static java.util.List<java.lang.String>getStringList(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key)static java.util.List<java.lang.String>getStringList(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.util.List<java.lang.String> def)static java.util.Set<java.lang.String>getSubsectionKeys(java.util.Set<java.lang.String> keys, java.lang.String section)static java.util.Map<java.lang.String,java.lang.Object>load(java.lang.String resource)Load properties from the given name resource.static java.util.Map<java.lang.String,java.lang.Object>overwriteWithSystemProperties(java.util.Map<java.lang.String,java.lang.Object> original)Overwrites any key-value pair in the given map for which a System property is availablestatic java.util.Map<java.lang.String,java.lang.Object>toMap(java.util.Properties p)
-
-
-
Method Detail
-
filter
public static java.util.Map<java.lang.String,java.lang.Object> filter(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String name, boolean prefix, boolean includeArrays)Filter the properties by the given name.- Parameters:
name- a part of the keyprefix- if true, property key must begin with the given name. Otherwise, the key merely contains the name to qualify.- Returns:
- key-value pairs that match.
-
filterArrayKeys
public static java.util.Map<java.lang.String,java.util.List<java.lang.Object>> filterArrayKeys(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String name, boolean prefix)Select only those property keys which ends with an array marker such asopenjpa.DataCache[1]. The multiple values of the property is inserted into the resultant map as a List of object against the original key.
For example, if the original map had three key-value pairs asopenjpa.DataCache[1]=trueopenjpa.DataCache[2]=falseopenjpa.DataCache[3]=default
Then that will result into a single entry in the resultant Map under the keyopenjpa.DataCachewith a value as a List of three elements namely{true, false, default}. The array index values are not significant other than they must all be different for the same base key.- Parameters:
name- part of the property keyprefix- does the name must appear as a prefix?- Returns:
- key-value pairs that match.
-
load
public static java.util.Map<java.lang.String,java.lang.Object> load(java.lang.String resource)
Load properties from the given name resource. The given named resource is first looked up as resource on the current thread's context and if not found as a file input.- Parameters:
resource- name a of resource.- Returns:
- empty properties if no resource found.
-
canFind
public static boolean canFind(java.lang.String resource)
Affirm if the given resource is available either as a resource in the current thread's context classpath or as a file.
-
toMap
public static java.util.Map<java.lang.String,java.lang.Object> toMap(java.util.Properties p)
-
overwriteWithSystemProperties
public static java.util.Map<java.lang.String,java.lang.Object> overwriteWithSystemProperties(java.util.Map<java.lang.String,java.lang.Object> original)
Overwrites any key-value pair in the given map for which a System property is available- Parameters:
original- properties to be overwritten- Returns:
- the original property overwritten with System properties
-
getInteger
public static int getInteger(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, int def)
-
getDouble
public static double getDouble(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, double def)
-
getString
public static java.lang.String getString(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.lang.String def)
-
getStringList
public static java.util.List<java.lang.String> getStringList(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key)
-
getStringList
public static java.util.List<java.lang.String> getStringList(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.util.List<java.lang.String> def)
-
getMap
public static java.util.Map<java.lang.String,java.lang.String> getMap(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key)
-
getMap
public static java.util.Map<java.lang.String,java.lang.String> getMap(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String key, java.util.Map<java.lang.String,java.lang.String> def)
-
getSubsectionKeys
public static java.util.Set<java.lang.String> getSubsectionKeys(java.util.Set<java.lang.String> keys, java.lang.String section)
-
getPrefix
public static java.lang.String getPrefix(java.lang.String s)
-
getSection
public static java.util.Map<java.lang.String,java.lang.Object> getSection(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String section)Get the portion of the given map whose key has the given section at prefix.- Parameters:
props- a set of name-value pairsection- a string representing section of a key- Returns:
- a new map with only the keys that starts with the given section.
-
getSection
public static java.util.Map<java.lang.String,java.lang.Object> getSection(java.util.Map<java.lang.String,java.lang.Object> props, java.lang.String section, boolean retain)Get the portion of the given map whose key has the given section at prefix.- Parameters:
props- a set of name-value pairsection- a string representing section of a keyretain- if true the key of resultant map is same as the original map. Otherwise the resultant map keys are without the section prefix.- Returns:
- the map with only the keys that starts with the given section.
-
-