Skip to content

Commit 77e6426

Browse files
committed
Map Arguments
1 parent 25eccf6 commit 77e6426

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

src/com/cleancoder/args/Args.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ else if (elementTail.equals("##"))
3737
marshalers.put(elementId, new DoubleArgumentMarshaler());
3838
else if (elementTail.equals("[*]"))
3939
marshalers.put(elementId, new StringArrayArgumentMarshaler());
40+
else if (elementTail.equals("&"))
41+
marshalers.put(elementId, new MapArgumentMarshaler());
4042
else
4143
throw new ArgsException(INVALID_ARGUMENT_FORMAT, elementId, elementTail);
4244
}
@@ -105,4 +107,8 @@ public double getDouble(char arg) {
105107
public String[] getStringArray(char arg) {
106108
return StringArrayArgumentMarshaler.getValue(marshalers.get(arg));
107109
}
110+
111+
public Map<String, String> getMap(char arg) {
112+
return MapArgumentMarshaler.getValue(marshalers.get(arg));
113+
}
108114
}

src/com/cleancoder/args/ArgsException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public String errorMessage() {
7070
return String.format("'%c' is not a valid argument name.", errorArgumentId);
7171
case INVALID_ARGUMENT_FORMAT:
7272
return String.format("'%s' is not a valid argument format.", errorParameter);
73+
case MISSING_MAP:
74+
return String.format("Could not find map string for -%c.", errorArgumentId);
75+
case MALFORMED_MAP:
76+
return String.format("Map string for -%c is not of form k1:v1,k2:v2...", errorArgumentId);
7377
}
7478
return "";
7579
}
@@ -78,5 +82,5 @@ public enum ErrorCode {
7882
OK, INVALID_ARGUMENT_FORMAT, UNEXPECTED_ARGUMENT, INVALID_ARGUMENT_NAME,
7983
MISSING_STRING,
8084
MISSING_INTEGER, INVALID_INTEGER,
81-
MISSING_DOUBLE, INVALID_DOUBLE}
85+
MISSING_DOUBLE, MALFORMED_MAP, MISSING_MAP, INVALID_DOUBLE}
8286
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.cleancoder.args;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
import java.util.NoSuchElementException;
7+
8+
import static com.cleancoder.args.ArgsException.ErrorCode.*;
9+
10+
public class MapArgumentMarshaler implements ArgumentMarshaler {
11+
private Map<String, String> map = new HashMap<>();
12+
13+
public void set(Iterator<String> currentArgument) throws ArgsException {
14+
try {
15+
String[] mapEntries = currentArgument.next().split(",");
16+
for (String entry : mapEntries) {
17+
String[] entryComponents = entry.split(":");
18+
if (entryComponents.length != 2)
19+
throw new ArgsException(MALFORMED_MAP);
20+
map.put(entryComponents[0], entryComponents[1]);
21+
}
22+
} catch (NoSuchElementException e) {
23+
throw new ArgsException(MISSING_MAP);
24+
}
25+
}
26+
27+
public static Map<String, String> getValue(ArgumentMarshaler am) {
28+
if (am != null && am instanceof MapArgumentMarshaler)
29+
return ((MapArgumentMarshaler) am).map;
30+
else
31+
return new HashMap<>();
32+
}
33+
}

test/com/cleancoder/args/ArgsExceptionTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cleancoder.args;
22

3-
import static com.cleancoder.args.ArgsException.ErrorCode.*;
4-
53
import junit.framework.TestCase;
64

5+
import static com.cleancoder.args.ArgsException.ErrorCode.*;
6+
77
public class ArgsExceptionTest extends TestCase {
88
public void testUnexpectedMessage() throws Exception {
99
ArgsException e = new ArgsException(UNEXPECTED_ARGUMENT, 'x', null);
@@ -35,6 +35,16 @@ public void testMissingDoubleMessage() throws Exception {
3535
assertEquals("Could not find double parameter for -x.", e.errorMessage());
3636
}
3737

38+
public void testMissingMapMessage() throws Exception {
39+
ArgsException e = new ArgsException(MISSING_MAP, 'x', null);
40+
assertEquals("Could not find map string for -x.", e.errorMessage());
41+
}
42+
43+
public void testMalformedMapMessage() throws Exception {
44+
ArgsException e = new ArgsException(MALFORMED_MAP, 'x', null);
45+
assertEquals("Map string for -x is not of form k1:v1,k2:v2...", e.errorMessage());
46+
}
47+
3848
public void testInvalidArgumentName() throws Exception {
3949
ArgsException e = new ArgsException(INVALID_ARGUMENT_NAME, '#', null);
4050
assertEquals("'#' is not a valid argument name.", e.errorMessage());

test/com/cleancoder/args/ArgsTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.junit.Test;
44

5+
import java.util.Map;
6+
57
import static com.cleancoder.args.ArgsException.ErrorCode.*;
68
import static org.junit.Assert.*;
79

@@ -184,7 +186,28 @@ public void manyStringArrayElements() throws Exception {
184186
assertEquals("alpha", result[0]);
185187
assertEquals("beta", result[1]);
186188
assertEquals("gamma", result[2]);
189+
}
190+
191+
@Test
192+
public void MapArgument() throws Exception {
193+
Args args = new Args("f&", new String[] {"-f", "key1:val1,key2:val2"});
194+
assertTrue(args.has('f'));
195+
Map<String, String> map = args.getMap('f');
196+
assertEquals("val1", map.get("key1"));
197+
assertEquals("val2", map.get("key2"));
198+
}
199+
200+
@Test(expected=ArgsException.class)
201+
public void malFormedMapArgument() throws Exception {
202+
Args args = new Args("f&", new String[] {"-f", "key1:val1,key2"});
203+
}
187204

205+
@Test
206+
public void oneMapArgument() throws Exception {
207+
Args args = new Args("f&", new String[] {"-f", "key1:val1"});
208+
assertTrue(args.has('f'));
209+
Map<String, String> map = args.getMap('f');
210+
assertEquals("val1", map.get("key1"));
188211
}
189212

190213
@Test

0 commit comments

Comments
 (0)