Skip to content

Commit 0b836a8

Browse files
committed
externalized some strings, refactored argumenttype a bit
1 parent 82551ab commit 0b836a8

12 files changed

Lines changed: 132 additions & 47 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public Map<ArgumentType, CommandLineArgument> build() {
2424
String stringArgPlusOne = args.length <= ++index ? null : args[index];
2525
stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne.trim().length() == 0
2626
? null : stringArgPlusOne;
27-
ArgumentType argumentType = ArgumentType.TYPES_BY_STRING.get(stringArg);
28-
ArgumentType argumentTypePlusOne = ArgumentType.TYPES_BY_STRING.get(stringArgPlusOne);
27+
ArgumentType argumentType = ArgumentType.findTypeByString(stringArg);
28+
ArgumentType argumentTypePlusOne = ArgumentType.findTypeByString(stringArgPlusOne);
2929
if(argumentType != null){ // matches an anticipated argument string
3030
// so does next value, must be an argument too, reevaluate w/ index incremented only once above
3131
if(argumentTypePlusOne instanceof ArgumentType){

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class Help implements ArgumentBehavior {
1212

1313
public void run(String value) {
1414
StringBuilder sb = new StringBuilder(
15-
"Java Koans - HELP!!!").append(KoanConstants.EOL);
16-
line(sb, "--------------------");
15+
"Java Koans - HELP").append(KoanConstants.EOL);
16+
line(sb, "-----------------");
1717
line(sb, "the following options are available:");
1818
ArrayList<ArgumentType> argTypes = new ArrayList<ArgumentType>(Arrays.asList(getArgumentTypes()));
1919
Collections.reverse(argTypes);

‎koans-lib/src/com/sandwich/koan/constant/ArgumentType.java‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,49 @@
1515
import com.sandwich.koan.cmdline.behavior.Reset;
1616
import com.sandwich.koan.cmdline.behavior.Test;
1717
import com.sandwich.koan.runner.RunKoans;
18+
import com.sandwich.util.Messages;
1819

1920
public enum ArgumentType implements ArgumentBehavior {
2021

21-
HELP("Help. Displays stuff to, er, help you.",
22-
new Help(), "-help", "help", "h", "?"),
23-
RESET("Restore all the koans in the src/ folder to their original (or last backed up) state.",
24-
new Reset(), "-reset"),
25-
BACKUP("Backup all the koans in the src/ for easy restoration later (useful for developing koans).",
26-
new Backup(), "-backup"),
27-
// nothing using this currently - but probably a few things that could - say, a debug presenter?
28-
DEBUG("Enable debug state in the app.",
29-
new Debug(), "-debug"),
30-
TEST("Run tests. System returns number of failing testcases - not to exceed 255 to retain DOS compatibility for BAT files.",
31-
new Test(), "-test"),
22+
HELP( getString("help_description"), new Help(), getCommandLineArgs("help_args")),
23+
RESTORE( getString("restore_description"), new Reset(), getCommandLineArgs("restore_args")),
24+
BACKUP( getString("backup_description"), new Backup(), getCommandLineArgs("backup_args")),
25+
DEBUG( getString("debug_description"), new Debug(), getCommandLineArgs("debug_args")),
26+
TEST( getString("test_description"), new Test(), getCommandLineArgs("test_args")),
3227
// important class MUST come before method - due to how Enum implements comparable and order
3328
// dependent logic later @ see ArgumentTypeTest.testClassPrecedesMethod
34-
CLASS_ARG("Switch is optional, app tries to find a class definition for any unrecognized string - which becomes a method argument if class is not found. If classcast succeeds - the class will become the only koansuite to run. Permits users/developers to focus on one suite at a time.",
35-
new ClassArg(), "-class"),
36-
METHOD_ARG("Switch is optional, results from failing to find a class definition by an unrecognized string if switch is omitted.",
37-
new MethodArg(), "-method"),
38-
RUN_KOANS("Default target. No switch - this runs if no switch is defined, or if a valid class is found as an argument.",
39-
new RunKoans(), "");
29+
CLASS_ARG( getString("class_description"), new ClassArg(), getCommandLineArgs("class_args")),
30+
METHOD_ARG( getString("method_description"), new MethodArg(), getCommandLineArgs("method_args")),
31+
RUN_KOANS( getString("default_run_description"), new RunKoans(), getCommandLineArgs("default_run_args"));
4032

4133
private final List<String> args;
4234
private final ArgumentBehavior behavior;
4335
private final String description;
36+
4437
ArgumentType(String description, ArgumentBehavior behavior, String...args){
4538
this.args = Arrays.asList(args);
4639
this.behavior = behavior;
4740
this.description = description;
4841
}
42+
static String[] getCommandLineArgs(String key) {
43+
return Messages.getCSVs(ArgumentType.class, key);
44+
}
45+
private static String getString(String key) {
46+
return Messages.getString(ArgumentType.class, key);
47+
}
4948
public List<String> args(){
5049
return args;
5150
}
5251
public String description(){
5352
return description;
5453
}
55-
public static final Map<String, ArgumentType> TYPES_BY_STRING;
54+
private static final Map<String, ArgumentType> TYPES_BY_STRING;
5655
static{
5756
Map<String, ArgumentType> types = new HashMap<String, ArgumentType>();
5857
for(ArgumentType type : ArgumentType.values()){
59-
for(String arg : type.args){
58+
for(String arg : type.args()){
6059
if(types.containsKey(arg)){
61-
throw new IllegalArgumentException("command line arg: "+arg+" is duplicated.");
60+
throw new IllegalArgumentException(Messages.getString("ArgumentType.duplicated_arg_error_part1")+arg+Messages.getString("ArgumentType.duplicated_arg_error_part2")); //$NON-NLS-1$ //$NON-NLS-2$
6261
}
6362
types.put(arg, type);
6463
}
@@ -68,6 +67,9 @@ public String description(){
6867
public void run(String value) {
6968
behavior.run(value);
7069
}
70+
public static ArgumentType findTypeByString(String stringArg) {
71+
return TYPES_BY_STRING.get(stringArg);
72+
}
7173
}
7274

7375

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ArgumentType.backup_description=Backup all the koans in the src/ for easy restoration later (useful for developing koans).
2+
ArgumentType.backup_args= -backup, backup, b
3+
ArgumentType.class_description=Switch is optional, app tries to find a class definition for any unrecognized string - which becomes a method argument if class is not found. If classcast succeeds - the class will become the only koansuite to run. Permits users/developers to focus on one suite at a time.
4+
ArgumentType.class_args= -class, class, c
5+
ArgumentType.debug_description=Enable debug state in the app.
6+
ArgumentType.debut_args= -debug, debug, d
7+
ArgumentType.default_run_description=Default target. No switch - this runs if no switch is defined, or if a valid class is found as an argument.
8+
ArgumentType.default_run_args=
9+
ArgumentType.duplicated_arg_error_part1=command line arg:
10+
ArgumentType.duplicated_arg_error_part2=\ is duplicated.
11+
ArgumentType.help_description=Help. Displays stuff to, er, help you.
12+
ArgumentType.help_args= -help, help, h, ?
13+
ArgumentType.method_description=Switch is optional, results from failing to find a class definition by an unrecognized string if switch is omitted.
14+
ArgumentType.method_args= -method, method, m
15+
ArgumentType.restore_description=Restore all the koans in the src/ folder to their original (or last backed up) state.
16+
ArgumentType.restore_args= -reset, reset, r, restore, -restore
17+
ArgumentType.test_description=Run tests. System returns number of failing testcases - not to exceed 255 to retain DOS compatibility for BAT files.
18+
ArgumentType.test_args= -test, test, t
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.sandwich.util;
2+
import static com.sandwich.koan.constant.KoanConstants.PERIOD;
3+
4+
import java.util.MissingResourceException;
5+
import java.util.ResourceBundle;
6+
7+
public class Messages {
8+
private static final String BUNDLE_NAME = "com.sandwich.koan.constant.messages"; //$NON-NLS-1$
9+
10+
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
11+
.getBundle(BUNDLE_NAME);
12+
13+
private Messages() {
14+
}
15+
16+
public static String getString(String key) {
17+
try {
18+
return RESOURCE_BUNDLE.getString(key);
19+
} catch (MissingResourceException e) {
20+
return '!' + key + '!';
21+
}
22+
}
23+
24+
public static String getString(Class<?> clazz, String key){
25+
return Messages.getString(new StringBuilder(clazz.getSimpleName()).append(PERIOD).append(key).toString());
26+
}
27+
28+
public static String[] getCSVs(Class<?> clazz, String key) {
29+
String[] tmp = getString(clazz, key).split(",");
30+
String[] trimmed = new String[tmp.length];
31+
for(int i = 0; i < tmp.length; i++){
32+
trimmed[i] = tmp[i].trim();
33+
}
34+
return trimmed;
35+
}
36+
}

‎koans/.classpath‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
5-
<classpathentry kind="lib" path="lib/koans.jar"/>
6-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
7-
<classpathentry combineaccessrules="false" kind="src" path="/koans-lib"/>
8-
<classpathentry kind="output" path="bin"/>
9-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry combineaccessrules="false" kind="src" path="/koans-lib"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

‎koans/README‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
The purpose of this project is to emulate the awesomeness of the Ruby Koans, and apply it to the Java language.
2+
3+
Why Java? Why not?
4+
5+
We've all seen plenty of bad Java code, and written it ourselves. Hopefully these koans can help people eager to learn Java; to write clean, maintainable code.
6+
17
Running Instructions:
28
1. Download and unarchive the contents of the most recent java-koans in development from:
39
https://github.com/matyb/java-koans/archives/master
410
2. Open a terminal and cd to the 'koans/src' directory from within the archive
5-
3. Run run.bat or run.sh (whichever is applicable for your OS)
6-
4. Follow the instructions on screen, and rerun the run file in the last step
11+
3. run run.bat or run.sh (whichever is applicable for your OS)
12+
4. follow the instructions on screen, and rerun the run file in the last step
713

814
Developing a Koan:
915
1. Follow any of the existing koans as an example to create a new class w/ koan methods (indicated by the @Koan annotation)
1016
2. Define the order and metadata associated with each koan in the PathToEnlightment.xml
11-
3. If necessary - use dynamic content in your lesson, examples are located in XmlVariableInjector class (and Test) and the AboutKoans.java file
17+
3. If necessary - use dynamic content in your lesson, examples are located in XmlVariableInjector class (and Test) and the AboutKoans.java file

‎koans/data/src/PathToEnlightment.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="ISO-8859-1"?>
22
<Packages>
3-
<Package pkg="beginner" name="Novice">
4-
<!--
5-
TODO: AboutKoans...
3+
<Package pkg="beginner" name="Beginner">
4+
<!--
5+
TODO: AboutKoans...
66
-->
77
<Suite class="AboutKoans">
88
<Koan name="findAboutKoansFile"

‎koans/lib/koans.jar‎

30.5 KB
Binary file not shown.

‎koans/src/PathToEnlightment.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="ISO-8859-1"?>
22
<Packages>
3-
<Package pkg="beginner" name="Novice">
4-
<!--
5-
TODO: AboutKoans...
3+
<Package pkg="beginner" name="Beginner">
4+
<!--
5+
TODO: AboutKoans...
66
-->
77
<Suite class="AboutKoans">
88
<Koan name="findAboutKoansFile"

0 commit comments

Comments
 (0)