-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor entry retrieval and processing
- Loading branch information
Showing
10 changed files
with
199 additions
and
293 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 4 additions & 13 deletions
17
src/main/java/org/dsikkema/jamphony/jamphony/io/ArgumentDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,16 @@ | ||
package org.dsikkema.jamphony.jamphony.io; | ||
|
||
public class ArgumentDefinition { | ||
public class ArgumentDefinition extends EntryDefinition { | ||
private String name; | ||
private int index; | ||
private Type type; | ||
|
||
public ArgumentDefinition(String name, int index, Type type) { | ||
this.name = name; | ||
this.index = index; | ||
this.type = type; | ||
super(name, type); | ||
this.index = index; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getIndex() { | ||
return index; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
return this.index; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/org/dsikkema/jamphony/jamphony/io/EntryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.dsikkema.jamphony.jamphony.io; | ||
|
||
/** | ||
* "Entry" means an argument, option, or flag. This serves as a generic way to hold entries and their values, and | ||
* encapsulate retrieving them by type | ||
*/ | ||
public class EntryData { | ||
|
||
private final EntryDefinition definition; | ||
private final Object val; | ||
|
||
public EntryData(EntryDefinition definition, String val) throws InputException { | ||
switch (definition.getType()) { | ||
case INT: | ||
if (!this.isInt(val)) { | ||
throw new InputException(String.format( | ||
"Entry '%s' with value '%s' does not match expected type '%s'", | ||
definition.getName(), | ||
val, | ||
definition.getType().getName() | ||
)); | ||
} | ||
this.val = Integer.valueOf(val); | ||
break; | ||
case STRING: | ||
this.val = val; | ||
break; | ||
|
||
default: | ||
throw new RuntimeException("Unhandled argument type"); // just to make this compile. Should never be hit | ||
} | ||
|
||
this.definition = definition; | ||
} | ||
|
||
public String getStringValue() { | ||
if (this.definition.getType() != Type.STRING) { | ||
throw new RuntimeException("Argument '" + definition.getName() + "' is not of type " + Type.STRING.getName()); | ||
} | ||
|
||
return (String)this.val; | ||
} | ||
|
||
public int getIntValue() { | ||
if (this.definition.getType() != Type.INT) { | ||
throw new RuntimeException("Argument '" + definition.getName() + "' is not of type " + Type.INT.getName()); | ||
} | ||
|
||
return (int)this.val; | ||
} | ||
|
||
private boolean isInt(String in) { | ||
try { | ||
Integer.parseInt(in); | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/dsikkema/jamphony/jamphony/io/EntryDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.dsikkema.jamphony.jamphony.io; | ||
|
||
public class EntryDefinition { | ||
private String name; | ||
private Type type; | ||
|
||
public EntryDefinition(String name, Type type) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
} |
Oops, something went wrong.