Skip to content

Commit cf62897

Browse files
Sarthak | Refactors Arguments, it does not inherit from ArrayList<String>
1 parent eb31ffe commit cf62897

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package com.codurance.training.commands;
22

3-
import java.util.ArrayList;
43
import java.util.Arrays;
54
import java.util.List;
65
import java.util.stream.Collectors;
76

8-
/**
9-
* The class `Arguments` is showing signs of both mutability and immutability.
10-
* Signs of mutability:
11-
* - Arguments extends from ArrayList<String> which is mutable.
12-
* - The method `skipOne` creates a new instance of Arguments, which means the author does not want to change the existing collection.
13-
* An instance of Arguments is passed to all the Commands. Given, Arguments extends from ArrayList<String>, it gives all the commands
14-
* the authority to mutate arguments.
15-
* Technically, Arguments should never be mutated once created.
16-
*/
17-
public class Arguments extends ArrayList<String> {
7+
public class Arguments {
8+
9+
private final List<String> arguments;
1810

1911
static Arguments skipOneAndCreate(String[] parts) {
2012
if (parts == null) {
@@ -24,26 +16,26 @@ static Arguments skipOneAndCreate(String[] parts) {
2416
}
2517

2618
Arguments(List<String> arguments) {
27-
this.addAll(arguments);
19+
this.arguments = arguments;
2820
}
2921

3022
Arguments skipOne() {
31-
return new Arguments(this.subList(1, this.size()));
23+
return new Arguments(this.arguments.subList(1, this.count()));
3224
}
3325

3426
int argumentAtIndexAsInt(int index) {
35-
return Integer.parseInt(this.get(index));
27+
return Integer.parseInt(this.arguments.get(index));
3628
}
3729

3830
String argumentAtIndexAsString(int index) {
39-
return get(index);
31+
return this.arguments.get(index);
4032
}
4133

4234
int count() {
43-
return size();
35+
return this.arguments.size();
4436
}
4537

46-
boolean hasAny() {
47-
return size() == 0;
38+
boolean isEmpty() {
39+
return this.arguments.isEmpty();
4840
}
4941
}

src/main/java/com/codurance/training/commands/ShowCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ShowCommand implements Command {
1515

1616
@Override
1717
public void execute(Arguments arguments) throws Exception {
18-
assert (arguments.hasAny());
18+
assert (arguments.isEmpty());
1919
this.writer.write(this.projects.format());
2020
}
2121
}

src/test/java/com/codurance/training/commands/ArgumentsTest.java

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

55
import java.util.List;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.*;
88

99
public class ArgumentsTest {
1010

1111
@Test
1212
public void skipOneAndCreateArguments() {
1313
Arguments arguments = Arguments.skipOneAndCreate(new String[]{"an", "argument", "1"});
14-
assertEquals(List.of("argument", "1"), arguments);
14+
assertEquals("argument", arguments.argumentAtIndexAsString(0));
15+
assertEquals("1", arguments.argumentAtIndexAsString(1));
1516
}
1617

1718
@Test
1819
public void skipOneAndCreateEmptyArguments() {
1920
Arguments arguments = Arguments.skipOneAndCreate(new String[]{});
20-
assertEquals(List.of(), arguments);
21+
assertTrue(arguments.isEmpty());
2122
}
2223

2324
@Test
2425
public void skipOneAndCreateEmptyArgumentsGivenNull() {
2526
Arguments arguments = Arguments.skipOneAndCreate(null);
26-
assertEquals(List.of(), arguments);
27+
assertTrue(arguments.isEmpty());
2728
}
2829

2930
@Test
3031
public void skipOneFromArguments() {
3132
Arguments arguments = new Arguments(List.of("an", "argument", "1"));
32-
assertEquals(List.of("argument", "1"), arguments.skipOne());
33+
Arguments skipped = arguments.skipOne();
34+
assertEquals("argument", skipped.argumentAtIndexAsString(0));
35+
assertEquals("1", skipped.argumentAtIndexAsString(1));
3336
}
3437

3538
@Test
@@ -43,4 +46,40 @@ public void argumentAtIndex1AsInt() {
4346
Arguments arguments = new Arguments(List.of("0", "200", "1"));
4447
assertEquals(200, arguments.argumentAtIndexAsInt(1));
4548
}
49+
50+
@Test
51+
public void argumentAtIndex0AsString() {
52+
Arguments arguments = new Arguments(List.of("1", "argument", "1"));
53+
assertEquals("1", arguments.argumentAtIndexAsString(0));
54+
}
55+
56+
@Test
57+
public void argumentAtIndex1AsString() {
58+
Arguments arguments = new Arguments(List.of("0", "200", "1"));
59+
assertEquals("200", arguments.argumentAtIndexAsString(1));
60+
}
61+
62+
@Test
63+
public void zeroArguments() {
64+
Arguments arguments = new Arguments(List.of());
65+
assertEquals(0, arguments.count());
66+
}
67+
68+
@Test
69+
public void threeArguments() {
70+
Arguments arguments = new Arguments(List.of("0", "200", "1"));
71+
assertEquals(3, arguments.count());
72+
}
73+
74+
@Test
75+
public void argumentsIsEmpty() {
76+
Arguments arguments = Arguments.skipOneAndCreate(null);
77+
assertTrue(arguments.isEmpty());
78+
}
79+
80+
@Test
81+
public void argumentsIsNotEmpty() {
82+
Arguments arguments = new Arguments(List.of("argument"));
83+
assertFalse(arguments.isEmpty());
84+
}
4685
}

src/test/java/com/codurance/training/commands/CommandLineTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void parseCheckCommand() {
2222
CommandDescription commandDescription = commandLine.parse();
2323

2424
assertEquals(CommandType.CHECK, commandDescription.commandType());
25-
assertEquals(List.of("100"), commandDescription.arguments());
25+
assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0));
2626
}
2727

2828
@Test
@@ -31,7 +31,7 @@ public void parseUncheckCommand() {
3131
CommandDescription commandDescription = commandLine.parse();
3232

3333
assertEquals(CommandType.UNCHECK, commandDescription.commandType());
34-
assertEquals(List.of("100"), commandDescription.arguments());
34+
assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0));
3535
}
3636

3737
@Test
@@ -40,7 +40,8 @@ public void parseAddProjectCommand() {
4040
CommandDescription commandDescription = commandLine.parse();
4141

4242
assertEquals(CommandType.ADD, commandDescription.commandType());
43-
assertEquals(List.of("project", "caizin"), commandDescription.arguments());
43+
assertEquals("project", commandDescription.arguments().argumentAtIndexAsString(0));
44+
assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1));
4445
}
4546

4647
@Test
@@ -49,6 +50,8 @@ public void parseAddTaskCommand() {
4950
CommandDescription commandDescription = commandLine.parse();
5051

5152
assertEquals(CommandType.ADD, commandDescription.commandType());
52-
assertEquals(List.of("task", "caizin", "200"), commandDescription.arguments());
53+
assertEquals("task", commandDescription.arguments().argumentAtIndexAsString(0));
54+
assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1));
55+
assertEquals("200", commandDescription.arguments().argumentAtIndexAsString(2));
5356
}
5457
}

0 commit comments

Comments
 (0)