sugar4j.lang
Class VarArgs

java.lang.Object
  extended by sugar4j.lang.VarArgs

public class VarArgs
extends Object

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."); 
 
 

Version:
$Id: VarArgs.java,v 1.1 2008/02/14 12:54:16 krizzdewizz Exp $
Author:
krizzdewizz

Constructor Summary
VarArgs(Object... nameValuePairs)
          Constructs a new instance with the given name-value pairs.
 
Method Summary
<T> T
get(String name)
          Returns the value at a given name.
<T> T
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

VarArgs

public VarArgs(Object... nameValuePairs)
Constructs a new instance with the given name-value pairs.

Parameters:
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
Throws:
ArrayStoreException - if nameValuePairs are not pairs
ClassCastException - if the first of a pair is not an instance of String
Method Detail

get

public <T> T get(String name)
Returns the value at a given name.

Type Parameters:
T - Type of the value to get
Parameters:
name - Name
Returns:
Value
Throws:
ArrayStoreException - if there is no value for the given name
ClassCastException - if the value at the given name is not of type T

get

public <T> T get(String name,
                 T defaultValue)
Returns the value at a given name or a default value if there is no value for the name.

Type Parameters:
T - Type of the value to get
Parameters:
name - Name
defaultValue - Value to return if the name does not exist
Returns:
Value
Throws:
ClassCastException - if the value at the given name is not of type T