Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Reduce duplication between ConfigUtil and JsonUserPrefsStorage #95 #147

Conversation

lejolly
Copy link
Contributor

@lejolly lejolly commented Oct 6, 2016

Fixes #95

The tests are hard to merge even though they do roughly the same thing because each is specialised for that particular json file format (along with their respective classes, etc).

@lejolly lejolly added this to the v1.2 milestone Oct 6, 2016

import java.util.Objects;

/**
* Represents User's preferences.
*/
public class UserPrefs {
public class UserPrefs extends JsonFile {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserPrefs should not extend JsonFile because UserPrefs should not know anything about the storage format. The duplication occurs because both are stored in the same format, and hence should be unified at storage level.

Copy link
Contributor Author

@lejolly lejolly Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however the current implementation saves a UserPrefs object to json rather than a GuiSettings object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, removed JsonFile.


FileUtil.serializeObjectToJsonFile(new File(filePath), jsonFile);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two methods are json specific, so move to a json specific class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved all json specific methods to JsonUtil.

@damithc
Copy link
Contributor

damithc commented Oct 6, 2016

Some comments added.

@lejolly lejolly force-pushed the 95-Reduce-duplication-between-ConfigUtil-and-JsonUserPrefsStorage branch 3 times, most recently from 11e6837 to 088bb84 Compare October 6, 2016 09:29
Copy link
Contributor

@damithc damithc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonUtil is the right abstraction. Some cosmetic changes requested.

@@ -5,9 +5,11 @@
import java.nio.file.Files;

/**
* Writes and reads file
* Writes and reads files, includes methods for reading and writing to
* preference/config json files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A class should not mention the call sites/context. i.e. preference/config

public static <T> T deserializeObjectFromJsonFile(File jsonFile, Class<T> classOfObjectToDeserialize)
throws IOException {
return fromJsonString(FileUtil.readFromFile(jsonFile), classOfObjectToDeserialize);
}
Copy link
Contributor

@damithc damithc Oct 6, 2016

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.

Copy link
Contributor Author

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.

File file = new File(filePath);

if (!file.exists()) {
logger.info("Config file " + file + " not found");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not mention Config file

* 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header comment got deleted?

@@ -14,13 +14,12 @@
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.*;
import seedu.address.commons.util.ConfigUtil;
import seedu.address.storage.ConfigStorage;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reluctant to put this class in storage package because it's a more general utility the app should have even if there is no storage component.

/**
* A class for accessing the Config File.
*/
public class ConfigStorage {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind if you name this ConfigUtil and put inside util package. As mentioned above, config is a util class rather than a class in the storage component.

assert prefsFilePath != null;

FileUtil.serializeObjectToJsonFile(new File(prefsFilePath), userPrefs);
@Override
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comments are in the interface UserPrefsStorage for that method.

/**
* Tests JSON Read and Write
*/
public class JsonUtilTest {

private static final File SERIALIZATION_FILE = new File(TestUtil.getFilePathInSandboxFolder("serialize.json"));
Copy link
Contributor

@damithc damithc Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use JUnit Temp Folder @Rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think it works here, as the specified file actually exists.


thrown.expect(DataConversionException.class);
read("NotJasonFormatConfig.json");
read("NotJsonFormatConfig.json");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

@lejolly lejolly force-pushed the 95-Reduce-duplication-between-ConfigUtil-and-JsonUserPrefsStorage branch from 088bb84 to 0797764 Compare October 7, 2016 03:58
@lejolly lejolly force-pushed the 95-Reduce-duplication-between-ConfigUtil-and-JsonUserPrefsStorage branch from 0797764 to 79e960c Compare October 7, 2016 04:17
@damithc damithc merged commit ef2543e into master Oct 7, 2016
@damithc damithc deleted the 95-Reduce-duplication-between-ConfigUtil-and-JsonUserPrefsStorage branch October 7, 2016 09:34
auskure pushed a commit to auskure/main that referenced this pull request Nov 6, 2018
mechome referenced this pull request in mechome/main Apr 5, 2019
Fix bug that caused data files to not load when starting the Application
pyokagan pushed a commit to pyokagan/addressbook-level4 that referenced this pull request Nov 10, 2019
GUI improvements for mid-v1.3
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants