|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.meta.util.Arguments
public class Arguments
An arguments object represents a collection of String arguments that may or may not have an associated String value.
Argument names are expected to be prefixed with a dash (-), and are looked up in a case-insensitive manner.
Example:
-foo -bar barbar -goo goo goo goo -foobarHere, four argument names are present:
foo
,
bar
, goo
, and foobar
. foo
and foobar
have no associated values, while
bar
has the value barbar
associated,
and goo
has goo goo goo
.
Nested Class Summary | |
---|---|
static class |
Arguments.Argument
An argument represents a single argument, with or without an associated value. |
Field Summary | |
---|---|
Map<String,Arguments.Argument> |
arguments
Actual arguments stored according to insertion order. |
Constructor Summary | |
---|---|
Arguments(Arguments arguments)
Copy constructor. |
|
Arguments(String... args)
Constructor. |
Method Summary | ||
---|---|---|
boolean |
equals(Object object)
Returns true if object is an Arguments
instance storing the same arguments as these arguments,
false if not. |
|
Arguments.Argument |
getArgument(String name)
Returns the argument having the name supplied as name ,
regardless if it has an associated value or not. |
|
Arguments.Argument |
getArgument(String name,
boolean mustHaveValue)
Returns the argument having the name supplied as name ,
regardless if it has an associated value or not if
mustHaveValue is false. |
|
List<Arguments.Argument> |
getArguments(String prefix)
Returns a list of arguments having a name that starts with the prefix supplied as prefix , if any. |
|
String |
getArgumentValue(String name)
Returns the argument value associated with the argument name supplied as name , if any. |
|
|
getArgumentValue(String name,
Class<V> type)
Returns the argument value associated with the argument name supplied as name , if any, transformed to
the type supplied as type using the static
type.valueOf(String) method. |
|
String |
getArgumentValue(String name,
String defaultValue)
Returns the argument value associated with the argument name supplied as name , if any. |
|
|
getArgumentValue(String name,
V defaultValue,
Class<V> type)
Returns the argument value associated with the argument name supplied as name , if any, transformed to
the type supplied as type using the static
type.valueOf(String) method. |
|
private static String |
getName(String name)
Returns a valid argument name based on name . |
|
boolean |
hasArgument(String name)
Returns true if an argument has the name supplied as name ,
regardless if it has an associated value or not. |
|
int |
hashCode()
Returns the hash code of these arguments. |
|
Iterator<Arguments.Argument> |
iterator()
Returns an iterator that is backed by these arguments. |
|
static Map<String,Arguments.Argument> |
parse(String... args)
Parses the arguments supplied in args , and returns
a corresponding map, ordered according to the order of
args . |
|
boolean |
setArgument(String name,
String value)
Updates or adds the argument having the name supplied as name with the associated value supplied as
value , if any. |
|
int |
size()
Returns the number of arguments. |
|
String |
toString()
Returns the string representation of these arguments. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public Map<String,Arguments.Argument> arguments
All names contained as keys do not have a prefixed dash (-).
Never null, but can be empty.
Constructor Detail |
---|
public Arguments(Arguments arguments)
arguments
- The arguments to copy; cannot be null.
NullPointerException
- If arguments
is null.public Arguments(String... args)
The arguments are parsed as described in the class documentation.
args
- The actual arguments to parse; can be null.parse(String...)
Method Detail |
---|
public boolean equals(Object object)
object
is an Arguments
instance storing the same arguments as these arguments,
false if not.
equals
in class Object
object
- The object to test; can be null.
public Arguments.Argument getArgument(String name)
name
,
regardless if it has an associated value or not. The comparison on argument name is performed in a case-insensitive manner.
name
- The argument name; cannot be null, and may
start with a dash.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public Arguments.Argument getArgument(String name, boolean mustHaveValue)
name
,
regardless if it has an associated value or not if
mustHaveValue
is false. If true, an argument is only
returned if it has an associated value. The comparison on argument name is performed in a case-insensitive manner.
name
- The argument name; cannot be null, and may
start with a dash.mustHaveValue
- True if an argument should only be returned
if it has an associated value.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public List<Arguments.Argument> getArguments(String prefix)
prefix
, if any.
If prefix
is null, all arguments are returned.
Modifying the returned list will not affect these arguments.
prefix
- The argument prefix; cannot be null, and may
start with a dash.
IllegalArgumentException
- If prefix
is empty or
illegal.public String getArgumentValue(String name)
name
, if any.
name
- The argument name; cannot be null, and may
start with a dash.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public <V> V getArgumentValue(String name, Class<V> type)
name
, if any, transformed to
the type supplied as type
using the static
type.valueOf(String)
method.
If no such method is found in type
, null is returned.
If the transformation fails, null is returned.
V
- The type to transform the argument value into
using its static valueOf(String)
method.name
- The argument name; cannot be null, and may
start with a dash.type
- The class of the desired type that the
argument value should be transformed
to by type.valueOf(String)
;
cannot be null.
V
type; can be null.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal. public String getArgumentValue(String name, String defaultValue)
name
, if any.
defaultValue
is returned if no such argument
is found or it does not have an associated value.
name
- The argument name; cannot be null, and may
start with a dash.defaultValue
- The default value; can be null.
defaultValue
(which can
be null).
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public <V> V getArgumentValue(String name, V defaultValue, Class<V> type)
name
, if any, transformed to
the type supplied as type
using the static
type.valueOf(String)
method.
If no such method is found in type
, null is returned.
If no argument value is found or the transformation
fails, defaultValue
is returned.
V
- The type to transform the argument value into
using its static valueOf(String)
method.name
- The argument name; cannot be null, and may
start with a dash.defaultValue
- The default value; can be null.type
- The class of the desired type that the
argument value should be transformed
to by type.valueOf(String)
;
cannot be null.
V
type; can be null.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal. private static final String getName(String name)
name
.
name
- The name to make valid; cannot be null.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public boolean hasArgument(String name)
name
,
regardless if it has an associated value or not.
name
- The argument name; cannot be null, and may
start with a dash.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public int hashCode()
hashCode
in class Object
public Iterator<Arguments.Argument> iterator()
iterator
in interface Iterable<Arguments.Argument>
public static final Map<String,Arguments.Argument> parse(String... args)
args
, and returns
a corresponding map, ordered according to the order of
args
. Argument names are expected to be prefixed with a single dash. Everything else is considered an argument value.
args
- The arguments to parse; can be null or empty.
public boolean setArgument(String name, String value)
name
with the associated value supplied as
value
, if any. If an argument with that name is already contained, it will be updated; if not, a new argument will be added.
name
- The argument name; cannot be null, and may
start with a dash.value
- The associated argument value; can be null.
name
already
exists, false if not.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
is empty or
illegal.public int size()
public String toString()
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |