|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsugar4j.lang.VarArgs
public class VarArgs
Simulates named parameters (order not relevant) using name-value pairs with variable arguments.
Usage:
class Person { Person(Object... attributes) { // declare an Object varargs parameter VarArgs args = new VarArgs(attributes); // construct a VarArgs instance, passing the array // those are required // access members using get(). No casts necessary id = args.get("id"); name = args.get("name"); lastName = args.get("lastName"); // title is optional title = args.get("title", null); } String name; String lastName; int id; String title; } // Person is then used like this: Person krizz = new Person("id", 23, "name", "Christian", "lastName", "Oetterli", "title", "Mr.");
Constructor Summary | |
---|---|
VarArgs(Object... nameValuePairs)
Constructs a new instance with the given name-value pairs. |
Method Summary | ||
---|---|---|
|
get(String name)
Returns the value at a given name. |
|
|
get(String name,
T defaultValue)
Returns the value at a given name or a default value if there is no value for the name. |
Constructor Detail |
---|
public VarArgs(Object... nameValuePairs)
nameValuePairs
- Array which must consist of pairs. First ist the name, which must be an
instance of String. Second is the value for that name
ArrayStoreException
- if nameValuePairs are not pairs
ClassCastException
- if the first of a pair is not an instance of StringMethod Detail |
---|
public <T> T get(String name)
T
- Type of the value to getname
- Name
ArrayStoreException
- if there is no value for the given name
ClassCastException
- if the value at the given name is not of type Tpublic <T> T get(String name, T defaultValue)
T
- Type of the value to getname
- NamedefaultValue
- Value to return if the name does not exist
ClassCastException
- if the value at the given name is not of type T
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |