| org.cougaar.util.Arguments
Arguments | final public class Arguments extends AbstractMap> implements Serializable(Code) | | A "name=value" parser.
Note that Arguments instances are unmodifiable, like Strings.
Example use:
Arguments args = new Arguments("x=y, q=one, q=two, z=99");
String x = args.getString("x");
assert "y".equals(x);
int z = args.getInt("z", 1234);
assert z == 99;
List<String> q = args.getStrings("q");
assert q != null && q.size() == 2;
The Cougaar component model includes built-in support to invoke an optional
"setArguments" method. Here's an example:
package org;
public class MyPlugin ... {
private Arguments args;
// The "setArguments" method is special -- it's an optional method
// that's found by the component model via reflection. The passed-in
// arguments instance is created via:
// new Arguments(listOfStrings, classname);
public void setArguments(Arguments args) { this.args = args; }
public void load() {
super.load();
// Get the value of our "foo" argument
//
// First looks for a plugin XML argument named "foo",
// next looks for a "-Dorg.MyPlugin.foo=" system property,
// otherwise the value will be the 1234 default.
int foo = args.getInt("foo", 1234);
System.out.println("foo is "+foo);
}
}
The
Arguments.callSetters method supports "setter" reflection, for example:
package org;
public class MyPlugin ... {
private int foo = 1234;
public void setArguments(Arguments args) { args.callSetters(this); }
// This "setNAME(TYPE)" method is found by reflection.
// The args class will only invoke the setters for which it has values.
public void setFoo(int i) { this.foo = i; }
public void load() {
super.load();
System.out.println("foo is "+foo);
}
}
|
Method Summary | |
public Set<String> | callSetters(Object o) Call the given object's setter methods or fields for every name=value
pair in the
Arguments.entrySet , return the Set of unknown String keys. | public boolean | containsKey(Object key) | public Set<Map.Entry<String, List<String>>> | entrySet() | public List<String> | get(String key) | public boolean | getBoolean(String key) | public boolean | getBoolean(String key, boolean deflt) | public List<Boolean> | getBooleans(String key) | public List<Boolean> | getBooleans(String key, List<Boolean> deflt) | public double | getDouble(String key) | public double | getDouble(String key, double deflt) | public List<Double> | getDoubles(String key) | public List<Double> | getDoubles(String key, List<Double> deflt) | public int | getInt(String key) | public int | getInt(String key, int deflt) | public List<Integer> | getInts(String key) | public List<Integer> | getInts(String key, List<Integer> deflt) | public long | getLong(String key) | public long | getLong(String key, long deflt) | public List<Long> | getLongs(String key) | public List<Long> | getLongs(String key, List<Long> deflt) | public String | getString(String key) | public String | getString(String key, String deflt) Get the first value, or the specified default if there is no value. | public List<String> | getStrings(String key, List<String> deflt) All the other "get*" methods call this method. | public List<String> | getStrings(String key) | public Arguments | setString(String key, String value) | public Arguments | setStrings(String key, List<String> values) | public int | size() | public List<Arguments> | split() Split this arguments instance of String-to-List[N] pairs into a List of
"flattened" Arguments with String-to-List[1] pairs. | public Arguments | swap(String key1, String key2) Return an Arguments instance where the values for key1 and key2 are
swapped. | public String | toString() | public String | toString(String format) | public String | toString(String format, String separator) Create a string representation of this map using the given format.
For example, if our map contains:
A=B
X=V0,V1,V2
then:
toString("the_$key is the_$value", " * ");
would return:
the_A is the_B * the_X is the_V0
and:
toString("($key eq $vals)", " +\n");
would return:
(A eq B) +
(X eq [V0, V1, V2])
and:
toString("$key=$veach", "&");
would return a URL-like string:
A=B&X=V0&X=V1&X=V2
and:
"{" + toString("$key=$vlist", ", ") + "}";
would return the standard
Map.toString format:
{A=[B], X=[V0, V1, V2]}
The supported variables are:
- "$key" is the string key name (e.g.
|
EMPTY_INSTANCE | final public static Arguments EMPTY_INSTANCE(Code) | | A singleton instance for an empty arguments
|
Arguments | public Arguments(Object o, Object propertyPrefix, Object deflt, Object keys)(Code) | | Parameters: o - the optional input object, e.g. a List of name=value Strings, oranother Arguments instance. Parameters: propertyPrefix - the optional SystemProperties property prefix, e.g."org.MyPlugin.", for "-Dorg.MyPlugin.name=value" lookups. If aclass is specified, that class's name+. and its parent'snames+. will be used. Parameters: deflt - the optional default values, e.g. a List of name=valueStrings, or another Arguments instance. Parameters: keys - the optional filter on which keys are allowed, e.g. onlyallow (A, B, C) |
callSetters | public Set<String> callSetters(Object o)(Code) | | Call the given object's setter methods or fields for every name=value
pair in the
Arguments.entrySet , return the Set of unknown String keys.
For example, the name=value pair "x=y" will look for:
public void setX(type) {..}
and field:
public type x;
If neither are found then "x" will be included in the returned Set.
Parameters: o - Object that has the setter methods & fields Subset of the Arguments.keySet that could not be set |
getBoolean | public boolean getBoolean(String key)(Code) | | the value, or false if not set |
getBoolean | public boolean getBoolean(String key, boolean deflt)(Code) | | the first value, or the deflt if not set |
getDouble | public double getDouble(String key)(Code) | | the value, or Double.NaN if not set |
getDouble | public double getDouble(String key, double deflt)(Code) | | the first value, or the deflt if not set |
getInt | public int getInt(String key)(Code) | | the value, or -1 if not set |
getInt | public int getInt(String key, int deflt)(Code) | | the first value, or the deflt if not set |
getLong | public long getLong(String key)(Code) | | the value, or -1 if not set |
getLong | public long getLong(String key, long deflt)(Code) | | the first value, or the deflt if not set |
getString | public String getString(String key, String deflt)(Code) | | Get the first value, or the specified default if there is no value.
Equivalent to:
List<String> l = get(key);
return (l == null ? deflt : l.get(0));
the first value, or the deflt if not set |
getStrings | public List<String> getStrings(String key, List<String> deflt)(Code) | | All the other "get*" methods call this method.
Equivalent to:
List<String> l = get(key);
return (l == null ? deflt : l);
the non-empty values or the deflt |
setStrings | public Arguments setStrings(String key, List<String> values)(Code) | | Parameters: key - the non-null key Parameters: values - the values, which can be null or empty to remove thespecified key's entry returns a possibly new Arguments instance (likeString.trim and similar copy-on-modify classes) where"getStrings(key)" will be equal to the specified "values" |
split | public List<Arguments> split()(Code) | | Split this arguments instance of String-to-List[N] pairs into a List of
"flattened" Arguments with String-to-List[1] pairs.
This is useful to group together same-named arguments.
For example, the constructor input:
"foo=f1, bar=b1, qux=q1,
foo=f2, bar=b2, qux=q2,
foo=f3, bar=b3, qux=q3"
will be parsed as:
{foo=[f1,f2,f3], bar=[b1,b2,b3], qux=[q1,q2,q3]}
and can be split into:
[{foo=[f1], bar=[b1], qux=[q1]},
{foo=[f2], bar=[b2], qux=[q2]},
{foo=[f3], bar=[b3], qux=[q3]}}
This simplifies iteration:
for (Arguments a : args.split()) {
System.out.println("foo is "+a.getString("foo"));
}
which will print:
foo is f1
foo is f2
foo is f3
a List of Arguments. |
swap | public Arguments swap(String key1, String key2)(Code) | | Return an Arguments instance where the values for key1 and key2 are
swapped.
In other words:
// given:
List<String> v1 = args.getStrings(key1);
List<String> v2 = args.getStrings(key2);
// do swap:
Arguments ret = args.swap(key1, key2);
// validate:
assert(ret.size() == args.size());
List<String> x1 = ret.getStrings(key1);
List<String> x2 = ret.getStrings(key2);
assert(v1 == null ? x2 == null : v1.equals(x2));
assert(v2 == null ? x1 == null : v2.equals(x1));
returns a new Arguments instance |
toString | public String toString(String format, String separator)(Code) | | Create a string representation of this map using the given format.
For example, if our map contains:
A=B
X=V0,V1,V2
then:
toString("the_$key is the_$value", " * ");
would return:
the_A is the_B * the_X is the_V0
and:
toString("($key eq $vals)", " +\n");
would return:
(A eq B) +
(X eq [V0, V1, V2])
and:
toString("$key=$veach", "&");
would return a URL-like string:
A=B&X=V0&X=V1&X=V2
and:
"{" + toString("$key=$vlist", ", ") + "}";
would return the standard
Map.toString format:
{A=[B], X=[V0, V1, V2]}
The supported variables are:
- "$key" is the string key name (e.g. "X")
- "$value" is the first value (e.g. "V0"), as defined in
Arguments.getString(String)
- "$vals" is the first value if there is only one value (e.g. "B"),
otherwise the list of values prefixed with "[" and "]" if there are
multiple values (e.g. "[V0, V1, V2]").
- "$veach" is current value in the list (e.g. "V1")
- "$vlist" is the "[]" wrapped list (e.g. "[B]" or "[V0, V1, V2]")
Parameters: format - optional format, defaults to "$key=$vlist" Parameters: separator - optional separator, defaults to ", " a string with each entry in the given format, where every "$key"is replaced with the map key and every "$value" is replaced withthe map value. |
|
|