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/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..099e91cf5 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 */ @@ -64,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'"); } @@ -141,8 +125,4 @@ public AddressBook load() throws StorageOperationException { } } - public String getPath() { - return path.toString(); - } - } 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); 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 {