diff --git a/src/main/java/chapter14/args/Args.java b/src/main/java/chapter14/args/Args.java index 0e79af4..60e6b82 100644 --- a/src/main/java/chapter14/args/Args.java +++ b/src/main/java/chapter14/args/Args.java @@ -5,15 +5,15 @@ public class Args { private String schema; - private String[] args; private boolean valid = true; private Set unexpectedArguments = new TreeSet<>(); private Map marshalers = new HashMap<>(); private Set argsFound = new HashSet<>(); - private int currentArgument; + private Iterator currentArgument; private char errorArgumentId = '\0'; private String errorParameter = "TILT"; private ErrorCode errorCode = ErrorCode.OK; + private List argsList; enum ErrorCode { OK, MISSING_STRING, MISSING_INTEGER, INVALID_INTEGER, UNEXPECTED_ARGUMENT @@ -21,12 +21,12 @@ enum ErrorCode { public Args(String schema, String[] args) throws ParseException { this.schema = schema; - this.args = args; + argsList = Arrays.asList(args); valid = parse(); } private boolean parse() throws ParseException { - if (schema.length() == 0 && args.length == 0) + if (schema.length() == 0 && argsList.size() == 0) return true; parseSchema(); try { @@ -83,8 +83,8 @@ private boolean isIntegerSchemaElement(String elementTail) { } private boolean parseArguments() throws ArgsException { - for (currentArgument = 0; currentArgument < args.length; currentArgument++) { - String arg = args[currentArgument]; + for (currentArgument = argsList.iterator(); currentArgument.hasNext(); ) { + String arg = currentArgument.next(); parseArgument(arg); } return true; @@ -113,54 +113,16 @@ private void parseElement(char argChar) throws ArgsException { private boolean setArgument(char argChar) throws ArgsException { ArgumentMarshaler m = marshalers.get(argChar); + if (m == null) + return false; try { - if (m instanceof BooleanArgumentMarshaler) - setBooleanArg(m); - else if (m instanceof StringArgumentMarshaler) - setStringArg(m); - else if (m instanceof IntegerArgumentMarshaler) - setIntArg(m); - else - return false; + m.set(currentArgument); + return true; } catch (ArgsException e) { valid = false; errorArgumentId = argChar; throw e; } - return true; - } - - private void setIntArg(ArgumentMarshaler m) throws ArgsException { - currentArgument++; - String parameter = null; - try { - parameter = args[currentArgument]; - m.set(parameter); - } catch (ArrayIndexOutOfBoundsException e) { - errorCode = ErrorCode.MISSING_INTEGER; - throw new ArgsException(); - } catch (ArgsException e) { - errorParameter = parameter; - errorCode = ErrorCode.INVALID_INTEGER; - throw e; - } - } - - private void setStringArg(ArgumentMarshaler m) throws ArgsException { - currentArgument++; - try { - m.set(args[currentArgument]); - } catch (ArrayIndexOutOfBoundsException e) { - errorCode = ErrorCode.MISSING_STRING; - throw new ArgsException(); - } - } - - private void setBooleanArg(ArgumentMarshaler m) { - try { - m.set("true"); - } catch (ArgsException e) { - } } public int cardinality() { @@ -244,17 +206,17 @@ public boolean isValid() { private class ArgsException extends Exception { } - private abstract class ArgumentMarshaler { - public abstract void set(String s) throws ArgsException; + private interface ArgumentMarshaler { + void set(Iterator currentArgument) throws ArgsException; - public abstract Object get(); + Object get(); } - private class BooleanArgumentMarshaler extends ArgumentMarshaler { + private class BooleanArgumentMarshaler implements ArgumentMarshaler { private boolean booleanValue = false; @Override - public void set(String s) { + public void set(Iterator currentArgument) throws ArgsException { booleanValue = true; } @@ -264,12 +226,17 @@ public Object get() { } } - private class StringArgumentMarshaler extends ArgumentMarshaler { + private class StringArgumentMarshaler implements ArgumentMarshaler { private String stringValue = ""; @Override - public void set(String s) { - stringValue = s; + public void set(Iterator currentArgument) throws ArgsException { + try { + stringValue = currentArgument.next(); + } catch (NoSuchElementException e) { + errorCode = ErrorCode.MISSING_STRING; + throw new ArgsException(); + } } @Override @@ -278,14 +245,21 @@ public Object get() { } } - private class IntegerArgumentMarshaler extends ArgumentMarshaler { + private class IntegerArgumentMarshaler implements ArgumentMarshaler { private int intValue = 0; @Override - public void set(String s) throws ArgsException { + public void set(Iterator currentArgument) throws ArgsException { + String parameter = null; try { - intValue = Integer.parseInt(s); + parameter = currentArgument.next(); + intValue = Integer.parseInt(parameter); + } catch (NoSuchElementException e) { + errorCode = ErrorCode.MISSING_INTEGER; + throw new ArgsException(); } catch (NumberFormatException e) { + errorParameter = parameter; + errorCode = ErrorCode.INVALID_INTEGER; throw new ArgsException(); } }