diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 379a6f0..5b69f39 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,272 +4,25 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -388,7 +141,7 @@
-
+
@@ -398,28 +151,30 @@
- {
- "keyToString": {
- "ASKED_ADD_EXTERNAL_FILES": "true",
- "RunOnceActivity.OpenProjectViewOnStart": "true",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "SHARE_PROJECT_CONFIGURATION_FILES": "true",
- "WebServerToolWindowFactoryState": "false",
- "extract.method.default.visibility": "private",
- "jdk.selected.JAVA_MODULE": "openjdk-17.0.2",
- "last_opened_file_path": "/Users/zerumi/IdeaProjects/gitCodes/no6_150323_3846274761835/client/src/responseLogic",
- "node.js.detected.package.eslint": "true",
- "node.js.detected.package.tslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "node.js.selected.package.tslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "project.structure.last.edited": "Modules",
- "project.structure.proportion": "0.15",
- "project.structure.side.proportion": "0.2",
- "settings.editor.selected.configurable": "preferences.lookFeel",
- "vue.rearranger.settings.migration": "true"
+
+}]]>
@@ -443,6 +198,9 @@
+
+
+
@@ -833,8 +605,6 @@
-
-
@@ -858,7 +628,9 @@
-
+
+
+
@@ -883,14 +655,9 @@
file://$PROJECT_DIR$/server/src/commandManager/CommandManager.java
- 73
+ 76
-
- file://$PROJECT_DIR$/server/src/commandManager/commands/HelpCommand.java
- 43
-
-
file://$PROJECT_DIR$/client/src/serverLogic/UdpServerConnection.java
62
diff --git a/client/src/commandManager/CommandManager.java b/client/src/commandManager/CommandManager.java
index f7e11f7..3512273 100644
--- a/client/src/commandManager/CommandManager.java
+++ b/client/src/commandManager/CommandManager.java
@@ -14,6 +14,7 @@
import exceptions.CommandInterruptedException;
import exceptions.UnknownCommandException;
import exceptions.WrongAmountOfArgumentsException;
+import main.Utilities;
import models.Route;
import models.handlers.ModuleHandler;
import models.handlers.nonUserMode.RouteNonCLIHandler;
@@ -85,7 +86,10 @@ public LinkedHashMap getCommands() {
*/
public void executeCommand(String[] args) {
try {
- Optional.ofNullable(commands.get(args[0])).orElseThrow(() -> new UnknownCommandException("Указанная команда не была обнаружена")).execute(args);
+ BaseCommand command = Optional.ofNullable(commands.get(args[0])).orElseThrow(() -> new UnknownCommandException("Указанная команда не была обнаружена"));
+ if (command.getArgCount() > 0)
+ Utilities.checkArgumentsOrThrow(args.length, command.getArgCount());
+ command.execute(args);
} catch (IllegalArgumentException | NullPointerException e) {
myLogger.log(Level.SEVERE, "Выполнение команды пропущено из-за неправильных предоставленных аргументов! (" + e.getMessage() + ")");
throw new CommandInterruptedException(e);
diff --git a/client/src/commandManager/commands/ExecuteScriptCommand.java b/client/src/commandManager/commands/ExecuteScriptCommand.java
index 7c346e5..3af3703 100644
--- a/client/src/commandManager/commands/ExecuteScriptCommand.java
+++ b/client/src/commandManager/commands/ExecuteScriptCommand.java
@@ -63,12 +63,16 @@ private boolean checkRecursion(Path path, ArrayDeque stack) throws IOExcep
String str = Files.readString(path);
Pattern pattern = Pattern.compile("execute_script .*");
var patternMatcher = pattern.matcher(str);
- while (patternMatcher.find())
- {
+ while (patternMatcher.find()) {
Path newPath = Path.of(patternMatcher.group().split(" ")[1]);
- if(checkRecursion(newPath, stack)) return true;
+ if (checkRecursion(newPath, stack)) return true;
}
stack.removeLast();
return false;
}
+
+ @Override
+ public int getArgCount() {
+ return 1;
+ }
}
diff --git a/client/src/main/Utilities.java b/client/src/main/Utilities.java
index a8f003e..101521f 100644
--- a/client/src/main/Utilities.java
+++ b/client/src/main/Utilities.java
@@ -6,28 +6,6 @@
import java.util.Scanner;
public class Utilities {
- public static boolean isNotNumeric(String str) {
- return !str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
- }
-
- public static Long handleUserInputID(String input) {
- if (Utilities.isNotNumeric(input)) {
- System.out.println("Provided argument id: \"" + input + "\" is not a number! Try again.");
- return null;
- } else if (input.contains(".")) {
- System.out.println("ID field cannot accept decimal values. Try again.");
- return null;
- }
-
- Long id = null;
- try {
- id = Long.valueOf(input);
- } catch (NumberFormatException e) {
- System.out.println("Provided argument: \"" + input + "\" is too large for ID field. Try again.");
- }
- return id;
- }
-
public static void checkArgumentsOrThrow(int provided, int expected) throws WrongAmountOfArgumentsException {
if (provided - 1 != expected)
throw new WrongAmountOfArgumentsException("Provided " + (provided - 1) + " arguments, expected " + expected);
diff --git a/server/src/commandManager/CommandManager.java b/server/src/commandManager/CommandManager.java
index a1b5fba..15132fd 100644
--- a/server/src/commandManager/CommandManager.java
+++ b/server/src/commandManager/CommandManager.java
@@ -67,6 +67,9 @@ public void executeCommand(CommandClientRequest command) {
} catch (IllegalArgumentException | NullPointerException e) {
response = new CommandStatusResponse("Выполнение команды пропущено из-за неправильных предоставленных аргументов! (" + e.getMessage() + ")", -90);
logger.fatal(response.getResponse(), e);
+ } catch (IndexOutOfBoundsException e) {
+ response = new CommandStatusResponse("В команде предоставлено неправильное количество аргументов. Возможно, вам нужно обновить клиент", -91);
+ logger.fatal(response.getResponse(), e);
} catch (Exception e) {
response = new CommandStatusResponse("В командном менеджере произошла непредвиденная ошибка!", -92);
logger.fatal(response.getResponse(), e);
diff --git a/server/src/commandManager/CommandMode.java b/server/src/commandManager/CommandMode.java
deleted file mode 100644
index c896174..0000000
--- a/server/src/commandManager/CommandMode.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package commandManager;
-
-/**
- * Contains information about mode of commands behavior
- *
- * @author zerumi
- * @since 1.1
- */
-public enum CommandMode {
- /**
- * Commands will use server request interaction.
- */
- NETWORK_MODE,
-}
diff --git a/server/src/commandManager/commands/AddIfMaxCommand.java b/server/src/commandManager/commands/AddIfMaxCommand.java
index 927adeb..8c343d0 100644
--- a/server/src/commandManager/commands/AddIfMaxCommand.java
+++ b/server/src/commandManager/commands/AddIfMaxCommand.java
@@ -1,13 +1,11 @@
package commandManager.commands;
-import dataTransferObjects.models.RouteDTO;
import models.Route;
import models.comparators.RouteDistanceComparator;
import models.handlers.CollectionHandler;
import models.handlers.RoutesHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import requestLogic.dtoMappers.RouteDTOMapper;
import responseLogic.responses.CommandStatusResponse;
import java.util.HashSet;
@@ -39,14 +37,14 @@ public String getArgs() {
}
@Override
- public void execute(String[] args) throws ClassNotFoundException {
+ public void execute(String[] args) {
CollectionHandler, Route> collectionHandler = RoutesHandler.getInstance();
if (obj.compareTo(collectionHandler.getMax(new RouteDistanceComparator())) > 0) {
collectionHandler.addElementToCollection(obj);
response = CommandStatusResponse.ofString("Element added!");
} else {
- response = CommandStatusResponse.ofString("Element not added: it's not greater than max value.");
+ response = new CommandStatusResponse("Element not added: it's not greater than max value.", 3);
}
logger.info(response.getResponse());
diff --git a/server/src/commandManager/commands/AddIfMinCommand.java b/server/src/commandManager/commands/AddIfMinCommand.java
index 1480394..4f8ca6e 100644
--- a/server/src/commandManager/commands/AddIfMinCommand.java
+++ b/server/src/commandManager/commands/AddIfMinCommand.java
@@ -1,13 +1,11 @@
package commandManager.commands;
-import dataTransferObjects.models.RouteDTO;
import models.Route;
import models.comparators.RouteDistanceComparator;
import models.handlers.CollectionHandler;
import models.handlers.RoutesHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import requestLogic.dtoMappers.RouteDTOMapper;
import responseLogic.responses.CommandStatusResponse;
import java.util.HashSet;
@@ -39,14 +37,14 @@ public String getArgs() {
}
@Override
- public void execute(String[] args) throws ClassNotFoundException {
+ public void execute(String[] args) {
CollectionHandler, Route> collectionHandler = RoutesHandler.getInstance();
if (obj.compareTo(collectionHandler.getMin(new RouteDistanceComparator())) < 0) {
collectionHandler.addElementToCollection(obj);
response = CommandStatusResponse.ofString("Element added!");
} else {
- response = CommandStatusResponse.ofString("Element not added: it's not lower than min value.");
+ response = new CommandStatusResponse("Element not added: it's not lower than min value.", 3);
}
logger.info(response.getResponse());
diff --git a/server/src/commandManager/commands/ArgumentConsumer.java b/server/src/commandManager/commands/ArgumentConsumer.java
index 05f84a4..0c6047b 100644
--- a/server/src/commandManager/commands/ArgumentConsumer.java
+++ b/server/src/commandManager/commands/ArgumentConsumer.java
@@ -1,5 +1,12 @@
package commandManager.commands;
+/**
+ * Provides Argument Consumer
+ *
+ * @param Argument param
+ * @author zerumi
+ * @since 2.1
+ */
public interface ArgumentConsumer {
void setObj(T obj);
}
diff --git a/server/src/commandManager/commands/BaseCommand.java b/server/src/commandManager/commands/BaseCommand.java
index dbb0839..d04445e 100644
--- a/server/src/commandManager/commands/BaseCommand.java
+++ b/server/src/commandManager/commands/BaseCommand.java
@@ -2,8 +2,6 @@
import responseLogic.responses.CommandStatusResponse;
-import java.io.Serializable;
-
/**
* Base interface for command implementation. You should implement it before applying command in CommandManager
*
@@ -11,7 +9,7 @@
* @see commandManager.CommandManager
* @since 1.0
*/
-public interface BaseCommand extends Serializable {
+public interface BaseCommand {
/**
* Base method for show command name
*
@@ -41,7 +39,7 @@ default String getArgs() {
* @param args full array of entered line.
* @throws IllegalArgumentException when command can't understand given arguments
*/
- void execute(String[] args) throws IllegalArgumentException, ClassNotFoundException;
+ void execute(String[] args) throws IllegalArgumentException;
/**
* Base method for get command Output
diff --git a/server/src/commandManager/commands/RemoveGreaterCommand.java b/server/src/commandManager/commands/RemoveGreaterCommand.java
index 5669b24..3396734 100644
--- a/server/src/commandManager/commands/RemoveGreaterCommand.java
+++ b/server/src/commandManager/commands/RemoveGreaterCommand.java
@@ -1,13 +1,11 @@
package commandManager.commands;
-import dataTransferObjects.models.RouteDTO;
import models.Route;
import models.comparators.RouteDistanceComparator;
import models.handlers.CollectionHandler;
import models.handlers.RoutesHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import requestLogic.dtoMappers.RouteDTOMapper;
import responseLogic.responses.CommandStatusResponse;
import java.util.HashSet;
@@ -39,7 +37,7 @@ public String getArgs() {
}
@Override
- public void execute(String[] args) throws ClassNotFoundException {
+ public void execute(String[] args) {
RouteDistanceComparator comparator = new RouteDistanceComparator();
CollectionHandler, Route> collectionHandler = RoutesHandler.getInstance();
diff --git a/server/src/commandManager/commands/UpdateCommand.java b/server/src/commandManager/commands/UpdateCommand.java
index caa11d7..fa2d1e1 100644
--- a/server/src/commandManager/commands/UpdateCommand.java
+++ b/server/src/commandManager/commands/UpdateCommand.java
@@ -1,12 +1,10 @@
package commandManager.commands;
-import dataTransferObjects.models.RouteDTO;
import models.Route;
import models.handlers.CollectionHandler;
import models.handlers.RoutesHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
-import requestLogic.dtoMappers.RouteDTOMapper;
import responseLogic.responses.CommandStatusResponse;
import java.util.HashSet;
@@ -39,7 +37,7 @@ public String getArgs() {
}
@Override
- public void execute(String[] args) throws ClassNotFoundException {
+ public void execute(String[] args) {
CollectionHandler, Route> collectionHandler = RoutesHandler.getInstance();
Long finalId = Long.valueOf(args[1]);
diff --git a/shared/src/commandLogic/commands/BaseCommand.java b/shared/src/commandLogic/commands/BaseCommand.java
index 28234e8..bc1ce28 100644
--- a/shared/src/commandLogic/commands/BaseCommand.java
+++ b/shared/src/commandLogic/commands/BaseCommand.java
@@ -10,6 +10,10 @@ public interface BaseCommand {
String getName();
+ default int getArgCount() {
+ return 0;
+ }
+
/**
* Base method for command executing.
*
diff --git a/shared/src/commandLogic/commands/argCommands/UpdateCommand.java b/shared/src/commandLogic/commands/argCommands/UpdateCommand.java
index e0cdf81..cd22e5e 100644
--- a/shared/src/commandLogic/commands/argCommands/UpdateCommand.java
+++ b/shared/src/commandLogic/commands/argCommands/UpdateCommand.java
@@ -3,6 +3,7 @@
import commandLogic.commandReceiverLogic.ReceiverType;
import commandLogic.commands.BaseCommand;
+import main.LibUtilities;
/**
* Updates element by its ID.
@@ -20,4 +21,15 @@ public UpdateCommand(ReceiverType type) {
public String getName() {
return "update";
}
+
+ @Override
+ public int getArgCount() {
+ return 1;
+ }
+
+ @Override
+ public void execute(String[] args) throws Exception {
+ if (LibUtilities.handleUserInputID(args[1]) != null)
+ super.execute(args);
+ }
}
diff --git a/shared/src/commandLogic/commands/noArgCommands/CountGreaterThanDistanceCommand.java b/shared/src/commandLogic/commands/noArgCommands/CountGreaterThanDistanceCommand.java
index 730d284..80b153c 100644
--- a/shared/src/commandLogic/commands/noArgCommands/CountGreaterThanDistanceCommand.java
+++ b/shared/src/commandLogic/commands/noArgCommands/CountGreaterThanDistanceCommand.java
@@ -2,6 +2,7 @@
import commandLogic.commands.BaseCommand;
+import main.LibUtilities;
/**
* Shows count of the elements greater than distance value.
@@ -14,4 +15,23 @@ public class CountGreaterThanDistanceCommand extends NoArgumentCommand implement
public String getName() {
return "count_greater_than_distance";
}
+
+ @Override
+ public int getArgCount() {
+ return 1;
+ }
+
+ @Override
+ public void execute(String[] args) throws Exception {
+
+ if (LibUtilities.isNotNumeric(args[1])) {
+ System.out.println("Provided argument \"" + args[1] + "\" is not a number! Try again.");
+ return;
+ } else if (args[1].contains(",")) {
+ System.out.println("Distance field cannot accept decimal values. Try again");
+ return;
+ }
+
+ super.execute(args);
+ }
}
diff --git a/shared/src/commandLogic/commands/noArgCommands/RemoveByIdCommand.java b/shared/src/commandLogic/commands/noArgCommands/RemoveByIdCommand.java
index b1301d7..2a520e1 100644
--- a/shared/src/commandLogic/commands/noArgCommands/RemoveByIdCommand.java
+++ b/shared/src/commandLogic/commands/noArgCommands/RemoveByIdCommand.java
@@ -1,6 +1,7 @@
package commandLogic.commands.noArgCommands;
import commandLogic.commands.BaseCommand;
+import main.LibUtilities;
/**
* Removes element from collection by id.
@@ -15,7 +16,13 @@ public String getName() {
}
@Override
- public void execute(String[] args) {
- // TODO: validate id?
+ public int getArgCount() {
+ return 1;
+ }
+
+ @Override
+ public void execute(String[] args) throws Exception {
+ if (LibUtilities.handleUserInputID(args[1]) != null)
+ super.execute(args);
}
}
diff --git a/shared/src/main/LibUtilities.java b/shared/src/main/LibUtilities.java
new file mode 100644
index 0000000..a1ced32
--- /dev/null
+++ b/shared/src/main/LibUtilities.java
@@ -0,0 +1,25 @@
+package main;
+
+public class LibUtilities {
+ public static boolean isNotNumeric(String str) {
+ return !str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
+ }
+
+ public static Long handleUserInputID(String input) {
+ if (LibUtilities.isNotNumeric(input)) {
+ System.out.println("Provided argument id: \"" + input + "\" is not a number! Try again.");
+ return null;
+ } else if (input.contains(".")) {
+ System.out.println("ID field cannot accept decimal values. Try again.");
+ return null;
+ }
+
+ Long id = null;
+ try {
+ id = Long.valueOf(input);
+ } catch (NumberFormatException e) {
+ System.out.println("Provided argument: \"" + input + "\" is too large for ID field. Try again.");
+ }
+ return id;
+ }
+}
diff --git a/shared/src/main/Main.java b/shared/src/main/Main.java
index 06cc0f6..0c669a6 100644
--- a/shared/src/main/Main.java
+++ b/shared/src/main/Main.java
@@ -1,5 +1,10 @@
package main;
public class Main {
-
+ public static void main(String[] args) {
+ System.out.println("""
+ Однажды Вася спросил у своего старшего брата-сеньора Пети:
+ "Петя, зачем вообще нам нужно запускать общую библиотеку классов?"
+ На что Петя ответил: "Братиш, для того, чтобы практик на защите прочитал этот текст.\"""");
+ }
}