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
Reduce duplication between ConfigUtil and JsonUserPrefsStorage #95 #147
Merged
damithc
merged 1 commit into
master
from
95-Reduce-duplication-between-ConfigUtil-and-JsonUserPrefsStorage
Oct 7, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,62 +1,23 @@ | ||
package seedu.address.commons.util; | ||
|
||
import seedu.address.commons.core.Config; | ||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.exceptions.DataConversionException; | ||
import seedu.address.commons.util.JsonUtil; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* A class for accessing the Config File. | ||
*/ | ||
public class ConfigUtil { | ||
|
||
private static final Logger logger = LogsCenter.getLogger(ConfigUtil.class); | ||
|
||
/** | ||
* Returns the Config object from the given file or {@code Optional.empty()} object if the file is not found. | ||
* If any values are missing from the file, default values will be used, as long as the file is a valid json file. | ||
* @param configFilePath cannot be null. | ||
* @throws DataConversionException if the file format is not as expected. | ||
*/ | ||
public static Optional<Config> readConfig(String configFilePath) throws DataConversionException { | ||
|
||
assert configFilePath != null; | ||
|
||
File configFile = new File(configFilePath); | ||
|
||
if (!configFile.exists()) { | ||
logger.info("Config file " + configFile + " not found"); | ||
return Optional.empty(); | ||
} | ||
|
||
Config config; | ||
|
||
try { | ||
config = FileUtil.deserializeObjectFromJsonFile(configFile, Config.class); | ||
} catch (IOException e) { | ||
logger.warning("Error reading from config file " + configFile + ": " + e); | ||
throw new DataConversionException(e); | ||
} | ||
|
||
return Optional.of(config); | ||
return JsonUtil.readJsonFile(configFilePath, Config.class); | ||
} | ||
|
||
/** | ||
* Saves the Config object to the specified file. | ||
* Overwrites existing file if it exists, creates a new file if it doesn't. | ||
* @param config cannot be null | ||
* @param configFilePath cannot be null | ||
* @throws IOException if there was an error during writing to the file | ||
*/ | ||
public static void saveConfig(Config config, String configFilePath) throws IOException { | ||
assert config != null; | ||
assert configFilePath != null; | ||
|
||
FileUtil.serializeObjectToJsonFile(new File(configFilePath), config); | ||
JsonUtil.saveJsonFile(config, configFilePath); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
package seedu.address.storage; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.exceptions.DataConversionException; | ||
import seedu.address.commons.util.FileUtil; | ||
import seedu.address.commons.util.JsonUtil; | ||
import seedu.address.model.UserPrefs; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* A class to access UserPrefs stored in the hard disk as a json file | ||
*/ | ||
public class JsonUserPrefsStorage implements UserPrefsStorage{ | ||
|
||
private static final Logger logger = LogsCenter.getLogger(JsonUserPrefsStorage.class); | ||
public class JsonUserPrefsStorage implements UserPrefsStorage { | ||
|
||
private String filePath; | ||
|
||
|
@@ -28,46 +23,18 @@ public Optional<UserPrefs> readUserPrefs() throws DataConversionException, IOExc | |
return readUserPrefs(filePath); | ||
} | ||
|
||
@Override | ||
public void saveUserPrefs(UserPrefs userPrefs) throws IOException { | ||
saveUserPrefs(userPrefs, filePath); | ||
} | ||
|
||
/** | ||
* Similar to {@link #readUserPrefs()} | ||
* @param prefsFilePath location of the data. Cannot be null. | ||
* @throws DataConversionException if the file format is not as expected. | ||
*/ | ||
public Optional<UserPrefs> readUserPrefs(String prefsFilePath) throws DataConversionException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Header comment got deleted? |
||
assert prefsFilePath != null; | ||
|
||
File prefsFile = new File(prefsFilePath); | ||
|
||
if (!prefsFile.exists()) { | ||
logger.info("Prefs file " + prefsFile + " not found"); | ||
return Optional.empty(); | ||
} | ||
|
||
UserPrefs prefs; | ||
|
||
try { | ||
prefs = FileUtil.deserializeObjectFromJsonFile(prefsFile, UserPrefs.class); | ||
} catch (IOException e) { | ||
logger.warning("Error reading from prefs file " + prefsFile + ": " + e); | ||
throw new DataConversionException(e); | ||
} | ||
|
||
return Optional.of(prefs); | ||
return JsonUtil.readJsonFile(prefsFilePath, UserPrefs.class); | ||
} | ||
|
||
/** | ||
* Similar to {@link #saveUserPrefs(UserPrefs)} | ||
* @param prefsFilePath location of the data. Cannot be null. | ||
*/ | ||
public void saveUserPrefs(UserPrefs userPrefs, String prefsFilePath) throws IOException { | ||
assert userPrefs != null; | ||
assert prefsFilePath != null; | ||
|
||
FileUtil.serializeObjectToJsonFile(new File(prefsFilePath), userPrefs); | ||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this method doesn't have a header comment, then the parent method should have one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comments are in the interface |
||
public void saveUserPrefs(UserPrefs userPrefs) throws IOException { | ||
JsonUtil.saveJsonFile(userPrefs, filePath); | ||
} | ||
|
||
} |
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
File renamed without changes.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these two methods be private? or at least package-private?
If they are used only once each, you can inline them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't be private as
JsonUtilTest
uses them.