Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[T6A1][F12-B4] Lee Jun Han Bryan #524

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/Test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test

This is a test commit
46 changes: 28 additions & 18 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
package seedu.addressbook.logic;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.CommandResult;
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;
import java.util.List;
import java.util.Optional;

/**
* Represents the main Logic of the AddressBook.
*/
public class Logic {


private StorageFile storage;
private Storage storage;
private AddressBook addressBook;

/** The list of person shown to the user most recently. */
/** The list of person shown to the user most recently. */
private List<? extends ReadOnlyPerson> lastShownList = Collections.emptyList();

public Logic() throws Exception{
public Logic() throws Exception {
setStorage(initializeStorage());
setAddressBook(storage.load());
}

Logic(StorageFile storageFile, AddressBook addressBook){
Logic(Storage storageFile, AddressBook addressBook) {
setStorage(storageFile);
setAddressBook(addressBook);
}

void setStorage(StorageFile storage){
this.storage = storage;
void setStorage(Storage saveFile) {
this.storage = saveFile;
}

void setAddressBook(AddressBook addressBook){
void setAddressBook(AddressBook addressBook) {
this.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.
* 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 {
return new StorageFile();
Expand All @@ -66,7 +69,9 @@ protected void setLastShownList(List<? extends ReadOnlyPerson> newList) {

/**
* Parses the user command, executes it, and returns the result.
* @throws Exception if there was any problem during command execution.
*
* @throws Exception
* if there was any problem during command execution.
*/
public CommandResult execute(String userCommandText) throws Exception {
Command command = new Parser().parseCommand(userCommandText);
Expand All @@ -78,9 +83,11 @@ public CommandResult execute(String userCommandText) throws Exception {
/**
* Executes the command, updates storage, and returns the result.
*
* @param command user command
* @param command
* user command
* @return result of the command
* @throws Exception if there was any problem during command execution.
* @throws Exception
* if there was any problem during command execution.
*/
private CommandResult execute(Command command) throws Exception {
command.setData(addressBook, lastShownList);
Expand All @@ -89,7 +96,10 @@ private CommandResult execute(Command command) throws Exception {
return result;
}

/** Updates the {@link #lastShownList} if the result contains a list of Persons. */
/**
* Updates the {@link #lastShownList} if the result contains a list of
* Persons.
*/
private void recordResult(CommandResult result) {
final Optional<List<? extends ReadOnlyPerson>> personList = result.getRelevantPersons();
if (personList.isPresent()) {
Expand Down
90 changes: 90 additions & 0 deletions src/seedu/addressbook/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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.data.exception.IllegalValueException;
import seedu.addressbook.storage.jaxb.AdaptedAddressBook;

public abstract class Storage {
Copy link

Choose a reason for hiding this comment

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

This class and its methods should have header comments. If not, developers who need to inherit from this class will not know how exactly they should override the methods and other developers who write client code for this class will not know how to use it either.

/** 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
*/

/**
* 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 occurred 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 final JAXBContext jaxbContext;

public final Path path;

/**
* @throws InvalidStorageFilePathException
* if the default path is invalid
*/
public Storage() throws InvalidStorageFilePathException {
this(DEFAULT_STORAGE_FILEPATH);
}

public Storage(String filePath) throws InvalidStorageFilePathException {
try {
jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class);
} catch (JAXBException jaxbe) {
throw new RuntimeException("jaxb initialisation error");
}
path = Paths.get(filePath);
if (!isValidPath(path)) {
throw new InvalidStorageFilePathException("Storage file should end with '.txt'");
}
}

/**
* Returns true if the given path is acceptable as a storage file. The file
* path is considered acceptable if it ends with '.txt'
*/
public abstract boolean isValidPath(Path filePath);

/**
* Saves all data to this storage file.
*
* @throws StorageOperationException
* if there were errors converting and/or storing data to file.
*/
public abstract void save(AddressBook addressBook) throws StorageOperationException;

/**
* Loads data from this storage file.
*
* @throws StorageOperationException
* if there were errors reading and/or converting data from
* file.
*/
public abstract AddressBook load() throws StorageOperationException;

public abstract String getPath();
}
116 changes: 52 additions & 64 deletions src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,77 @@
package seedu.addressbook.storage;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.storage.jaxb.AdaptedAddressBook;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.storage.jaxb.AdaptedAddressBook;

/**
* 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
*/

/**
* 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.
/*
* Note: Note the use of nested classes below. More info
* https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
*/
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 InvalidStorageFilePathException
* if the default path is invalid
*/
public StorageFile() throws InvalidStorageFilePathException {
this(DEFAULT_STORAGE_FILEPATH);
public StorageFile()
throws InvalidStorageFilePathException, seedu.addressbook.storage.Storage.InvalidStorageFilePathException {
super();
}

/**
* @throws InvalidStorageFilePathException if the given file path is invalid
* @throws InvalidStorageFilePathException
* if the given file path is invalid
*/
public StorageFile(String filePath) throws InvalidStorageFilePathException {
try {
jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class);
} catch (JAXBException jaxbe) {
throw new RuntimeException("jaxb initialisation error");
}

path = Paths.get(filePath);
if (!isValidPath(path)) {
throw new InvalidStorageFilePathException("Storage file should end with '.txt'");
}
super();
}

/**
* Returns true if the given path is acceptable as a storage file.
* The file path is considered acceptable if it ends with '.txt'
* Returns true if the given path is acceptable as a storage file. The file
* path is considered acceptable if it ends with '.txt'
*/
private static boolean isValidPath(Path filePath) {
@Override
public boolean isValidPath(Path filePath) {
return filePath.toString().endsWith(".txt");
}

/**
* Saves all data to this storage file.
*
* @throws StorageOperationException if there were errors converting and/or storing data to file.
* @throws StorageOperationException
* if there were errors converting and/or storing data to file.
*/
@Override
public void save(AddressBook addressBook) throws StorageOperationException {

/* Note: Note the 'try with resource' statement below.
* More info: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
/*
* Note: Note the 'try with resource' statement below. More info:
* https://docs.oracle.com/javase/tutorial/essential/exceptions/
* tryResourceClose.html
*/
try (final Writer fileWriter =
new BufferedWriter(new FileWriter(path.toFile()))) {
try (final Writer fileWriter = new BufferedWriter(new FileWriter(path.toFile()))) {

final AdaptedAddressBook toSave = new AdaptedAddressBook(addressBook);
final Marshaller marshaller = jaxbContext.createMarshaller();
Expand All @@ -106,11 +88,13 @@ public void save(AddressBook addressBook) throws StorageOperationException {
/**
* Loads data from this storage file.
*
* @throws StorageOperationException if there were errors reading and/or converting data from file.
* @throws StorageOperationException
* if there were errors reading and/or converting data from
* file.
*/
@Override
public AddressBook load() throws StorageOperationException {
try (final Reader fileReader =
new BufferedReader(new FileReader(path.toFile()))) {
try (final Reader fileReader = new BufferedReader(new FileReader(path.toFile()))) {

final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
final AdaptedAddressBook loaded = (AdaptedAddressBook) unmarshaller.unmarshal(fileReader);
Expand All @@ -120,18 +104,21 @@ public AddressBook load() throws StorageOperationException {
}
return loaded.toModelType();

/* Note: Here, we are using an exception to create the file if it is missing. However, we should minimize
* using exceptions to facilitate normal paths of execution. If we consider the missing file as a 'normal'
* situation (i.e. not truly exceptional) we should not use an exception to handle it.
*/
/*
* Note: Here, we are using an exception to create the file if it is
* missing. However, we should minimize using exceptions to
* facilitate normal paths of execution. If we consider the missing
* file as a 'normal' situation (i.e. not truly exceptional) we
* should not use an exception to handle it.
*/

// create empty file if not found
// create empty file if not found
} catch (FileNotFoundException fnfe) {
final AddressBook empty = new AddressBook();
save(empty);
return empty;

// other errors
// other errors
} catch (IOException ioe) {
throw new StorageOperationException("Error writing to file: " + path);
} catch (JAXBException jaxbe) {
Expand All @@ -141,6 +128,7 @@ public AddressBook load() throws StorageOperationException {
}
}

@Override
public String getPath() {
return path.toString();
}
Expand Down
Loading