intarsys runtime library

de.intarsys.tools.functor
Class ArgTools

java.lang.Object
  extended by de.intarsys.tools.functor.ArgTools

public class ArgTools
extends Object

Tool class to ease handling of arguments.


Nested Class Summary
static interface ArgTools.IBindingProcessor
           
 
Field Summary
static IFunctor toString
           
 
Constructor Summary
ArgTools()
           
 
Method Summary
static IArgs createArgs()
           
static IArgs getArgs(IArgs args, String name, IArgs defaultValue)
          The argument value at name as an IArgs instance.
static boolean getBool(IArgs args, String name, boolean defaultValue)
          The argument value at name as a boolean.
static boolean getBoolean(IArgs args, String name, boolean defaultValue)
          Synonym for getBool.
static byte getByte(IArgs args, String name, byte defaultValue)
          The argument value at name as a byte.
static byte[] getByteArray(IArgs args, String name, byte[] defaultValue)
          The argument value at name as a byte array.
static char getChar(IArgs args, String name, char defaultValue)
          The argument value at name as a char.
static char[] getCharArray(IArgs args, String name, char[] defaultValue)
          The argument value at name as a char[].
static Class getClass(IArgs args, String name, Class defaultValue)
          The argument value at name as a Class.
static Color getColor(IArgs args, String name, Color defaultValue)
          The argument value at name as a Color.
static Date getDate(IArgs args, String name, Date defaultValue)
          The argument value at name as a Date.
static IDigest getDigest(IArgs args, String name)
          The argument value at name as an IDigest.
static
<T extends EnumItem>
T
getEnumItem(IArgs args, EnumMeta<T> meta, String name)
          The argument value at name as a EnumItem.
static
<T extends EnumItem>
T
getEnumItem(IArgs args, EnumMeta<T> meta, String name, String defaultValue)
          The argument value at name as a EnumItem.
static
<T extends EnumItem>
T
getEnumItem(IArgs args, EnumMeta<T> meta, String name, T defaultValue)
          The argument value at name as a EnumItem.
static File getFile(IArgs args, String name, File defaultValue)
          The argument value at name as a File.
static float getFloat(IArgs args, String name, float defaultValue)
          The argument value at name as a float.
static int getInt(IArgs args, String name, int defaultValue)
          The argument value at name as a int.
static List getList(IArgs args, String name, List defaultValue)
          The argument value at name as a List.
static ILocator getLocator(IArgs args, String name, ILocator defaultValue, ILocatorFactory factory)
          The argument value at name as a ILocator.
static List<ILocator> getLocators(IArgs args, String name, ILocatorFactory factory)
          The argument value at name as a List.
static Level getLogLevel(IArgs args, String name, Level defaultValue)
          The argument value at name as a Level.
static long getLong(IArgs args, String name, long defaultValue)
          The argument value at name as a long.
static Map getMap(IArgs args, String name, Map defaultValue)
          The argument value at name as a Map.
static Object getObject(IArgs args, String name, Object defaultValue)
          The argument value at name as a Object.
static Object getPath(IArgs args, String path)
          Interpret path as a "." separated sequence of arg names, descend (and lazy create) the tree of IArgs objects and return the value in the leaf IArgs instance or null.
static Point2D getPoint(IArgs args, String name, Point2D defaultValue)
          The argument value at name as a Point2D.
static String getString(IArgs args, String name, String defaultValue)
          The argument value at name as a String.
static
<T> T
getValue(IArgs args, String name, Class<T> clazz, Object defaultValue)
          The argument value at name converted to clazz.
static String prefix(String prefix, String name)
          Create a new camel case argument name from name by prefixing with prefix.
static IArgs putAll(IArgs args, IArgs other)
          Put all named top level entries in other into args.
static IArgs putAll(IArgs args, Map map)
          Put all top level entries in map into args.
static IArgs putAllDeep(IArgs args, IArgs other)
          Merge recursively all named entries from other into args.
static IArgs putAllIfAbsent(IArgs args, IArgs other)
          Put all named top level entries in other that are not already defined into args.
static IArgs putAllIfAbsent(IArgs args, Map map)
          Put all top level entries in map that are not already defined into args.
static IArgs putAllIfAbsentDeep(IArgs args, IArgs other)
          Merge recursively all named entries from other that are not already defined in args into args.
static void putDefinition(IArgs args, String definition)
          Add a String based definition to args.
static IArgs putMapped(IArgs args, IArgs other, String[] argsNames, String[] otherNames)
          Shovel arguments from other to args, mapping the argument names from otherNames to argsNames.
static IArgs putPath(IArgs args, String path, Object value)
          Interpret path as a "." separated sequence of arg names, descend (and lazy create) the tree of IArgs objects and set value in the leaf IArgs instance.
static IArgs toArgs(Object value)
          Cast or convert value to an IArgs. null is converted to an empty IArgs object.
static List toList(IArgs args)
          Convert the args to a List.
static Map toMap(IArgs args)
          Convert the args to a Map.
static Map toMapDeep(IArgs args)
          Convert the args to a Map.
static Map<String,Object> toMapDeepFlat(IArgs args)
          Convert the args to a Map.
static Map<String,Object> toMapDeepFlat(IArgs args, String prefix, Map<String,Object> map)
          Convert the args to a Map.
static String toString(IArgs args, String prefix)
          Create a printable String for args.
static void visitNamedBindings(String prefix, IArgs args, ArgTools.IBindingProcessor processor)
          For all named argument bindings perform the ArgTools.IBindingProcessor.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

toString

public static final IFunctor toString
Constructor Detail

ArgTools

public ArgTools()
Method Detail

createArgs

public static IArgs createArgs()

getArgs

public static IArgs getArgs(IArgs args,
                            String name,
                            IArgs defaultValue)
The argument value at name as an IArgs instance. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, IArgs, String, Map and List.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as an IArgs instance.

getBool

public static boolean getBool(IArgs args,
                              String name,
                              boolean defaultValue)
The argument value at name as a boolean. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Boolean, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a boolean

getBoolean

public static boolean getBoolean(IArgs args,
                                 String name,
                                 boolean defaultValue)
Synonym for getBool.

Parameters:
args -
name -
defaultValue -
Returns:
The result of getBool

getByte

public static byte getByte(IArgs args,
                           String name,
                           byte defaultValue)
The argument value at name as a byte. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Number, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a byte.

getByteArray

public static byte[] getByteArray(IArgs args,
                                  String name,
                                  byte[] defaultValue)
The argument value at name as a byte array. If the argument value is not provided or not convertible, defaultValueis returned.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a byte array.

getChar

public static char getChar(IArgs args,
                           String name,
                           char defaultValue)
The argument value at name as a char. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Character, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a char.

getCharArray

public static char[] getCharArray(IArgs args,
                                  String name,
                                  char[] defaultValue)
                           throws IllegalArgumentException
The argument value at name as a char[]. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, String, char[]. Unlike the other conversion methods, this one throws an IllegalArgumentException, if the value is not of type String or char[].

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a String.
Throws:
IllegalArgumentException - if value is not of type String or char[]

getClass

public static Class getClass(IArgs args,
                             String name,
                             Class defaultValue)
The argument value at name as a Class. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Boolean, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Class.

getColor

public static Color getColor(IArgs args,
                             String name,
                             Color defaultValue)
The argument value at name as a Color. If the argument value is not provided or not convertible, defaultValueis returned.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Color.

getDate

public static Date getDate(IArgs args,
                           String name,
                           Date defaultValue)
The argument value at name as a Date. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Date, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Date.

getDigest

public static IDigest getDigest(IArgs args,
                                String name)
                         throws IOException
The argument value at name as an IDigest.

Parameters:
args -
name -
Returns:
The argument value at name as an IDigest.
Throws:
IOException

getEnumItem

public static <T extends EnumItem> T getEnumItem(IArgs args,
                                                 EnumMeta<T> meta,
                                                 String name)
The argument value at name as a EnumItem. If the argument value is not provided or not convertible, the enumeration default value is returned.

This method performs the necessary casts and conversions. Supported input types are null, EnumItem, String.

Parameters:
args -
meta -
name -
Returns:
The argument value at name as a EnumItem.

getEnumItem

public static <T extends EnumItem> T getEnumItem(IArgs args,
                                                 EnumMeta<T> meta,
                                                 String name,
                                                 String defaultValue)
The argument value at name as a EnumItem. If the argument value is not provided or not convertible, the enumeration item with the id defaultValue is returned.

This method performs the necessary casts and conversions. Supported input types are null, EnumItem, String.

Parameters:
args -
meta -
name -
Returns:
The argument value at name as a EnumItem.

getEnumItem

public static <T extends EnumItem> T getEnumItem(IArgs args,
                                                 EnumMeta<T> meta,
                                                 String name,
                                                 T defaultValue)
The argument value at name as a EnumItem. If the argument value is not provided or not convertible, the defaultValue is returned.

Parameters:
args -
meta -
name -
Returns:
The argument value at name as a EnumItem.

getFile

public static File getFile(IArgs args,
                           String name,
                           File defaultValue)
The argument value at name as a File. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, File, String, ILocator.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Date.

getFloat

public static float getFloat(IArgs args,
                             String name,
                             float defaultValue)
The argument value at name as a float. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Number, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a float.

getInt

public static int getInt(IArgs args,
                         String name,
                         int defaultValue)
The argument value at name as a int. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Number, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a int.

getList

public static List getList(IArgs args,
                           String name,
                           List defaultValue)
The argument value at name as a List. If the argument value is not provided or not convertible, defaultValueis returned.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a List.

getLocator

public static ILocator getLocator(IArgs args,
                                  String name,
                                  ILocator defaultValue,
                                  ILocatorFactory factory)
The argument value at name as a ILocator. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, ILocator, String, File

Parameters:
args -
name -
defaultValue -
factory -
Returns:
The argument value at name as a ILocator.

getLocators

public static List<ILocator> getLocators(IArgs args,
                                         String name,
                                         ILocatorFactory factory)
The argument value at name as a List. If the argument value is not provided, nullis returned.

This method performs the necessary casts and conversions. Supported input types are Collection of ILocator, String and File.

Parameters:
args -
name -
factory -
Returns:
The argument value at name as a List.

getLogLevel

public static Level getLogLevel(IArgs args,
                                String name,
                                Level defaultValue)
The argument value at name as a Level. If the argument value is not provided or not convertible, defaultValueis returned.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Level.

getLong

public static long getLong(IArgs args,
                           String name,
                           long defaultValue)
The argument value at name as a long. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Number, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a int.

getMap

public static Map getMap(IArgs args,
                         String name,
                         Map defaultValue)
The argument value at name as a Map. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Map, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Map.

getObject

public static Object getObject(IArgs args,
                               String name,
                               Object defaultValue)
The argument value at name as a Object. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Object.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Object.

getPath

public static Object getPath(IArgs args,
                             String path)
Interpret path as a "." separated sequence of arg names, descend (and lazy create) the tree of IArgs objects and return the value in the leaf IArgs instance or null.

Parameters:
args -
path -
Returns:
The argument value in args at path

getPoint

public static Point2D getPoint(IArgs args,
                               String name,
                               Point2D defaultValue)
The argument value at name as a Point2D. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, Point2D, String.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a Point2D.

getString

public static String getString(IArgs args,
                               String name,
                               String defaultValue)
The argument value at name as a String. If the argument value is not provided or not convertible, defaultValueis returned.

This method performs the necessary casts and conversions. Supported input types are null, String, Object.

Parameters:
args -
name -
defaultValue -
Returns:
The argument value at name as a String.

getValue

public static <T> T getValue(IArgs args,
                             String name,
                             Class<T> clazz,
                             Object defaultValue)
The argument value at name converted to clazz. If the argument value is not provided or not convertible, defaultValueis returned.

Parameters:
args -
name -
clazz -
defaultValue -
Returns:
The argument value at name converted to clazz.

prefix

public static String prefix(String prefix,
                            String name)
Create a new camel case argument name from name by prefixing with prefix.

Parameters:
prefix -
name -
Returns:
The new argument name.

putAll

public static IArgs putAll(IArgs args,
                           IArgs other)
Put all named top level entries in other into args.

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
other -
Returns:
The modified args object.

putAll

public static IArgs putAll(IArgs args,
                           Map map)
Put all top level entries in map into args. The map keys are interpreted as a path expression ("." separated).

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
map -
Returns:
The modified args object.

putAllDeep

public static IArgs putAllDeep(IArgs args,
                               IArgs other)
Merge recursively all named entries from other into args.

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
other -
Returns:
The modified args object.

putAllIfAbsent

public static IArgs putAllIfAbsent(IArgs args,
                                   IArgs other)
Put all named top level entries in other that are not already defined into args.

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
other -
Returns:
The modified args object.

putAllIfAbsent

public static IArgs putAllIfAbsent(IArgs args,
                                   Map map)
Put all top level entries in map that are not already defined into args. The map keys are interpreted as a path expression ("." separated).

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
map -
Returns:
The modified args object.

putAllIfAbsentDeep

public static IArgs putAllIfAbsentDeep(IArgs args,
                                       IArgs other)
Merge recursively all named entries from other that are not already defined in args into args.

args is modified to contain the result of the merge process and returned to ease call chaining.

Parameters:
args -
other -
Returns:
The modified args object.

putDefinition

public static void putDefinition(IArgs args,
                                 String definition)
Add a String based definition to args. "definition" is of the form "x.y.z=b" or "x.y.z:b" (the key is a "." separated path).

Parameters:
args -
definition -

putMapped

public static IArgs putMapped(IArgs args,
                              IArgs other,
                              String[] argsNames,
                              String[] otherNames)
Shovel arguments from other to args, mapping the argument names from otherNames to argsNames.

Parameters:
args -
other -
argsNames -
otherNames -
Returns:
The modified input parameter args.

putPath

public static IArgs putPath(IArgs args,
                            String path,
                            Object value)
Interpret path as a "." separated sequence of arg names, descend (and lazy create) the tree of IArgs objects and set value in the leaf IArgs instance.

Parameters:
args -
path -
value -
Returns:
The modified input parameter args.

toArgs

public static IArgs toArgs(Object value)
Cast or convert value to an IArgs. null is converted to an empty IArgs object.

Parameters:
value -
Returns:
The IArgs created from value.

toList

public static List toList(IArgs args)
Convert the args to a List.

Parameters:
args -
Returns:
The List representation of the args

toMap

public static Map toMap(IArgs args)
Convert the args to a Map. This is done 1 level deep, i.e. the immediate children of args are now members of the map. If args contains nested args, these are left alone. Example, where {} denotes an IArgs structure and [] a Map .
 {
   a = "b"
   x = {
     i = 12
     j = {
       last = "nn"
     }
   }
 }
 
will result in
 [
   a -> "b"
   x -> {
     i = 12
     j = {
       last = "nn"
     }
   }
 ]
 

Parameters:
args -
Returns:
The Map representation of the args

toMapDeep

public static Map toMapDeep(IArgs args)
Convert the args to a Map. This is done recursively, i.e. all IArgs substructures are converted to maps. The result is a nested map as well. Example, where {} denotes an IArgs structure and [] a Map .
 {
   a = "b"
   x = {
     i = 12
     j = {
       last = "nn"
     }
   }
 }
 
will result in
 [
   a -> "b"
   x -> [
     i -> 12
     j -> [
       last -> "nn"
     ]
   ]
 ]
 

Parameters:
args -
Returns:
The Map representation of the args

toMapDeepFlat

public static Map<String,Object> toMapDeepFlat(IArgs args)
Convert the args to a Map. This is done recursively, i.e. all IArgs substructures are converted, too. The result is a map where the keys are path names. Example, where {} denotes an IArgs structure and [] a Map .
 {
   a = "b"
   x = {
     i = 12
     j = {
       last = "nn"
     }
   }
 }
 
will result in
 [
   a -> "b"
   x.i -> 12
   x.j.last -> "nn"
 ]
 

Parameters:
args -
Returns:
The Map representation of the args

toMapDeepFlat

public static Map<String,Object> toMapDeepFlat(IArgs args,
                                               String prefix,
                                               Map<String,Object> map)
Convert the args to a Map. This is done recursively, i.e. all IArgs substructures are converted, too. The result is a map where the keys are path names. Example, where {} denotes an IArgs structure and [] a Map .
 {
   a = "b"
   x = {
     i = 12
     j = {
       last = "nn"
     }
   }
 }
 
will result in
 [
   a -> "b"
   x.i -> 12
   x.j.last -> "nn"
 ]
 

Parameters:
args -
Returns:
The Map representation of the args

toString

public static String toString(IArgs args,
                              String prefix)
Create a printable String for args.

Parameters:
args -
prefix -
Returns:
The printed representation of args

visitNamedBindings

public static void visitNamedBindings(String prefix,
                                      IArgs args,
                                      ArgTools.IBindingProcessor processor)
For all named argument bindings perform the ArgTools.IBindingProcessor. This method performs a depth first enumeration and call the binding processor for each leaf element in the argument tree.

Parameters:
prefix -
args -
processor -

intarsys runtime library

Copyright © 2012 intarsys consulting GmbH. All Rights Reserved.