Refining Uncle Bob’s Clean Code(四)

And another one …

01 import java.util.regex.Pattern;
02 import com.mgi.util.args.ArgsException.ErrorCode;
03  
04 public class IntegerArgumentMarshaler extends AbstractArgumentMarshaler {
05  
06     private static final Pattern ARGUMENT_ID_PATTERN = Pattern.compile( "[a-z]" );
07     private static final String ARGUMENT_TYPE_POSTFIX = "#";
08  
09     private int intValue = 0;
10  
11     private IntegerArgumentMarshaler( String argumentId ){
12         super( argumentId );
13     }
14  
15     protected IntegerArgumentMarshaler(){
16         thisnull );
17     }
18  
19     @Override
20     public ArgumentMarshaler newInstance(String argumentId) {
21         return new IntegerArgumentMarshaler( argumentId );
22     }
23  
24     @Override
25     public String getArgumentTypePostfix() {
26         return ARGUMENT_TYPE_POSTFIX;
27     }
28  
29     @Override
30     public Pattern getArgumentIdPattern() {
31         return ARGUMENT_ID_PATTERN;
32     }
33  
34     @Override
35     public int getNumberOfArgs() {
36         return 1;
37     }
38  
39     public void set(String... arguments) throws ArgsException {
40  
41         if( arguments.length == 0 || arguments[0] == null ){
42             throw new ArgsException( ErrorCode.MISSING_INTEGER, getArgumentId(), null );
43         }
44  
45         try {
46             intValue = Integer.parseInt(arguments[0]);
47         }
48         catch (NumberFormatException e) {
49             throw new ArgsException( ErrorCode.INVALID_INTEGER, getArgumentId(), arguments[0] );
50         }
51     }
52  
53     public Object get() {
54       return intValue;
55     }
56   }

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;
03  
04 public class ArgumentPopulator {
05  
06     private List<String> argsList = null;
07  
08     public static ArgumentPopulator distribute( List<String> argsList ){
09         return new ArgumentPopulator( argsList );
10     }
11  
12     private ArgumentPopulator( List<String> argsList ){
13         this.argsList = argsList;
14     }
15  
16     public void to( Marshalers marshalers ) throws ArgsException{
17  
18         for ( Iterator<String> arguments = argsList.iterator(); arguments.hasNext(); ) {
19  
20             String argument = arguments.next();
21  
22             if ( representsArgumentId( argument ) ){
23  
24                 String argumentId = extractArgumentIdFrom( argument );
25  
26                 if( ! marshalers.isMarshalerAvailabeFor( argumentId ) ){
27                     throw new ArgsException( ArgsException.ErrorCode.UNEXPECTED_ARGUMENT, argumentId, null);
28                 }
29  
30                 ArgumentMarshaler marshaler = marshallers.getMarshalerFor( argumentId );                           
31  
32                 marshaler.set( getArgumentsAhead( marshaller.getNumberOfArgs(), arguments ) );
33             }
34         }
35     }
36  
37     private String extractArgumentIdFrom(String argument) {
38         return argument.substring( 1, argument.length() );
39     }
40  
41     private boolean representsArgumentId(String argument) {
42         return argument.startsWith( "-" );
43     }
44  
45     private String[] getArgumentsAhead( int numberOfArgs, Iterator<String> arguments ) {
46           String[] args = new String[numberOfArgs];
47  
48           forint i=0; i < numberOfArgs; i++ ){
49               args[i] = arguments.hasNext() ? arguments.next() : null;
50           }
51           return args;
52       }
53 }

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章