Skip to content

Commit

Permalink
Merge pull request #88 from 3m0W33D/branch-Refactor
Browse files Browse the repository at this point in the history
Branch refactor
  • Loading branch information
3m0W33D authored Oct 12, 2021
2 parents 6cce5a4 + a3c8e0d commit 18ab7d9
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 231 deletions.
2 changes: 1 addition & 1 deletion src/main/java/terminus/Terminus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import terminus.command.Command;
import terminus.command.CommandResult;
import terminus.common.TerminusLogger;
import terminus.exception.InvalidCommandException;
import terminus.exception.InvalidArgumentException;
import terminus.exception.InvalidCommandException;
import terminus.module.NusModule;
import terminus.parser.CommandParser;
import terminus.parser.MainCommandParser;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/terminus/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package terminus.command;

import terminus.exception.InvalidCommandException;
import terminus.exception.InvalidArgumentException;
import terminus.exception.InvalidCommandException;
import terminus.module.NusModule;
import terminus.ui.Ui;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/terminus/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package terminus.command;

import java.util.Locale;
import terminus.common.CommonFormat;
import terminus.common.CommonUtils;
import terminus.common.Messages;
import terminus.common.TerminusLogger;
import terminus.content.Content;
Expand All @@ -12,7 +12,7 @@

public class DeleteCommand<T extends Content> extends Command {

private Class<T> type;
private final Class<T> type;
private int itemNumber;

public DeleteCommand(Class<T> type) {
Expand Down Expand Up @@ -56,7 +56,7 @@ public CommandResult execute(Ui ui, NusModule module) throws InvalidArgumentExce
assert deletedContentName != null && !deletedContentName.isBlank();

ui.printSection(String.format(Messages.MESSAGE_RESPONSE_DELETE,
CommonFormat.getClassName(type).toLowerCase(), deletedContentName));
CommonUtils.getClassName(type).toLowerCase(), deletedContentName));
return new CommandResult(true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/terminus/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class HelpCommand extends Command {

public static final String HELP_MENU_MESSAGE = "\nHelp Menu\n---------";
private CommandParser commandMap;
private final CommandParser commandMap;

public HelpCommand(CommandParser commandMap) {
this.commandMap = commandMap;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/terminus/command/NotesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import terminus.common.CommonFormat;
import terminus.common.Messages;
import terminus.module.NusModule;
import terminus.parser.NoteCommandParser;

public class NotesCommand extends WorkspaceCommand {
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/terminus/command/TestCommand.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/terminus/command/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class ViewCommand<T extends Content> extends Command {

private Class<T> type;
private final Class<T> type;

private int itemNumber;
private boolean displayAll;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/terminus/command/note/AddNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import terminus.command.Command;
import terminus.command.CommandResult;
import terminus.common.CommonFormat;
import terminus.common.CommonUtils;
import terminus.common.Messages;
import terminus.common.TerminusLogger;
import terminus.content.ContentManager;
import terminus.content.Note;
import terminus.exception.InvalidArgumentException;
import terminus.exception.InvalidCommandException;
import terminus.module.NusModule;
import terminus.ui.Ui;
import terminus.common.TerminusLogger;

public class AddNoteCommand extends Command {

Expand Down Expand Up @@ -42,7 +43,7 @@ public void parseArguments(String arguments) throws InvalidArgumentException {
throw new InvalidArgumentException(this.getFormat(), Messages.ERROR_MESSAGE_MISSING_ARGUMENTS);
}
// Regex to find arguments
ArrayList<String> argArray = CommonFormat.findArguments(arguments);
ArrayList<String> argArray = CommonUtils.findArguments(arguments);
if (!isValidNoteArguments(argArray)) {
throw new InvalidArgumentException(this.getFormat(), Messages.ERROR_MESSAGE_MISSING_ARGUMENTS);
}
Expand All @@ -69,7 +70,7 @@ private boolean isValidNoteArguments(ArrayList<String> argArray) {
TerminusLogger.warning(String.format("Failed to find %d arguments: %d arguments found",
ADD_NOTE_ARGUMENTS, argArray.size()));
isValid = false;
} else if (CommonFormat.isArrayEmpty(argArray)) {
} else if (CommonUtils.isArrayEmpty(argArray)) {
TerminusLogger.warning("Failed to parse arguments: some arguments found is empty");
isValid = false;
}
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/terminus/command/zoomlink/AddLinkCommand.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package terminus.command.zoomlink;

import static terminus.common.CommonUtils.isValidDay;
import static terminus.common.CommonUtils.isValidUrl;

import java.time.LocalTime;
import java.util.ArrayList;
import terminus.command.Command;
import terminus.command.CommandResult;
import terminus.common.CommonFormat;
import terminus.common.CommonUtils;
import terminus.common.Messages;
import terminus.common.TerminusLogger;
import terminus.content.ContentManager;
import terminus.content.Link;
import terminus.exception.InvalidCommandException;
import terminus.exception.InvalidArgumentException;
import terminus.exception.InvalidCommandException;
import terminus.module.NusModule;
import terminus.common.CommonFormat;
import terminus.common.Messages;
import terminus.ui.Ui;

import java.time.LocalTime;
import java.util.ArrayList;
import static terminus.common.CommonFormat.isValidDay;
import static terminus.common.CommonFormat.isValidUrl;

/**
* AddLinkCommand class which will manage the adding of new Links from user command.
*/
Expand Down Expand Up @@ -66,15 +67,15 @@ public void parseArguments(String arguments) throws InvalidArgumentException {
if (arguments == null || arguments.isBlank()) {
throw new InvalidArgumentException(this.getFormat(), Messages.ERROR_MESSAGE_MISSING_ARGUMENTS);
}
ArrayList<String> argArray = CommonFormat.findArguments(arguments);
ArrayList<String> argArray = CommonUtils.findArguments(arguments);
if (!isValidScheduleArguments(argArray)) {
throw new InvalidArgumentException(this.getFormat(), Messages.ERROR_MESSAGE_MISSING_ARGUMENTS);
}
String userStartTime = argArray.get(2);

this.description = argArray.get(0);
this.day = argArray.get(1);
this.startTime = CommonFormat.convertToLocalTime(userStartTime);
this.startTime = CommonUtils.convertToLocalTime(userStartTime);
this.link = argArray.get(3);

if (!isValidDay(this.day)) {
Expand Down Expand Up @@ -122,7 +123,7 @@ private boolean isValidScheduleArguments(ArrayList<String> argArray) {
TerminusLogger.warning(String.format("Failed to find %d arguments, %d arguments found",
ADD_SCHEDULE_ARGUMENTS, argArray.size()));
isValid = false;
} else if (CommonFormat.isArrayEmpty(argArray)) {
} else if (CommonUtils.isArrayEmpty(argArray)) {
TerminusLogger.warning("Failed to parse arguments, some arguments found is empty");
isValid = false;
}
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/terminus/common/CommonFormat.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package terminus.common;


import java.time.format.DateTimeParseException;
import java.net.URL;
import terminus.exception.InvalidArgumentException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonFormat {

public static final String COMMAND_NOTE = "note";
Expand All @@ -28,88 +19,4 @@ public class CommonFormat {
public static final String COMMAND_ADD_SCHEDULE_FORMAT = COMMAND_ADD + " \"<link description>\" "
+ "\"<day>\" \"<start_time " + LOCAL_TIME_FORMAT + ">\" \"<zoom_link>\"";
public static final String COMMAND_ADD_NOTE_FORMAT = COMMAND_ADD + " \"<note name>\" \"<note content>\"";

/**
* Method to get arguments.
*
* @param arg String containing the arguments
* @return An array list containing the separated arguments
*/
public static ArrayList<String> findArguments(String arg) {
assert arg != null;
ArrayList<String> argsArray = new ArrayList<>();
Pattern p = Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(arg);
while (m.find()) {
argsArray.add(m.group(1));
}
return argsArray;
}

/**
* Checks if an array list is empty.
*
* @param argArray The array list to be checked
* @return True if array list is empty, false otherwise
*/
public static boolean isArrayEmpty(ArrayList<String> argArray) {
assert argArray != null;
if (argArray.isEmpty()) {
return true;
}
for (String s : argArray) {
if (s == null || s.isBlank()) {
return true;
}
}
return false;
}

/**
* Converts string to a LocalTime object.
*
* @param startTime The string to be converted to a LocalTime object
* @return A LocalTime object of the converted string
* @throws InvalidArgumentException Exception for when string does not follow the proper time format
*/
public static LocalTime convertToLocalTime(String startTime) throws InvalidArgumentException {
assert startTime != null;
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern(LOCAL_TIME_FORMAT);
return LocalTime.parse(startTime, format);
} catch (DateTimeParseException e) {
throw new InvalidArgumentException(
String.format(Messages.ERROR_MESSAGE_INVALID_TIME_FORMAT, LOCAL_TIME_FORMAT));
}

}

public static <T> String getClassName(T type) {
assert type != null;
String result = type.toString();
String[] string = result.split("\\.");
if (string.length > 0) {
result = string[string.length - 1];
}
return result;
}

public static boolean isValidUrl(String url) throws InvalidArgumentException {
try {
new URL(url).toURI();
return true;
} catch (Exception e) {
throw new InvalidArgumentException(
String.format(Messages.ERROR_MESSAGE_INVALID_LINK, url));
}
}

public static boolean isValidDay(String day) {
for (DaysOfWeekEnum dayOfWeek : DaysOfWeekEnum.values()) {
if (dayOfWeek.name().equalsIgnoreCase(day)) {
return true;
}
}
return false;
}
}
92 changes: 92 additions & 0 deletions src/main/java/terminus/common/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package terminus.common;

import java.net.URL;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import terminus.exception.InvalidArgumentException;

public class CommonUtils {

/**
* Method to get arguments.
*
* @param arg String containing the arguments
* @return An array list containing the separated arguments
*/
public static ArrayList<String> findArguments(String arg) {
assert arg != null;
ArrayList<String> argsArray = new ArrayList<>();
Pattern p = Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(arg);
while (m.find()) {
argsArray.add(m.group(1));
}
return argsArray;
}

/**
* Checks if an array list is empty.
*
* @param argArray The array list to be checked
* @return True if array list is empty, false otherwise
*/
public static boolean isArrayEmpty(ArrayList<String> argArray) {
assert argArray != null;
if (argArray.isEmpty()) {
return true;
}
return argArray.stream().anyMatch(x -> x == null || x.isBlank());
}

/**
* Converts string to a LocalTime object.
*
* @param startTime The string to be converted to a LocalTime object
* @return A LocalTime object of the converted string
* @throws InvalidArgumentException Exception for when string does not follow the proper time format
*/
public static LocalTime convertToLocalTime(String startTime) throws InvalidArgumentException {
assert startTime != null;
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern(CommonFormat.LOCAL_TIME_FORMAT);
return LocalTime.parse(startTime, format);
} catch (DateTimeParseException e) {
throw new InvalidArgumentException(
String.format(Messages.ERROR_MESSAGE_INVALID_TIME_FORMAT, CommonFormat.LOCAL_TIME_FORMAT));
}

}

public static <T> String getClassName(T type) {
assert type != null;
String result = type.toString();
String[] string = result.split("\\.");
if (string.length > 0) {
result = string[string.length - 1];
}
return result;
}

public static boolean isValidUrl(String url) throws InvalidArgumentException {
try {
new URL(url).toURI();
return true;
} catch (Exception e) {
throw new InvalidArgumentException(
String.format(Messages.ERROR_MESSAGE_INVALID_LINK, url));
}
}

public static boolean isValidDay(String day) {
for (DaysOfWeekEnum dayOfWeek : DaysOfWeekEnum.values()) {
if (dayOfWeek.name().equalsIgnoreCase(day)) {
return true;
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/terminus/common/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Messages {
public static final String MESSAGE_COMMAND_NOTE = "Move to notes workspace.";
public static final String MESSAGE_COMMAND_SCHEDULE = "Move to schedules workspace.";

public static final String MESSAGE_RESPONSE_DELETE = "Your %s on \'%s\' has been deleted!";
public static final String MESSAGE_RESPONSE_DELETE = "Your %s on '%s' has been deleted!";
public static final String MESSAGE_RESPONSE_ADD = "Your %s on '%s' has been added!";

public static final String ERROR_MESSAGE_TAG = "Error: ";
Expand Down
Loading

0 comments on commit 18ab7d9

Please sign in to comment.