This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
String operations are used for the construction of file paths. Also, many APIs in the codebase take in String arguments as file paths. This method of file/path handling is error prone as creating paths using string concatenation can lead to the creation of invalid paths. Furthermore, checking if two paths resolve to the same file using string equality is incorrect as it only tests if the strings are equal. Let's refactor the codebase to use java.nio.file for file/path handling as it provides a safe and platform-independent way to perform operations on paths and properly separate the concerns of parsing/stringifying paths and manipulating paths. More information on why String operations are error-prone: http://twistedoakstudios.com/blog/Post4872_dont-treat-paths-like-strings
- Loading branch information
Showing
34 changed files
with
340 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package seedu.address; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.logging.Logger; | ||
|
||
import javafx.application.Application; | ||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.util.FileUtil; | ||
|
||
/** | ||
* Represents the parsed command-line parameters given to the application. | ||
*/ | ||
public class AppParameters { | ||
private static final Logger logger = LogsCenter.getLogger(AppParameters.class); | ||
|
||
private Path configPath; | ||
|
||
public Path getConfigPath() { | ||
return configPath; | ||
} | ||
|
||
public void setConfigPath(Path configPath) { | ||
this.configPath = configPath; | ||
} | ||
|
||
/** | ||
* Parses the application command-line parameters. | ||
*/ | ||
public static AppParameters parse(Application.Parameters parameters) { | ||
AppParameters appParameters = new AppParameters(); | ||
Map<String, String> namedParameters = parameters.getNamed(); | ||
|
||
String configPathParameter = namedParameters.get("config"); | ||
if (configPathParameter != null && !FileUtil.isValidPath(configPathParameter)) { | ||
logger.warning("Invalid config path " + configPathParameter + ". Using default config path."); | ||
configPathParameter = null; | ||
} | ||
appParameters.setConfigPath(configPathParameter != null ? Paths.get(configPathParameter) : null); | ||
|
||
return appParameters; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof AppParameters)) { | ||
return false; | ||
} | ||
|
||
AppParameters otherAppParameters = (AppParameters) other; | ||
return Objects.equals(getConfigPath(), otherAppParameters.getConfigPath()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return configPath.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.