Skip to content

Commit

Permalink
Check for empty element provided
Browse files Browse the repository at this point in the history
  • Loading branch information
NereusWB922 committed Nov 13, 2023
1 parent 56b7dcb commit f90c686
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/java/seedu/address/model/path/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class Path {
public static final String MESSAGE_INVALID_PATH_ELEMENT = "Encountered invalid path element: %1$s";
public static final String MESSAGE_UNABLE_TO_NAVIGATE_ABOVE_ROOT = "Unable to navigate above root directory";
public static final String MESSAGE_INVALID_PATH_STRUCTURE = "Invalid path structure.";
public static final String MESSAGE_EMPTY_PATH_ELEMENT = "Path element cannot be empty.";
public static final String PATH_DELIMETER = "/";
protected final List<PathElement> pathElements;

Expand Down Expand Up @@ -58,6 +59,10 @@ protected void commonConstructor(String path) throws InvalidPathException {

String[] elementStrs = path.split("/");

if (elementStrs.length == 0) {
throw new InvalidPathException(MESSAGE_EMPTY_PATH_ELEMENT);
}

List<PathElement> elements = new ArrayList<>();

// Parses all string elements into PathElement object.
Expand Down Expand Up @@ -101,10 +106,6 @@ protected static void appendPathElements(List<PathElement> destination, List<Pat
break;
}
}
// For relative path is possible to have empty list in the end e.g. "."
if (destination.size() == 0) {
destination.add(PathElement.ELEMENT_CURRENT);
}
}

private static void handleParentElement(List<PathElement> destination, PathElement element)
Expand All @@ -129,7 +130,9 @@ private static void handleOtherElement(List<PathElement> destination, PathElemen
int priorityDiff = element.getPriorityDiff(prevElement);

// Make sure current element is 1 level lower than prev element or prev element is ".."
if (priorityDiff != -1 && prevElement.getType() != PathElementType.PARENT) {
if (priorityDiff != -1
&& prevElement.getType() != PathElementType.PARENT
&& prevElement.getType() != PathElementType.CURRENT) {
throw new InvalidPathException(MESSAGE_INVALID_PATH_STRUCTURE);
}

Expand Down

0 comments on commit f90c686

Please sign in to comment.