🔖 오늘 읽은 범위 : 14장, 점진적인 개선
package com.objectmentor.utilities.args;
import static com.objectmentor.utilities.args.ArgsException.ErrorCode.*;
import java.util.*;
public class Args {
private Map<Character, ArgumentMarshaler> marshalers;
private Set<Character> argsfound;
private Listiterator<String> currentArgument;
public Args(String schema, String[] args) throws ArgsException {
marshalers = new HashMap<Character, ArgumentMarshaler>( ) ;
argsfound = new HashSet<Character>( ) ;
parseSchema(schema) ;
parseArgumentStrings(Arrays.asList(args)) ;
}
private void parseSchema(String schema) throws ArgsException {
for (String element : schema.split(","))
if (element.length( ) > 0)
parseSchemaElement(element.trim( )) ;
}
private void parseSchemaElement(String element) throws ArgsException {
char elementId = element.charAt(0) ;
String elementTail = element.substring(1) ;
validateSchemaElementid(elementId) ;
if(elementTail.length() == 0)
marshalers.put(elementId, new BooleanArgumentMarshaled()) ;
else if(elementTail.equals("*"))
marshalers.put(elementId, new StringArgumentMarshaled()) ;
else if(elementTail.equals("#"))
marshalers.put(elementId, new IntegerArgumentMarshaled()) ;
else if(elementTail.equals("##"))
marshalers.put(elementId, new DoubleArgumentMarshaled()) ;
else if(elementTail.equals("[*]"))
marshalers.put(elementId, new StringArrayArgumentMarshaled()) ;
else
throw new ArgsException(INVALID_ARGUMENT_FORMAT, elementId, null);
}
private void validateSchemaElementId(char elementId) throws ArgsException {
if(!Character.isLetter(elementId))
throw new ArgsException(INVALID_ARGLMENT_NAME, elementId , null) ;
}
private void ParseArgumentStrings(List<String> argslist) throws ArgsException {
for(currentArgument = argslist.listIterator(); currentArgument,hasNext();) {
String argString = currentArgument.next();
if(argString.startsWith("-")) {
parseArgumentCharacters(argString.substring(1)) ;
} else {
currentArgument.previous() ;
break;
}
}
}
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)) ;
}
public int getInt(char arg) {
return IntegerArgumentMarshaler.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)) ;
}
}
public interface ArgumentMarshaler {
void set(Iterator<String> currentArgument) throws ArgsException;
}
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 ;
}
}
import static com.objectmentor.utilities.args.ArgsException.ErrorCode. *;
public class StringArgumentMarshaler implements ArgumentMarshaler {
private String stringValue = "";
public void set(Iterator<String> currentArgument) throws ArgsException {
try {
stringValue = currentArgument.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 "";
}
}
import static com.objectmentor.utilities.args.ArgsException.ErrorCode. *;
public class IntegerArgumentMarshaler implements ArgumentMarshaler {
private int intValue = 0;
public void set(Iterator<String> currentArgument) throws ArgsException {
String parameter = null;
try {
parameter = currentArgument.next();
intValue = Integer.paresInt(paramenter);
} catch(NoSuchElementException e) {
throw new ArgsException(MISSING_INTEGER);
} catch(NumberFormatException e)){
throw new ArgsException(INVALID_INTEGER, parameter);
}
}
public static int getValue(ArgumentMarshaler am) {
if (am != null && am instanceof IntegerArgumentMarshaler)
return ((IntegerArgumentMarshaler) am).intValue;
else
return 0;
}
}
import static com.objectmentor.utilities.args.ArgsException.ErrorCode.*;
public class ArgsException extends Exception {
Private char errorArgumentid = '\0' ;
Private String errorParameter = null;
Private ErrorCode errorCode = OK;
public ArgsException() {}
public ArgsException(String message) {super(message);}
public ArgsException(ErrorCode errorCode) {
this.errorCode = errorCode;
}
public ArgsException(ErrorCode errorCode, String errorParameter) {
this.errorCode = errorCode;
this.errorParameter = errorParameter;
}
public ArgsException(ErrorCode errorCode,
char errorArgumentId, String errorParameter) {
this.errorCode = errorCode;
this.errorParameter = errorParameter;
this.errorArgumentId= errorArgumentId;
}
public char getErrorArgumentId() {
return errorArgumentId;
}
public void setErrorArgumentld(char errorArgumentId} {
this.errorArgumentId= errorArgumentId;
}
public String getErrorParameter( ) {
return errorParameter;
}
public void setErrorParameter(String errorParameter) {
this.errorParameter = errorParameter;
}
public ErrorCode getErrorCode( ) {
return errorCode;
}
public void setErrorCode(ErrorCode errorCode) {
this.errorCode = errorCode;
}
public String errorMessage( ) {
switch(errorCode) {
case OK:
return "TILT: Should not get here.";
case UNEXPECTED_ARGUMENT:
return String.format("Argument -%c unexpected.", errorArgumentId) ;
case MISSING_STRING:
return return String.format("Could not find string parameter for -%c.", errorArgumentId) ;
case INVALID_INTEGER:
return String.format("Argument -%c expects an integer but was '%s'.", errorArgumentId, errorParameter) ;
case MISSING_INTEGER:
return String.format("Could not find integer parameter for -%c.", errorArgumentId) ;
case INVALID_DOUBLE:
return String.format("Argument -%c expects a double but was '%s'.", errorArgumentId, errorParameter) ;
case MISSING_DOUBLE:
return String.format("Could not find double parameter for -%c.", errorArgumentId) ;
case INVALID_ARGUMENT_NAME:
return String.format("'%c' is not a valid argument name.", errorArgumentId) ;
case INVALID_ARGUMENT_FORMAT:
return String.format("'%s' is not a valid argument format.", errorParameter) ;
}
return "";
}
public enum ErrorCode {
OK, INVALID_ARGLMENT_FOOOT, LNEXPECTED_ARGLMENT,
INVALID_ARGLMENT_NAME ,
MISSING_STRING ,
MISSING_INTEGER, INVALID_INTEGER,
MISSING_DOUBLE , INVALID_DOUBLE }
}
https://github.com/kdhyo/BooksReviews/tree/main/cleancode/Chapter14
이번 파트는 코드를 좀 보면서 해야될 것 같아서... 이번 파트에 있는 코드들 정리해두었는데, 필요하신 분들은 참고해주시면 좋을 것 같아요!