Refining Uncle Bob’s Clean Code(三)

But now, let’s take a closer look at the new interface of ArgumentMarshaler:

01 public interface ArgumentMarshaler {
02  
03     public boolean canHandle( String schemaElement );
04  
05     public int getNumberOfArgs();
06  
07     public ArgumentMarshaler newInstanceFor( String schemaElement );
08  
09     public String getArgumentId();
10  
11     public void set( String...arguments ) throws ArgsException;
12  
13     public Object get();
14  
15 }

As you can see, an ArgumentMarshaler is now a little bit more powerful. As said, it can decide, wether or not a given schema element can be handled by a distinct Marshaler, it also ‘knows’ about the current argumentId it’s responsible for (so we can get rid of the set ‘argsFound‘, since we only have to ‘ask’ the current Set of Marshalers about the dedicated argumentIds. In addition to that, a Marshaler knows about the number of arguments he’s capable to process. As you will see, there might be some kind of Marshalers, that may take more than one argument for conversion (like for exampe a DateArgumentMarshaler, which will take three arguments – year, month and day).

One of the most important points – each Marshaler acts as a Factory for producing new instances of its own type. We surely could have separated those two responsibilities, but for this first refinement, i’ll leave it within the Marshaler (that’s kind of ugly, i know – but i want to leave some work for the next boy scouts ;o)).

Can you handle this ?

Let’s take a closer look on how a distinct ArgumentMarshaler decides, if he can handle a given schema element. For this feature, i extended the functionality a bit. In the origin example it was only possible to allow argumentIds that consist of a single letter. But there are surely some argumentIds that might be longer than a single character (like -classpath or -keystore).

I decided to use an abstract base class that will process the most part of the decision, leaving only the ‘details’ to the implementations:

01 import java.util.regex.Pattern;
02  
03 public abstract class AbstractArgumentMarshaler implements ArgumentMarshaler {
04  
05     private String argumentId = null;
06  
07     public AbstractArgumentMarshaler( String argumentId ){
08         this.argumentId = argumentId;
09     }
10  
11     @Override
12     public String getArgumentId() {
13         return argumentId;
14     }
15  
16     public abstract Pattern getArgumentIdPattern();
17  
18     public abstract String getArgumentTypePostfix();
19  
20     public abstract ArgumentMarshaler newInstance( String argumentId );
21  
22     public boolean canHandle( String element ){
23  
24         if( element.length() < getArgumentTypePostfix().length() ){
25             return false;
26         }
27  
28         String argumentId = removeArgumentTypePostfixFrom( element );
29  
30         return
31             element.endsWith( getArgumentTypePostfix() ) &&
32             consistsAtLeastOfOneCharacter( argumentId ) &&
33             matches( argumentId, getArgumentIdPattern() );
34     }
35  
36     public ArgumentMarshaler newInstanceFor( String schemaElement ){
37         return newInstance( removeArgumentTypePostfixFrom( schemaElement ) );
38     }
39  
40     private boolean consistsAtLeastOfOneCharacter( String argumentId ){
41         return argumentId != null && argumentId.length() > 0;
42     }
43  
44     private boolean matches( String argumentId, Pattern regexp ){
45         return regexp.matcher( argumentId ).matches();
46     }
47  
48     private String removeArgumentTypePostfixFrom( String element ){
49         return element.substring( 0, ( element.length() - getArgumentTypePostfix().length() ) );
50     }
51 }

As you might see, AbstractArgumentMarshaler puts some burden on its subclasses (the mentioned details). It needs to know the argumentTypePostfix (like ‘*’) and the so called argumentIdPattern – a regexp pattern, constraining the possible instantiations of allowed argumentIds. To be compatible with the origin behaviour, we would only allow a single character argumentId ( ‘[a-z]‘).

… yes, I can handle this !

When inheriting from AbstractArgumentMarshaler, there’s again not too much more left to do than fulfilling the claimed template methods and processing the passed arguments. Let’s look at some concrete ArgumentMarshaler:

01 import java.util.Calendar;
02 import java.util.Date;
03 import java.util.regex.Pattern;
04  
05 import com.mgi.util.args.ArgsException.ErrorCode;
06  
07 public class DateArgumentMarshaler extends AbstractArgumentMarshaler {
08  
09     private static final Pattern ARGUMENT_ID_PATTERN = Pattern.compile( "([a-z]|[A-Z])*" );
10     private static final String ARGUMENT_TYPE_POSTFIX = "<date>";
11  
12     private Date date = null;
13  
14     public DateArgumentMarshaler( String argumentId ) {
15         super(argumentId);
16     }
17  
18     public DateArgumentMarshaler(){
19         thisnull );
20     }
21  
22     @Override
23     public ArgumentMarshaler newInstance(String argumentId) {
24         return new DateArgumentMarshaler( argumentId );
25     }
26  
27     @Override
28     public Pattern getArgumentIdPattern() {
29         return ARGUMENT_ID_PATTERN;
30     }
31  
32     @Override
33     public String getArgumentTypePostfix() {
34         return ARGUMENT_TYPE_POSTFIX;
35     }
36  
37     @Override
38     public int getNumberOfArgs() {
39         return 3;
40     }
41  
42     @Override
43     public void set(String... arguments) throws ArgsException {
44  
45         assertValid( arguments );
46  
47         try{
48             Calendar calendar = Calendar.getInstance();
49             calendar.set( Calendar.DATE, Integer.parseInt( arguments[0] ) );
50             calendar.set( Calendar.MONTH, Integer.parseInt( arguments[1] ) - 1 );
51             calendar.set( Calendar.YEAR, Integer.parseInt( arguments[2] ) );
52             calendar.set( Calendar.MINUTE, 0 );
53             calendar.set( Calendar.SECOND, 0 );
54             calendar.set( Calendar.MILLISECOND, 0 );
55  
56             this.date = calendar.getTime();
57         }
58         catch (NumberFormatException e) {
59             throw new ArgsException( ErrorCode.INVALID_DATE_ELEMENT, getArgumentId(), arguments[0] );
60         }
61     }
62  
63     private void assertValid( String... arguments ) throws ArgsException {
64  
65         if( arguments[0] == null ||
66             arguments[1] == null ||
67             arguments[2] == null ){
68  
69             throw new ArgsException( ErrorCode.MISSING_DATE_ELEMENT, getArgumentId(), null );
70         }
71     }
72  
73     @Override
74     public Object get() {
75         return date;
76     }
77 }

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