Package org.apache.openjpa.lib.util
Class Options
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<java.lang.Object,java.lang.Object>
-
- java.util.Properties
-
- org.apache.openjpa.lib.util.TypedProperties
-
- org.apache.openjpa.lib.util.Options
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<java.lang.Object,java.lang.Object>
public class Options extends TypedProperties
A specialization of theProperties
map type with the added abilities to read application options from the command line and to use bean patterns to set an object's properties via command-line the stored mappings. A typical use pattern for this class is to construct a new instance in themain
method, then callsetFromCmdLine(java.lang.String[])
with the given args. Next, an instanceof the class being invoked is created, andsetInto(java.lang.Object)
is called with that instance as a parameter. With this pattern, the user can configure any bean properties of the class, or even properties of classes reachable from the class, through the command line.- Author:
- Abe White
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.util.Collection<java.lang.String>
findOptionsFor(java.lang.Class<?> type)
Finds all the options that can be set on the provided class.boolean
getBooleanProperty(java.lang.String key, java.lang.String key2, boolean def)
Specialization ofgetBooleanProperty(java.lang.String, java.lang.String, boolean)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.double
getDoubleProperty(java.lang.String key, java.lang.String key2, double def)
Specialization ofTypedProperties.getDoubleProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.float
getFloatProperty(java.lang.String key, java.lang.String key2, float def)
Specialization ofTypedProperties.getFloatProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.int
getIntProperty(java.lang.String key, java.lang.String key2, int def)
Specialization ofTypedProperties.getIntProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.long
getLongProperty(java.lang.String key, java.lang.String key2, long def)
Specialization ofTypedProperties.getLongProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.java.lang.String
getProperty(java.lang.String key, java.lang.String key2, java.lang.String def)
Specialization ofProperties.getProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.boolean
removeBooleanProperty(java.lang.String key, java.lang.String key2, boolean def)
Specialization ofTypedProperties.removeBooleanProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.double
removeDoubleProperty(java.lang.String key, java.lang.String key2, double def)
Specialization ofTypedProperties.removeDoubleProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.float
removeFloatProperty(java.lang.String key, java.lang.String key2, float def)
Specialization ofTypedProperties.removeFloatProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.int
removeIntProperty(java.lang.String key, java.lang.String key2, int def)
Specialization ofTypedProperties.removeIntProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.long
removeLongProperty(java.lang.String key, java.lang.String key2, long def)
Specialization ofTypedProperties.removeLongProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.java.lang.String
removeProperty(java.lang.String key, java.lang.String key2, java.lang.String def)
Specialization ofProperties.remove(Object)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.java.lang.String[]
setFromCmdLine(java.lang.String[] args)
Parses the given argument list into flag/value pairs, which are stored as properties.Options
setInto(java.lang.Object obj)
This method uses reflection to set all the properties in the given object that are named by the keys in this map.-
Methods inherited from class org.apache.openjpa.lib.util.TypedProperties
getBooleanProperty, getBooleanProperty, getDoubleProperty, getDoubleProperty, getFloatProperty, getFloatProperty, getIntProperty, getIntProperty, getLongProperty, getLongProperty, removeBooleanProperty, removeBooleanProperty, removeDoubleProperty, removeDoubleProperty, removeFloatProperty, removeFloatProperty, removeIntProperty, removeIntProperty, removeLongProperty, removeLongProperty, removeProperty, removeProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty
-
Methods inherited from class java.util.Properties
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, getProperty, getProperty, hashCode, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values
-
-
-
-
Field Detail
-
EMPTY
public static Options EMPTY
Immutable empty instance.
-
-
Method Detail
-
setFromCmdLine
public java.lang.String[] setFromCmdLine(java.lang.String[] args)
Parses the given argument list into flag/value pairs, which are stored as properties. Flags that are present without values are given the value "true". If any flag is found for which there is already a mapping present, the existing mapping will be overwritten. Flags should be of the form:
java Foo -flag1 value1 -flag2 value2 ... arg1 arg2 ...
- Parameters:
args
- the command-line arguments- Returns:
- all arguments in the original array beyond the flag/value pair list
-
setInto
public Options setInto(java.lang.Object obj)
This method uses reflection to set all the properties in the given object that are named by the keys in this map. For a given key 'foo', the algorithm will look for a 'setFoo' method in the given instance. For a given key 'foo.bar', the algorithm will first look for a 'getFoo' method in the given instance, then will recurse on the return value of that method, now looking for the 'bar' property. This allows the setting of nested object properties. If in the above example the 'getFoo' method is not present or returns null, the algorithm will look for a 'setFoo' method; if found it will constrct a new instance of the correct type, set it using the 'setFoo' method, then recurse on it as above. Property names can be nested in this way to an arbitrary depth. For setter methods that take multiple parameters, the value mapped to the key can use the ',' as an argument separator character. If not enough values are present for a given method after splitting the string on ',', the remaining arguments will receive default values. All arguments are converted from string form to the correct type if possible(i.e. if the type is primitive, java.lang.Clas, or has a constructor that takes a single string argument). Examples:- Map Entry:
"age"->"12"
Resultant method call:obj.setAge(12)
- Map Entry:
"range"->"1,20"
Resultant method call:obj.setRange(1, 20)
- Map Entry:
"range"->"10"
Resultant method call:obj.setRange(10, 10)
- Map Entry:
"brother.name"->"Bob"
Resultant method call:obj.getBrother().setName("Bob")
Map
returned by this method.- Returns:
- an
Options
of key-value pairs in this object for which no setters could be found. - Throws:
java.lang.RuntimeException
- on parse error
- Map Entry:
-
findOptionsFor
public static java.util.Collection<java.lang.String> findOptionsFor(java.lang.Class<?> type)
Finds all the options that can be set on the provided class. This does not look for path-traversal expressions.- Parameters:
type
- The class for which available options should be listed.- Returns:
- The available option names in
type
. The names will have initial caps. They will be ordered alphabetically.
-
getBooleanProperty
public boolean getBooleanProperty(java.lang.String key, java.lang.String key2, boolean def)
Specialization ofgetBooleanProperty(java.lang.String, java.lang.String, boolean)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
getFloatProperty
public float getFloatProperty(java.lang.String key, java.lang.String key2, float def)
Specialization ofTypedProperties.getFloatProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
getDoubleProperty
public double getDoubleProperty(java.lang.String key, java.lang.String key2, double def)
Specialization ofTypedProperties.getDoubleProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
getLongProperty
public long getLongProperty(java.lang.String key, java.lang.String key2, long def)
Specialization ofTypedProperties.getLongProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
getIntProperty
public int getIntProperty(java.lang.String key, java.lang.String key2, int def)
Specialization ofTypedProperties.getIntProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
getProperty
public java.lang.String getProperty(java.lang.String key, java.lang.String key2, java.lang.String def)
Specialization ofProperties.getProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeBooleanProperty
public boolean removeBooleanProperty(java.lang.String key, java.lang.String key2, boolean def)
Specialization ofTypedProperties.removeBooleanProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeFloatProperty
public float removeFloatProperty(java.lang.String key, java.lang.String key2, float def)
Specialization ofTypedProperties.removeFloatProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeDoubleProperty
public double removeDoubleProperty(java.lang.String key, java.lang.String key2, double def)
Specialization ofTypedProperties.removeDoubleProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeLongProperty
public long removeLongProperty(java.lang.String key, java.lang.String key2, long def)
Specialization ofTypedProperties.removeLongProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeIntProperty
public int removeIntProperty(java.lang.String key, java.lang.String key2, int def)
Specialization ofTypedProperties.removeIntProperty(java.lang.String)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
removeProperty
public java.lang.String removeProperty(java.lang.String key, java.lang.String key2, java.lang.String def)
Specialization ofProperties.remove(Object)
to allow a value to appear under either of two keys; useful for short and long versions of command-line flags.
-
-