Chapter 14: Successive Renement Listing 14-2 (continued) Args.java private void parseArgumentCharacters(String argChars) throws ArgsException { for (int i = 0; i < argChars.length(); i++) parseArgumentCharacter(argChars.charAt(i)); } private void parseArgumentCharacter(char argChar) throws ArgsException { ArgumentMarshaler m = marshalers.get(argChar); if (m == null) { throw new ArgsException(UNEXPECTED_ARGUMENT, argChar, null); } else { argsFound.add(argChar); try { m.set(currentArgument); } catch (ArgsException e) { e.setErrorArgumentId(argChar); throw e; } } } public boolean has(char arg) { return argsFound.contains(arg); } public int nextArgument() { return currentArgument.nextIndex(); } public boolean getBoolean(char arg) { return BooleanArgumentMarshaler.getValue(marshalers.get(arg)); } public String getString(char arg) { return StringArgumentMarshaler.getValue(marshalers.get(arg)); }
id: e1f9914e9d22adf1a0c4f18681490bad - page: 213
getValue(marshalers.get(arg)); } public double getDouble(char arg) { return DoubleArgumentMarshaler.getValue(marshalers.get(arg)); } public String[] getStringArray(char arg) { return StringArrayArgumentMarshaler.getValue(marshalers.get(arg)); } } Notice that you can read this code from the top to the bottom without a lot of jumping around or looking ahead. The one thing you may have had to look ahead for is the denition of ArgumentMarshaler, which I left out intentionally. Having read this code carefully,
id: 66b0f1bc2b5f471fc3a4f69c142741c9 - page: 213
Args Implementation you should understand what the ArgumentMarshaler interface is and what its derivatives do. Ill show a few of them to you now (Listing 14-3 through Listing 14-6). Listing 14-3 ArgumentMarshaler.java public interface ArgumentMarshaler { void set(Iterator<String> currentArgument) throws ArgsException; } Listing 14-4 BooleanArgumentMarshaler.java public class BooleanArgumentMarshaler implements ArgumentMarshaler { private boolean booleanValue = false; public void set(Iterator<String> currentArgument) throws ArgsException { booleanValue = true; } public static boolean getValue(ArgumentMarshaler am) { if (am != null && am instanceof BooleanArgumentMarshaler) return ((BooleanArgumentMarshaler) am).booleanValue; else return false; } } Listing 14-5 StringArgumentMarshaler.java import static com.objectmentor.utilities.args.ArgsException.ErrorCode.*; public class StringArgumentMarshaler implements ArgumentMarshaler { private String stringValue = "";
id: ff07d1779828cd2d3dc0fb8956dabe64 - page: 214
next(); } catch (NoSuchElementException e) { throw new ArgsException(MISSING_STRING); } } public static String getValue(ArgumentMarshaler am) { if (am != null && am instanceof StringArgumentMarshaler) return ((StringArgumentMarshaler) am).stringValue; else return ""; } } 197 198
id: 864fda68f65b35197422bde4fe0fa087 - page: 214