From cf628976a83874fe03ca78cc51ddd145660c109d Mon Sep 17 00:00:00 2001 From: Sarthak Makhija Date: Fri, 10 Jan 2025 13:39:36 +0530 Subject: [PATCH] Sarthak | Refactors Arguments, it does not inherit from ArrayList --- .../training/commands/Arguments.java | 28 ++++------- .../training/commands/ShowCommand.java | 2 +- .../training/commands/ArgumentsTest.java | 49 +++++++++++++++++-- .../training/commands/CommandLineTest.java | 11 +++-- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/codurance/training/commands/Arguments.java b/src/main/java/com/codurance/training/commands/Arguments.java index dc67398..867c917 100644 --- a/src/main/java/com/codurance/training/commands/Arguments.java +++ b/src/main/java/com/codurance/training/commands/Arguments.java @@ -1,20 +1,12 @@ package com.codurance.training.commands; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -/** - * The class `Arguments` is showing signs of both mutability and immutability. - * Signs of mutability: - * - Arguments extends from ArrayList which is mutable. - * - The method `skipOne` creates a new instance of Arguments, which means the author does not want to change the existing collection. - * An instance of Arguments is passed to all the Commands. Given, Arguments extends from ArrayList, it gives all the commands - * the authority to mutate arguments. - * Technically, Arguments should never be mutated once created. - */ -public class Arguments extends ArrayList { +public class Arguments { + + private final List arguments; static Arguments skipOneAndCreate(String[] parts) { if (parts == null) { @@ -24,26 +16,26 @@ static Arguments skipOneAndCreate(String[] parts) { } Arguments(List arguments) { - this.addAll(arguments); + this.arguments = arguments; } Arguments skipOne() { - return new Arguments(this.subList(1, this.size())); + return new Arguments(this.arguments.subList(1, this.count())); } int argumentAtIndexAsInt(int index) { - return Integer.parseInt(this.get(index)); + return Integer.parseInt(this.arguments.get(index)); } String argumentAtIndexAsString(int index) { - return get(index); + return this.arguments.get(index); } int count() { - return size(); + return this.arguments.size(); } - boolean hasAny() { - return size() == 0; + boolean isEmpty() { + return this.arguments.isEmpty(); } } diff --git a/src/main/java/com/codurance/training/commands/ShowCommand.java b/src/main/java/com/codurance/training/commands/ShowCommand.java index e902d86..ed156c4 100644 --- a/src/main/java/com/codurance/training/commands/ShowCommand.java +++ b/src/main/java/com/codurance/training/commands/ShowCommand.java @@ -15,7 +15,7 @@ class ShowCommand implements Command { @Override public void execute(Arguments arguments) throws Exception { - assert (arguments.hasAny()); + assert (arguments.isEmpty()); this.writer.write(this.projects.format()); } } diff --git a/src/test/java/com/codurance/training/commands/ArgumentsTest.java b/src/test/java/com/codurance/training/commands/ArgumentsTest.java index efdb38e..6d7475d 100644 --- a/src/test/java/com/codurance/training/commands/ArgumentsTest.java +++ b/src/test/java/com/codurance/training/commands/ArgumentsTest.java @@ -4,32 +4,35 @@ import java.util.List; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; public class ArgumentsTest { @Test public void skipOneAndCreateArguments() { Arguments arguments = Arguments.skipOneAndCreate(new String[]{"an", "argument", "1"}); - assertEquals(List.of("argument", "1"), arguments); + assertEquals("argument", arguments.argumentAtIndexAsString(0)); + assertEquals("1", arguments.argumentAtIndexAsString(1)); } @Test public void skipOneAndCreateEmptyArguments() { Arguments arguments = Arguments.skipOneAndCreate(new String[]{}); - assertEquals(List.of(), arguments); + assertTrue(arguments.isEmpty()); } @Test public void skipOneAndCreateEmptyArgumentsGivenNull() { Arguments arguments = Arguments.skipOneAndCreate(null); - assertEquals(List.of(), arguments); + assertTrue(arguments.isEmpty()); } @Test public void skipOneFromArguments() { Arguments arguments = new Arguments(List.of("an", "argument", "1")); - assertEquals(List.of("argument", "1"), arguments.skipOne()); + Arguments skipped = arguments.skipOne(); + assertEquals("argument", skipped.argumentAtIndexAsString(0)); + assertEquals("1", skipped.argumentAtIndexAsString(1)); } @Test @@ -43,4 +46,40 @@ public void argumentAtIndex1AsInt() { Arguments arguments = new Arguments(List.of("0", "200", "1")); assertEquals(200, arguments.argumentAtIndexAsInt(1)); } + + @Test + public void argumentAtIndex0AsString() { + Arguments arguments = new Arguments(List.of("1", "argument", "1")); + assertEquals("1", arguments.argumentAtIndexAsString(0)); + } + + @Test + public void argumentAtIndex1AsString() { + Arguments arguments = new Arguments(List.of("0", "200", "1")); + assertEquals("200", arguments.argumentAtIndexAsString(1)); + } + + @Test + public void zeroArguments() { + Arguments arguments = new Arguments(List.of()); + assertEquals(0, arguments.count()); + } + + @Test + public void threeArguments() { + Arguments arguments = new Arguments(List.of("0", "200", "1")); + assertEquals(3, arguments.count()); + } + + @Test + public void argumentsIsEmpty() { + Arguments arguments = Arguments.skipOneAndCreate(null); + assertTrue(arguments.isEmpty()); + } + + @Test + public void argumentsIsNotEmpty() { + Arguments arguments = new Arguments(List.of("argument")); + assertFalse(arguments.isEmpty()); + } } \ No newline at end of file diff --git a/src/test/java/com/codurance/training/commands/CommandLineTest.java b/src/test/java/com/codurance/training/commands/CommandLineTest.java index 9c893bb..e953047 100644 --- a/src/test/java/com/codurance/training/commands/CommandLineTest.java +++ b/src/test/java/com/codurance/training/commands/CommandLineTest.java @@ -22,7 +22,7 @@ public void parseCheckCommand() { CommandDescription commandDescription = commandLine.parse(); assertEquals(CommandType.CHECK, commandDescription.commandType()); - assertEquals(List.of("100"), commandDescription.arguments()); + assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0)); } @Test @@ -31,7 +31,7 @@ public void parseUncheckCommand() { CommandDescription commandDescription = commandLine.parse(); assertEquals(CommandType.UNCHECK, commandDescription.commandType()); - assertEquals(List.of("100"), commandDescription.arguments()); + assertEquals("100", commandDescription.arguments().argumentAtIndexAsString(0)); } @Test @@ -40,7 +40,8 @@ public void parseAddProjectCommand() { CommandDescription commandDescription = commandLine.parse(); assertEquals(CommandType.ADD, commandDescription.commandType()); - assertEquals(List.of("project", "caizin"), commandDescription.arguments()); + assertEquals("project", commandDescription.arguments().argumentAtIndexAsString(0)); + assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1)); } @Test @@ -49,6 +50,8 @@ public void parseAddTaskCommand() { CommandDescription commandDescription = commandLine.parse(); assertEquals(CommandType.ADD, commandDescription.commandType()); - assertEquals(List.of("task", "caizin", "200"), commandDescription.arguments()); + assertEquals("task", commandDescription.arguments().argumentAtIndexAsString(0)); + assertEquals("caizin", commandDescription.arguments().argumentAtIndexAsString(1)); + assertEquals("200", commandDescription.arguments().argumentAtIndexAsString(2)); } } \ No newline at end of file