From 01e93391a95bcd05ce86ae6eb3b12c99249b7c19 Mon Sep 17 00:00:00 2001 From: Penghao Chen Date: Wed, 22 Feb 2017 23:58:46 +0800 Subject: [PATCH 1/5] =?UTF-8?q?[T6A1]=20DIP:=20Set=20up=20abstract=20class?= =?UTF-8?q?=20=E2=80=9Cstorage=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/seedu/addressbook/storage/Storage.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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..81540a542 --- /dev/null +++ b/src/seedu/addressbook/storage/Storage.java @@ -0,0 +1,35 @@ +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 { + + /** + * Signals that the given file path does not fulfill the storage filepath constraints. + */ + public static class InvalidStoragePathException extends IllegalValueException { + public InvalidStoragePathException(String message) { + super(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); + } + } + + public abstract AddressBook load() throws StorageOperationException; + + public abstract void save(AddressBook addressBook) throws StorageOperationException; + + public abstract String getPath(); +} From d02def6ff7e95af9ef9c5c51ba17c490c7e07372 Mon Sep 17 00:00:00 2001 From: Penghao Chen Date: Wed, 22 Feb 2017 23:59:28 +0800 Subject: [PATCH 2/5] [T6A1] Change accordingly to make Logic class depend on the abstract Storage class --- src/seedu/addressbook/logic/Logic.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/seedu/addressbook/logic/Logic.java b/src/seedu/addressbook/logic/Logic.java index 17afd61a0..e1871f36d 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; @@ -17,7 +18,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 +29,12 @@ public Logic() throws Exception{ setAddressBook(storage.load()); } - Logic(StorageFile storageFile, AddressBook addressBook){ + Logic(Storage storageFile, AddressBook addressBook){ setStorage(storageFile); setAddressBook(addressBook); } - void setStorage(StorageFile storage){ + void setStorage(Storage storage){ this.storage = storage; } @@ -45,7 +46,7 @@ void setAddressBook(AddressBook addressBook){ * Creates the StorageFile object based on the user specified path (if any) or the default storage path. * @throws StorageFile.InvalidStorageFilePathException if the target file path is incorrect. */ - private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePathException { + private Storage initializeStorage() throws Storage.InvalidStoragePathException { return new StorageFile(); } From 160f35d18d18726ec92647aaf3fa2ccd7c9e635a Mon Sep 17 00:00:00 2001 From: Penghao Chen Date: Wed, 22 Feb 2017 23:59:53 +0800 Subject: [PATCH 3/5] [T6A1] Make StorageFile class inheriting the abstract Storage class --- .../addressbook/storage/StorageFile.java | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/src/seedu/addressbook/storage/StorageFile.java b/src/seedu/addressbook/storage/StorageFile.java index 693097a86..a841bd1ce 100644 --- a/src/seedu/addressbook/storage/StorageFile.java +++ b/src/seedu/addressbook/storage/StorageFile.java @@ -15,49 +15,30 @@ /** * 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 final static String DEFAULT_STORAGE_FILEPATH = "addressbook.txt"; - /** 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. + /* Note: Note the use of nested classes below. * More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html */ - - /** - * Signals that the given file path does not fulfill the storage filepath constraints. - */ - public static class InvalidStorageFilePathException extends IllegalValueException { - public InvalidStorageFilePathException(String message) { - super(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 + * @throws InvalidStoragePathException if the default path is invalid */ - public StorageFile() throws InvalidStorageFilePathException { + public StorageFile() throws InvalidStoragePathException { this(DEFAULT_STORAGE_FILEPATH); } /** * @throws InvalidStorageFilePathException if the given file path is invalid */ - public StorageFile(String filePath) throws InvalidStorageFilePathException { + public StorageFile(String filePath) throws InvalidStoragePathException { try { jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class); } catch (JAXBException jaxbe) { @@ -66,7 +47,7 @@ public StorageFile(String filePath) throws InvalidStorageFilePathException { path = Paths.get(filePath); if (!isValidPath(path)) { - throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); + throw new InvalidStoragePathException("Storage file should end with '.txt'"); } } From fd73a2c96f0773a5f390373e39f246dcbfe39166 Mon Sep 17 00:00:00 2001 From: Penghao Chen Date: Thu, 23 Feb 2017 00:12:13 +0800 Subject: [PATCH 4/5] [T6A1] DI: Create class StorageStub --- .../addressbook/storage/StorageStub.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/seedu/addressbook/storage/StorageStub.java diff --git a/src/seedu/addressbook/storage/StorageStub.java b/src/seedu/addressbook/storage/StorageStub.java new file mode 100644 index 000000000..7c57aa304 --- /dev/null +++ b/src/seedu/addressbook/storage/StorageStub.java @@ -0,0 +1,67 @@ +package seedu.addressbook.storage; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.storage.Storage.InvalidStoragePathException; +import seedu.addressbook.storage.jaxb.AdaptedAddressBook; + +public class StorageStub extends Storage { + + /** Default file path used if the user doesn't provide the file name. */ + public final static 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; + + + public final Path path; + public StorageStub() throws InvalidStoragePathException { + this(DEFAULT_STORAGE_FILEPATH); + } + + /** + * @throws InvalidStorageFilePathException if the given file path is invalid + */ + public StorageStub(String filePath) throws InvalidStoragePathException { + try { + jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class); + } catch (JAXBException jaxbe) { + throw new RuntimeException("jaxb initialisation error"); + } + + path = Paths.get(filePath); + if (!isValidPath(path)) { + throw new InvalidStoragePathException("Storage file should end with '.txt'"); + } + } + + private boolean isValidPath(Path filePath) { + return filePath.toString().endsWith(".txt"); + } + + @Override + public AddressBook load() throws StorageOperationException { + // TODO Auto-generated method stub + return null; + } + + @Override + public void save(AddressBook addressBook) throws StorageOperationException { + return; + + } + + @Override + public String getPath() { + // TODO Auto-generated method stub + return null; + } + +} From ccddf0b53285bb36fc3fce551aaf91b3921975ec Mon Sep 17 00:00:00 2001 From: Penghao Chen Date: Thu, 23 Feb 2017 00:14:20 +0800 Subject: [PATCH 5/5] [T6A1] DI: Change LogicTest file to bring in StorageStub --- test/java/seedu/addressbook/logic/LogicTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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);