public class TypedProperties extends HashMap<String,Object>
Map
that publishes a set of typed properties, defined by
zero or more Reader
s containing textual definitions and assignments.Modifier and Type | Class and Description |
---|---|
static class |
TypedProperties.ParseException
An unchecked exception that is thrown when encountering a syntax
or semantic error in the input.
|
static class |
TypedProperties.TypeException
An unchecked exception that is thrown if a
get<TYPE>() method
is used to retrieve a parameter whose type does not match the method name. |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
static int |
STRING_NOT_SET |
static int |
STRING_NULL |
static int |
STRING_SET |
static int |
STRING_TYPE_MISMATCH |
Constructor and Description |
---|
TypedProperties()
Creates an empty TypedProperties instance.
|
Modifier and Type | Method and Description |
---|---|
Object |
get(Object key)
Returns the value to which the specified key is mapped,
or
null if this map contains no mapping for the key. |
boolean |
getBoolean(String property)
Returns the value of a boolean property, or false
if the property has not been defined.
|
boolean |
getBoolean(String property,
boolean def)
Returns the value of a boolean property, or the default if the property
has not been defined.
|
byte |
getByte(String property)
Returns the value of a byte property, or 0
if the property has not been defined.
|
byte |
getByte(String property,
byte def)
Returns the value of a byte property, or the default if the property
has not been defined.
|
double |
getDouble(String property)
Returns the value of a double property, or 0.0
if the property has not been defined.
|
double |
getDouble(String property,
double def)
Returns the value of a double property, or the default if the property
has not been defined.
|
float |
getFloat(String property)
Returns the value of a float property, or 0.0
if the property has not been defined.
|
float |
getFloat(String property,
float def)
Returns the value of a float property, or the default if the property
has not been defined.
|
int |
getInt(String property)
Returns the value of an integer property, or 0
if the property has not been defined.
|
int |
getInt(String property,
int def)
Returns the value of an integer property, or the default if the property
has not been defined.
|
long |
getLong(String property)
Returns the value of a long property, or 0
if the property has not been defined.
|
long |
getLong(String property,
long def)
Returns the value of a long property, or the default if the property
has not been defined.
|
short |
getShort(String property)
Returns the value of a short property, or 0
if the property has not been defined.
|
short |
getShort(String property,
short def)
Returns the value of a short property, or the default if the property
has not been defined.
|
String |
getString(String property)
Returns the value of a String property, or ""
if the property has not been defined.
|
String |
getString(String property,
String def)
Returns the value of a string property, or the default if the property
has not been defined.
|
int |
getStringInfo(String property)
Provides string type information about a property.
|
void |
load(Reader r)
Loads zero or more properties from the specified Reader.
|
clear, clone, containsKey, containsValue, entrySet, forEach, isEmpty, keySet, put, putAll, remove, replace, replaceAll, size, values
equals, hashCode, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, merge, putIfAbsent, remove, replace
public static final int STRING_TYPE_MISMATCH
public static final int STRING_NOT_SET
public static final int STRING_NULL
public static final int STRING_SET
public TypedProperties()
public void load(Reader r) throws IOException
File syntax:
<type> <property-name> = <value> ;
unset ( <property-name> ) ;"//" comments everything until the end of the line. "/a;" comments everything until the next appearance of "a;/".
Blank lines are ignored.
The only required whitespace is between the type and the property name.
<type> is one of {boolean, byte, short, int, long, float, double, String}, and is case-sensitive.
<property-name> is a valid fully-qualified class name (one or more valid identifiers separated by dot characters).
<value> depends on the type:
- boolean: one of {true, false} (case-sensitive)
- byte, short, int, long: a valid Java integer constant (including non-base-10 constants like 0xabc and 074) whose value does not overflow the type. NOTE: these are interpreted as Java integer values, so they are all signed.
- float, double: a valid Java floating-point constant. If the type is float, the value must fit in 32 bits.
- String: a double-quoted string value, or the word
null
. NOTE: the contents of the string must be 7-bit clean ASCII; C-style octal escapes are recognized, but Unicode escapes are not.Passing a property-name to
unset()
will unset the property, removing its value and type information, as if it had never been defined.
r
- The Reader to load properties fromIOException
- if an error occurs when reading the dataIllegalArgumentException
- if the data is malformedpublic Object get(Object key)
HashMap
null
if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k
to a value v
such that (key==null ? k==null :
key.equals(k))
, then this method returns v
; otherwise
it returns null
. (There can be at most one such mapping.)
A return value of null
does not necessarily
indicate that the map contains no mapping for the key; it's also
possible that the map explicitly maps the key to null
.
The containsKey
operation may be used to
distinguish these two cases.
get
in interface Map<String,Object>
get
in class HashMap<String,Object>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyHashMap.put(Object, Object)
public boolean getBoolean(String property, boolean def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a booleanpublic byte getByte(String property, byte def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a bytepublic short getShort(String property, short def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a shortpublic int getInt(String property, int def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not an integerpublic long getLong(String property, long def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a longpublic float getFloat(String property, float def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a floatpublic double getDouble(String property, double def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a doublepublic String getString(String property, String def)
property
- The name of the property to returndef
- The default value to return if the property is not setTypedProperties.TypeException
- if the property is set and is not a stringpublic boolean getBoolean(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a booleanpublic byte getByte(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a bytepublic short getShort(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a shortpublic int getInt(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not an integerpublic long getLong(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a longpublic float getFloat(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a floatpublic double getDouble(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a doublepublic String getString(String property)
property
- The name of the property to returnTypedProperties.TypeException
- if the property is set and is not a stringpublic int getStringInfo(String property)
property
- the property to check