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.
Reduced duplication between ConfigUtil and JsonUserPrefsStorage
- Loading branch information
Showing
14 changed files
with
120 additions
and
131 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
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
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.