From 318747191fbdc5d6bb4efc24f6edbceaa84b652d Mon Sep 17 00:00:00 2001 From: leon mak Date: Fri, 3 Mar 2017 00:47:42 +0800 Subject: [PATCH 1/4] Invert StorageFile dependency in Logic --- src/seedu/addressbook/storage/Storage.java | 42 +++++++++++++++++++ .../addressbook/storage/StorageFile.java | 24 +---------- 2 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/seedu/addressbook/storage/Storage.java diff --git a/src/seedu/addressbook/storage/Storage.java b/src/seedu/addressbook/storage/Storage.java new file mode 100644 index 000000000..74c851299 --- /dev/null +++ b/src/seedu/addressbook/storage/Storage.java @@ -0,0 +1,42 @@ +package seedu.addressbook.storage; + +import java.nio.file.Path; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.storage.Storage.StorageOperationException; + +public abstract class Storage { + + protected Path path; + + public Storage() { + } + + /* Note: Note the use of nested classes below. + * More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html + */ + + /** + * Signals that some error has occured while trying to convert and read/write data between the application + * and the storage file. + */ + public static class StorageOperationException extends Exception { + public StorageOperationException(String message) { + super(message); + } + } + + public abstract void save(AddressBook addressBook) throws StorageOperationException; + + public abstract AddressBook load() throws StorageOperationException; + + public String getPath() { + return path.toString(); + } + + public void setPath(Path path) { + this.path = path; + } + +} diff --git a/src/seedu/addressbook/storage/StorageFile.java b/src/seedu/addressbook/storage/StorageFile.java index 693097a86..d97e82684 100644 --- a/src/seedu/addressbook/storage/StorageFile.java +++ b/src/seedu/addressbook/storage/StorageFile.java @@ -15,14 +15,12 @@ /** * Represents the file used to store address book data. */ -public class StorageFile { +public class StorageFile extends Storage { /** Default file path used if the user doesn't provide the file name. */ public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt"; - /* Note: Note the use of nested classes below. - * More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html - */ + private final JAXBContext jaxbContext; /** * Signals that the given file path does not fulfill the storage filepath constraints. @@ -33,20 +31,6 @@ public InvalidStorageFilePathException(String message) { } } - /** - * Signals that some error has occured while trying to convert and read/write data between the application - * and the storage file. - */ - public static class StorageOperationException extends Exception { - public StorageOperationException(String message) { - super(message); - } - } - - private final JAXBContext jaxbContext; - - public final Path path; - /** * @throws InvalidStorageFilePathException if the default path is invalid */ @@ -141,8 +125,4 @@ public AddressBook load() throws StorageOperationException { } } - public String getPath() { - return path.toString(); - } - } From 0cf25695b90b32fb3eb2dbdf426a78b768bfeaf3 Mon Sep 17 00:00:00 2001 From: leon mak Date: Fri, 3 Mar 2017 00:53:10 +0800 Subject: [PATCH 2/4] Change storage import in test --- test/java/seedu/addressbook/storage/StorageFileTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/java/seedu/addressbook/storage/StorageFileTest.java b/test/java/seedu/addressbook/storage/StorageFileTest.java index 1bd6fcab3..a321670c5 100644 --- a/test/java/seedu/addressbook/storage/StorageFileTest.java +++ b/test/java/seedu/addressbook/storage/StorageFileTest.java @@ -17,7 +17,7 @@ import seedu.addressbook.data.person.Phone; import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; -import seedu.addressbook.storage.StorageFile.StorageOperationException; +import seedu.addressbook.storage.Storage.StorageOperationException; import static seedu.addressbook.util.TestUtil.assertTextFilesEqual; public class StorageFileTest { From cb5e00c69e8f5a7f4b138ed32889dc2c961e7039 Mon Sep 17 00:00:00 2001 From: leon mak Date: Fri, 3 Mar 2017 01:00:51 +0800 Subject: [PATCH 3/4] use setPath --- src/seedu/addressbook/storage/StorageFile.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/seedu/addressbook/storage/StorageFile.java b/src/seedu/addressbook/storage/StorageFile.java index d97e82684..099e91cf5 100644 --- a/src/seedu/addressbook/storage/StorageFile.java +++ b/src/seedu/addressbook/storage/StorageFile.java @@ -48,7 +48,7 @@ public StorageFile(String filePath) throws InvalidStorageFilePathException { throw new RuntimeException("jaxb initialisation error"); } - path = Paths.get(filePath); + setPath(Paths.get(filePath)); if (!isValidPath(path)) { throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); } From b46fd19bf679d7e252d74a68da2e4b12190637ec Mon Sep 17 00:00:00 2001 From: leon mak Date: Fri, 3 Mar 2017 01:22:50 +0800 Subject: [PATCH 4/4] Add StorageStub --- src/seedu/addressbook/logic/Logic.java | 10 +++---- .../addressbook/storage/StorageStub.java | 30 +++++++++++++++++++ .../seedu/addressbook/logic/LogicTest.java | 6 ++-- 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/seedu/addressbook/storage/StorageStub.java diff --git a/src/seedu/addressbook/logic/Logic.java b/src/seedu/addressbook/logic/Logic.java index 17afd61a0..fa989e8d3 100644 --- a/src/seedu/addressbook/logic/Logic.java +++ b/src/seedu/addressbook/logic/Logic.java @@ -5,6 +5,7 @@ import seedu.addressbook.data.AddressBook; import seedu.addressbook.data.person.ReadOnlyPerson; import seedu.addressbook.parser.Parser; +import seedu.addressbook.storage.Storage; import seedu.addressbook.storage.StorageFile; import java.util.Collections; @@ -16,8 +17,7 @@ */ public class Logic { - - private StorageFile storage; + private Storage storage; private AddressBook addressBook; /** The list of person shown to the user most recently. */ @@ -28,12 +28,12 @@ public Logic() throws Exception{ setAddressBook(storage.load()); } - Logic(StorageFile storageFile, AddressBook addressBook){ - setStorage(storageFile); + Logic(Storage storage, AddressBook addressBook){ + setStorage(storage); setAddressBook(addressBook); } - void setStorage(StorageFile storage){ + void setStorage(Storage storage){ this.storage = storage; } diff --git a/src/seedu/addressbook/storage/StorageStub.java b/src/seedu/addressbook/storage/StorageStub.java new file mode 100644 index 000000000..c34922f64 --- /dev/null +++ b/src/seedu/addressbook/storage/StorageStub.java @@ -0,0 +1,30 @@ +package seedu.addressbook.storage; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import seedu.addressbook.data.AddressBook; + +public class StorageStub extends Storage { + + AddressBook addressBook; + + public StorageStub(String pathString) { + setPath(Paths.get(pathString)); + } + + public StorageStub(Path path) { + setPath(path); + } + + @Override + public void save(AddressBook addressBook) throws StorageOperationException { + this.addressBook = addressBook; + } + + @Override + public AddressBook load() throws StorageOperationException { + return addressBook; + } + +} diff --git a/test/java/seedu/addressbook/logic/LogicTest.java b/test/java/seedu/addressbook/logic/LogicTest.java index 7efa921ca..9127251f3 100644 --- a/test/java/seedu/addressbook/logic/LogicTest.java +++ b/test/java/seedu/addressbook/logic/LogicTest.java @@ -12,7 +12,7 @@ import seedu.addressbook.data.person.*; import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; -import seedu.addressbook.storage.StorageFile; +import seedu.addressbook.storage.StorageStub; import java.util.*; @@ -28,13 +28,13 @@ public class LogicTest { @Rule public TemporaryFolder saveFolder = new TemporaryFolder(); - private StorageFile saveFile; + private StorageStub saveFile; private AddressBook addressBook; private Logic logic; @Before public void setup() throws Exception { - saveFile = new StorageFile(saveFolder.newFile("testSaveFile.txt").getPath()); + saveFile = new StorageStub(saveFolder.newFile("testSaveFile.txt").getPath()); addressBook = new AddressBook(); saveFile.save(addressBook); logic = new Logic(saveFile, addressBook);