forked from se-edu/addressbook-level2
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Storage uses the JAXB module for storing and retrieving of the address book data in a file. However, we are trying to ensure Java 9 compatibility in #381, but the JAXB module is deprecated from Java 9 onwards. As use of deprecated modules is considered bad practice, we should replace our storage needs with another library before moving on to ensure Java 9 compatibility. As suggested here[1], let's reuse the storage code from addressbook-level1. [1]: se-edu#382 (comment)
- Loading branch information
Showing
13 changed files
with
162 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import static seedu.addressbook.parser.Parser.PERSON_DATA_ARGS_FORMAT; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.Address; | ||
import seedu.addressbook.data.person.Email; | ||
import seedu.addressbook.data.person.Name; | ||
import seedu.addressbook.data.person.Person; | ||
import seedu.addressbook.data.person.Phone; | ||
import seedu.addressbook.data.person.UniquePersonList; | ||
import seedu.addressbook.data.tag.Tag; | ||
import seedu.addressbook.storage.StorageFile.StorageOperationException; | ||
|
||
/** | ||
* Decodes the storage data file into an {@code AddressBook} object. | ||
*/ | ||
public class AddressBookDecoder { | ||
|
||
/** | ||
* Decodes {@code encodedAddressBook} into an {@code AddressBook} containing the decoded persons. | ||
* | ||
* @throws IllegalValueException if any of the fields in any encoded person string is invalid. | ||
* @throws StorageOperationException if the {@code encodedAddressBook} is in an invalid format. | ||
*/ | ||
public static AddressBook decodeAddressBook(List<String> encodedAddressBook) | ||
throws IllegalValueException, StorageOperationException { | ||
final List<Person> decodedPersons = new ArrayList<>(); | ||
for (String encodedPerson : encodedAddressBook) { | ||
decodedPersons.add(decodePersonFromString(encodedPerson)); | ||
} | ||
return new AddressBook(new UniquePersonList(decodedPersons)); | ||
} | ||
|
||
/** | ||
* Decodes {@code encodedPerson} into a {@code Person}. | ||
* | ||
* @throws IllegalValueException if any field in the {@code encodedPerson} is invalid. | ||
* @throws StorageOperationException if {@code encodedPerson} is in an invalid format. | ||
*/ | ||
private static Person decodePersonFromString(String encodedPerson) | ||
throws IllegalValueException, StorageOperationException { | ||
final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(encodedPerson); | ||
if (!matcher.matches()) { | ||
throw new StorageOperationException("Encoded person in invalid format. Unable to decode."); | ||
} | ||
|
||
return new Person( | ||
new Name(matcher.group("name")), | ||
new Phone(matcher.group("phone"), isPrivatePrefixPresent(matcher.group("isPhonePrivate"))), | ||
new Email(matcher.group("email"), isPrivatePrefixPresent(matcher.group("isEmailPrivate"))), | ||
new Address(matcher.group("address"), isPrivatePrefixPresent(matcher.group("isAddressPrivate"))), | ||
getTagsFromEncodedPerson(matcher.group("tagArguments")) | ||
); | ||
} | ||
|
||
/** | ||
* Returns true if {@code matchedPrefix} is equal to the private prefix for contact details. | ||
*/ | ||
private static boolean isPrivatePrefixPresent(String matchedPrefix) { | ||
return "p".equals(matchedPrefix); | ||
} | ||
|
||
/** | ||
* Extracts the {@code Tag}s from the {@code tagArguments} string. | ||
* Merges duplicate tag strings. | ||
*/ | ||
private static Set<Tag> getTagsFromEncodedPerson(String tagArguments) throws IllegalValueException { | ||
if (tagArguments.isEmpty()) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
// replace first delimiter prefix, then split | ||
final String[] tagStrings = tagArguments.replaceFirst(" t/", "").split(" t/"); | ||
final Set<Tag> tagSet = new HashSet<>(); | ||
for (String tagName : tagStrings) { | ||
tagSet.add(new Tag(tagName)); | ||
} | ||
|
||
return tagSet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.person.Person; | ||
|
||
/** | ||
* Encodes the {@code AddressBook} object into a data file for storage. | ||
*/ | ||
public class AddressBookEncoder { | ||
|
||
/** | ||
* Encodes all the {@code Person} in the {@code toSave} into a list of decodable and readable string presentation | ||
* for storage. | ||
*/ | ||
public static List<String> encodeAddressBook(AddressBook toSave) { | ||
final List<String> encodedPersons = new ArrayList<>(); | ||
toSave.getAllPersons().forEach(person -> encodedPersons.add(encodePersonToString(person))); | ||
return encodedPersons; | ||
} | ||
|
||
/** | ||
* Encodes the {@code person} into a decodable and readable string representation. | ||
*/ | ||
private static String encodePersonToString(Person person) { | ||
final StringBuilder encodedPersonBuilder = new StringBuilder(); | ||
|
||
encodedPersonBuilder.append(person.getName()); | ||
|
||
encodedPersonBuilder.append(person.getPhone().isPrivate() ? " p" : " "); | ||
encodedPersonBuilder.append("p/").append(person.getPhone().value); | ||
|
||
encodedPersonBuilder.append(person.getEmail().isPrivate() ? " p" : " "); | ||
encodedPersonBuilder.append("e/").append(person.getEmail().value); | ||
|
||
encodedPersonBuilder.append(person.getAddress().isPrivate() ? " p" : " "); | ||
encodedPersonBuilder.append("a/").append(person.getAddress().value); | ||
|
||
person.getTags().forEach(tag -> encodedPersonBuilder.append(" t/").append(tag.tagName)); | ||
|
||
return encodedPersonBuilder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
src/seedu/addressbook/storage/jaxb/AdaptedAddressBook.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.