Skip to content

Commit a412af4

Browse files
committed
simplifying argument inference, adding clear ArgumentType
1 parent 1342043 commit a412af4

19 files changed

Lines changed: 137 additions & 153 deletions

File tree

‎koans/app/config/i18n/messages_en.properties‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ClassArg.description=Switch is optional, app tries to find a class definition fo
7676
ClassArg.error=
7777
ClassArg.success=
7878

79+
Clear.args= -clear
80+
Clear.description=Clears compiled artifacts and stored file system timestamps.
81+
Clear.error=Unable to delete filesystem_hashes.dat or bin directory.
82+
Clear.success=Classes and file system timestamps deleted successfully.
83+
7984
Debug.args= -debug, debug, d
8085
Debug.description=Enable debug state in the app.
8186
Debug.error=

‎lib/file-monitor/src/com/sandwich/util/io/FileMonitor.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,13 @@ public boolean isFileModifiedSinceLastPoll(String filePath, Long lastModified) {
8585
public void updateFileSaveTime(File file) {
8686
fileHashesByDirectory.put(file.getAbsolutePath(), file.lastModified());
8787
}
88+
89+
public void writeChanges() {
90+
try {
91+
fileHashesHelper.write(getFilesystemHashes());
92+
} catch (IOException e) {
93+
e.printStackTrace();
94+
}
95+
}
8896

8997
}

‎lib/koans-lib/src/com/sandwich/koan/cmdline/CommandLineArgument.java‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package com.sandwich.koan.cmdline;
22

3+
import java.util.Arrays;
4+
35
import com.sandwich.koan.constant.ArgumentType;
46

57
public class CommandLineArgument implements Runnable, Comparable<CommandLineArgument> {
68
private ArgumentType argType;
7-
private String value;
9+
private String[] values;
810
boolean isPlantedByApp = false;
9-
public CommandLineArgument(ArgumentType argType, String value) {
10-
this(argType, value, false);
11+
public CommandLineArgument(ArgumentType argType, String... values) {
12+
this(argType, false, values);
1113
}
1214

13-
public CommandLineArgument(ArgumentType argType, String value, boolean isPlantedByApp) {
15+
public CommandLineArgument(ArgumentType argType, boolean isPlantedByApp, String... values) {
1416
this.argType = argType;
15-
this.value = value;
17+
this.values = values;
1618
this.isPlantedByApp = isPlantedByApp;
1719
}
1820

19-
public String getValue() {
20-
return value;
21+
public String[] getValues() {
22+
return values;
2123
}
2224

2325
public ArgumentType getArgumentType() {
@@ -33,7 +35,7 @@ public boolean isPlantedByApp() {
3335
}
3436

3537
public void run(){
36-
argType.run(value);
38+
argType.run(values);
3739
}
3840

3941
public int compareTo(CommandLineArgument o) {
@@ -49,15 +51,15 @@ public int compareTo(CommandLineArgument o) {
4951
@Override
5052
public String toString() {
5153
return new StringBuilder("CommandLineArgument [argType=").append(argType)
52-
.append(", value=").append(value).append("]").toString();
54+
.append(", value=").append(Arrays.toString(values)).append("]").toString();
5355
}
5456

5557
@Override
5658
public int hashCode() {
5759
final int prime = 31;
5860
int result = 1;
5961
result = prime * result + ((argType == null) ? 0 : argType.hashCode());
60-
result = prime * result + ((value == null) ? 0 : value.hashCode());
62+
result = prime * result + ((values == null) ? 0 : Arrays.hashCode(values));
6163
return result;
6264
}
6365

@@ -72,10 +74,10 @@ public boolean equals(Object obj) {
7274
CommandLineArgument other = (CommandLineArgument) obj;
7375
if (argType != other.argType)
7476
return false;
75-
if (value == null) {
76-
if (other.value != null)
77+
if (values == null) {
78+
if (other.values != null)
7779
return false;
78-
} else if (!value.equals(other.value))
80+
} else if (!Arrays.equals(values, other.values))
7981
return false;
8082
return true;
8183
}
Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.sandwich.koan.cmdline;
22

3+
import java.util.ArrayList;
34
import java.util.LinkedHashMap;
4-
import java.util.Map;
5+
import java.util.List;
56
import java.util.Map.Entry;
7+
import java.util.logging.Logger;
68

79
import com.sandwich.koan.ApplicationSettings;
8-
import com.sandwich.koan.KoanClassLoader;
910
import com.sandwich.koan.constant.ArgumentType;
1011
import com.sandwich.koan.ui.SuitePresenter;
1112
import com.sandwich.koan.util.ApplicationUtils;
@@ -15,78 +16,45 @@ public class CommandLineArgumentBuilder extends LinkedHashMap<ArgumentType, Comm
1516
private static final long serialVersionUID = 7635285665311420603L;
1617

1718
public CommandLineArgumentBuilder(String...args){
18-
for(int index = 0; index < args.length;){
19-
String stringArg = args[index];
20-
if(stringArg == null || stringArg.trim().length() == 0){
21-
index++;
22-
continue;
23-
}
24-
// is incremented in test of ternary
25-
String stringArgPlusOne = args.length <= ++index ? null : args[index];
26-
stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne.trim().length() == 0
27-
? null : stringArgPlusOne;
28-
ArgumentType argumentType = ArgumentType.findTypeByString(stringArg);
29-
ArgumentType argumentTypePlusOne = ArgumentType.findTypeByString(stringArgPlusOne);
30-
if(argumentType != null){ // matches an anticipated argument string
31-
// so does next value, must be an argument too, reevaluate w/ index incremented only once above
32-
if(argumentTypePlusOne instanceof ArgumentType){
33-
put(argumentType, new CommandLineArgument(argumentType, null));
34-
// intentionally increment past next argument (only one) and
35-
// evaluate in next iteration with its following argument
36-
// - in other words, argumentType will be argumentTypePlusOne in next iteration
37-
continue;
38-
}
39-
// ok 2nd argument wasn't a recognized argument type - go
40-
// ahead and see if it's a class, if not, it must be a
41-
// method - or bogus
42-
else if(stringArgPlusOne != null){
43-
guessAtMethodAndClass(this, stringArgPlusOne);
44-
}
45-
else{
46-
put(argumentType, new CommandLineArgument(argumentType, null));
47-
}
48-
index++;
49-
continue;
50-
}else if(stringArg != null){
51-
// no flag, but a string - likely class or class and method
52-
guessAtMethodAndClass(this, stringArg);
53-
// do not increment again, bump stringArgPlusOne into stringArg from prior increment
54-
continue;
55-
}
56-
}
57-
applyAssumedStartupBehaviors();
58-
}
59-
60-
private static void guessAtMethodAndClass(
61-
Map<ArgumentType, CommandLineArgument> commandLineArguments,
62-
String potentialClassOrMethod) {
63-
boolean hasMethod = commandLineArguments.containsKey(ArgumentType.METHOD_ARG);
64-
boolean hasClass = commandLineArguments.containsKey(ArgumentType.CLASS_ARG);
65-
try{
66-
if(potentialClassOrMethod != null){
67-
if(!hasClass){
68-
commandLineArguments.put(ArgumentType.CLASS_ARG,
69-
new CommandLineArgument(ArgumentType.CLASS_ARG,
70-
KoanClassLoader.getInstance().loadClass(potentialClassOrMethod).getName()));
71-
}else if(!hasMethod){
72-
commandLineArguments.put(ArgumentType.METHOD_ARG,
73-
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));
19+
if(args.length == 0){
20+
put(ArgumentType.RUN_KOANS, new CommandLineArgument(ArgumentType.RUN_KOANS, new String[0]));
21+
} else if (args.length == 1 && ArgumentType.findTypeByString(args[0]) == null){
22+
put(ArgumentType.CLASS_ARG, new CommandLineArgument(ArgumentType.CLASS_ARG, args[0]));
23+
} else if (args.length == 2 && ArgumentType.findTypeByString(args[0]) == null && ArgumentType.findTypeByString(args[1]) == null){
24+
put(ArgumentType.CLASS_ARG, new CommandLineArgument(ArgumentType.CLASS_ARG, args[0]));
25+
put(ArgumentType.METHOD_ARG, new CommandLineArgument(ArgumentType.METHOD_ARG, args[1]));
26+
} else {
27+
ArgumentType type = null;
28+
List<String> params = null;
29+
for(int index = 0; index < args.length; index++){
30+
ArgumentType tmpType = ArgumentType.findTypeByString(args[index]);
31+
if(tmpType == null){
32+
if(type == null){
33+
Logger.getAnonymousLogger().warning("The argument: " + args[index] + " is not recognized, it will be ignored");
34+
}else{
35+
params.add(args[index]);
36+
}
37+
}else{
38+
if(type == null){
39+
type = tmpType;
40+
params = new ArrayList<String>();
41+
}else{
42+
put(type, new CommandLineArgument(type, params.toArray(new String[params.size()])));
43+
type = null;
44+
params = new ArrayList<String>();
45+
}
7446
}
7547
}
76-
}catch(Exception cnfe2){
77-
if(!hasMethod){
78-
commandLineArguments.put(ArgumentType.METHOD_ARG,
79-
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));
80-
}else{
81-
throw new IllegalArgumentException(potentialClassOrMethod
82-
+ " does not match an expected argument, nor value.");
48+
if(type != null && params != null){
49+
put(type, new CommandLineArgument(type, params.toArray(new String[params.size()])));
8350
}
8451
}
52+
applyAssumedStartupBehaviors();
8553
}
8654

8755
void applyAssumedStartupBehaviors() {
8856
if(ApplicationUtils.isFirstTimeAppHasBeenRun()){
89-
ArgumentType.BACKUP.run(null);
57+
ArgumentType.BACKUP.run(new String[0]);
9058
ApplicationUtils.getPresenter().clearMessages();
9159
}
9260
if(isEmpty() || !containsKey(ArgumentType.RUN_KOANS) && (
@@ -100,7 +68,7 @@ void applyAssumedStartupBehaviors() {
10068
presenter.displayMessage("Value: '"+argEntry.getValue()+"'");
10169
}
10270
}
103-
put(ArgumentType.RUN_KOANS, new CommandLineArgument(ArgumentType.RUN_KOANS, null, true));
71+
put(ArgumentType.RUN_KOANS, new CommandLineArgument(ArgumentType.RUN_KOANS, true, new String[0]));
10472
}
10573
}
10674
}

‎lib/koans-lib/src/com/sandwich/koan/cmdline/behavior/ArgumentBehavior.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public interface ArgumentBehavior {
55

6-
void run(String value) throws Exception;
6+
void run(String... values) throws Exception;
77
String getErrorMessage();
88
String getSuccessMessage();
99

‎lib/koans-lib/src/com/sandwich/koan/cmdline/behavior/ClassArg.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
public class ClassArg extends AbstractArgumentBehavior {
66

7-
public void run(String koanSuiteClassName) {
8-
if(koanSuiteClassName != null && koanSuiteClassName.trim().length() != 0){
9-
PathToEnlightenment.filterBySuite(koanSuiteClassName);
7+
public void run(String...koanSuiteClassName) {
8+
if(koanSuiteClassName != null &&
9+
koanSuiteClassName.length > 0 &&
10+
koanSuiteClassName[0] != null &&
11+
koanSuiteClassName[0].trim().length() != 0){
12+
PathToEnlightenment.filterBySuite(koanSuiteClassName[0]);
1013
}
1114
}
1215

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.sandwich.koan.cmdline.behavior;
2+
3+
import java.io.File;
4+
5+
import com.sandwich.util.io.directories.DirectoryManager;
6+
7+
public class Clear extends AbstractArgumentBehavior {
8+
9+
public void run(String... values) throws Exception {
10+
new File(DirectoryManager.getDataFile()).delete();
11+
new File(DirectoryManager.getBinDir()).delete();
12+
}
13+
14+
}

‎lib/koans-lib/src/com/sandwich/koan/cmdline/behavior/Debug.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
public class Debug extends AbstractArgumentBehavior{
66

7-
public void run(String arg){
7+
public void run(String... args){
88
ApplicationSettings.setDebug(ApplicationSettings.isDebug() ||
9-
"true".equalsIgnoreCase(arg) ||
10-
arg == null ||
11-
arg.trim().length() == 0);
9+
"true".equalsIgnoreCase(args[0]) ||
10+
args[0] == null ||
11+
args[0].trim().length() == 0);
1212
}
1313

1414
}

‎lib/koans-lib/src/com/sandwich/koan/cmdline/behavior/DefaultNoArg.java‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎lib/koans-lib/src/com/sandwich/koan/cmdline/behavior/Help.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class Help extends AbstractArgumentBehavior {
1313

14-
public void run(String value) {
14+
public void run(String...values) {
1515
StringBuilder sb = new StringBuilder(
1616
"Java Koans - HELP").append(KoanConstants.EOL);
1717
line(sb, "-----------------");

0 commit comments

Comments
 (0)