And another one …
01 |
import java.util.regex.Pattern; |
02 |
import com.mgi.util. args.ArgsException.ErrorCode; |
04 |
public class IntegerArgumentMarshaler extends AbstractArgumentMarshaler
{ |
06 |
private static final Pattern
ARGUMENT_ID_PATTERN = Pattern.compile( "[a-z]" ); |
07 |
private static final String
ARGUMENT_TYPE_POSTFIX = "#" ; |
09 |
private int intValue
= 0 ; |
11 |
private IntegerArgumentMarshaler(
String argumentId ){ |
15 |
protected IntegerArgumentMarshaler(){ |
20 |
public ArgumentMarshaler
newInstance(String argumentId) { |
21 |
return new IntegerArgumentMarshaler(
argumentId ); |
25 |
public String
getArgumentTypePostfix() { |
26 |
return ARGUMENT_TYPE_POSTFIX; |
30 |
public Pattern
getArgumentIdPattern() { |
31 |
return ARGUMENT_ID_PATTERN; |
35 |
public int getNumberOfArgs()
{ |
39 |
public void set(String...
arguments) throws ArgsException
{ |
41 |
if (
arguments.length == 0 ||
arguments[ 0 ]
== null ){ |
42 |
throw new ArgsException(
ErrorCode.MISSING_INTEGER, getArgumentId(), null ); |
46 |
intValue
= Integer.parseInt(arguments[ 0 ]); |
48 |
catch (NumberFormatException
e) { |
49 |
throw new ArgsException(
ErrorCode.INVALID_INTEGER, getArgumentId(), arguments[ 0 ]
); |
I think you can see that implementing a new ArgumentMarshaler is not more complicated as before. Just answer some more questions about it’s context – the main part remains quite the same.
Give it to me, Baby …
Now that we know how a single Marshaler works and how we can retrieve them from our Marshalers collection, it’s very easy to pass the current arguments to them. As said before, we just shifted that task into a class of its own – ArgumentPopulator:
01 |
import java.util.Iterator; |
02 |
import java.util.List; |
04 |
public class ArgumentPopulator
{ |
06 |
private List<String>
argsList = null ; |
08 |
public static ArgumentPopulator
distribute( List<String> argsList ){ |
09 |
return new ArgumentPopulator(
argsList ); |
12 |
private ArgumentPopulator(
List<String> argsList ){ |
13 |
this .argsList
= argsList; |
16 |
public void to(
Marshalers marshalers ) throws ArgsException{ |
18 |
for (
Iterator<String> arguments = argsList.iterator(); arguments.hasNext(); ) { |
20 |
String
argument = arguments.next(); |
22 |
if (
representsArgumentId( argument ) ){ |
24 |
String
argumentId = extractArgumentIdFrom( argument ); |
26 |
if (
! marshalers.isMarshalerAvailabeFor( argumentId ) ){ |
27 |
throw new ArgsException(
ArgsException.ErrorCode.UNEXPECTED_ARGUMENT, argumentId, null ); |
30 |
ArgumentMarshaler
marshaler = marshallers.getMarshalerFor( argumentId ); |
32 |
marshaler.set(
getArgumentsAhead( marshaller.getNumberOfArgs(), arguments ) ); |
37 |
private String
extractArgumentIdFrom(String argument) { |
38 |
return argument.substring( 1 ,
argument.length() ); |
41 |
private boolean representsArgumentId(String
argument) { |
42 |
return argument.startsWith( "-" ); |
45 |
private String[]
getArgumentsAhead( int numberOfArgs,
Iterator<String> arguments ) { |
46 |
String[] args
= new String[numberOfArgs]; |
48 |
for ( int i= 0 ;
i < numberOfArgs; i++ ){ |
49 |
args[i]
= arguments.hasNext() ? arguments.next() : null ; |
As you can see, ArgumentPopulator accepts a List of current arguments (by using the provided Factory method distribute()) and tries to push the found arguments to any collection of passed Marshalers (using the to() method).