Skip to content

Commit

Permalink
Merge pull request #125 from woke02/branch-kelly-add-logging
Browse files Browse the repository at this point in the history
Add more logging
  • Loading branch information
ZweZeya authored Oct 24, 2024
2 parents a2b6e55 + 896d4db commit 399d0f1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 14 deletions.
99 changes: 89 additions & 10 deletions src/main/java/seedu/hireme/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import seedu.hireme.commons.core.LogsCenter;
import seedu.hireme.commons.util.CollectionUtil;


/**
* Represents the in-memory model of the address book data.
*
* @param <T> the type of elements in the AddressBook, which extends {@code HireMeComparable}
*/
public class ModelManager<T extends HireMeComparable<T>> implements Model<T> {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);
Expand All @@ -24,50 +25,90 @@ public class ModelManager<T extends HireMeComparable<T>> implements Model<T> {

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
*
* @param addressBook The address book containing the application data.
* @param userPrefs The user preferences.
*/
public ModelManager(ReadOnlyAddressBook<T> addressBook, ReadOnlyUserPrefs userPrefs) {
CollectionUtil.requireAllNonNull(addressBook, userPrefs);

logger.fine("Initializing with address book: " + addressBook + " and user prefs " + userPrefs);
logger.info("Initializing ModelManager with AddressBook and UserPrefs");
logger.fine("AddressBook: " + addressBook + ", UserPrefs: " + userPrefs);

this.addressBook = new AddressBook<>(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
filtered = new FilteredList<>(this.addressBook.getList());

logger.fine("Filtered list initialized with " + filtered.size() + " items.");
}

/**
* Initializes a ModelManager with default values.
*/
public ModelManager() {
this(new AddressBook<>(), new UserPrefs());
logger.info("Initialized default ModelManager");
}

//=========== UserPrefs ==================================================================================

/**
* Sets the user preferences.
*
* @param userPrefs The new user preferences to be saved.
*/
@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
requireNonNull(userPrefs);
this.userPrefs.resetData(userPrefs);
}

/**
* Gets the user preferences.
*
* @return the {@code ReadOnlyUserPrefs} object.
*/
@Override
public ReadOnlyUserPrefs getUserPrefs() {
return userPrefs;
}

/**
* Gets the GUI settings for the application.
*
* @return the current {@code GuiSettings}.
*/
@Override
public GuiSettings getGuiSettings() {
return userPrefs.getGuiSettings();
}

/**
* Sets the GUI settings for the application.
*
* @param guiSettings the new {@code GuiSettings} to be saved.
*/
@Override
public void setGuiSettings(GuiSettings guiSettings) {
requireNonNull(guiSettings);
userPrefs.setGuiSettings(guiSettings);
}

/**
* Gets the file path to the HireMe data file.
*
* @return the {@code Path} to the HireMe data file.
*/
@Override
public Path getHireMeFilePath() {
return userPrefs.getHireMeFilePath();
}

/**
* Sets the file path to the HireMe data file.
*
* @param hireMeFilePath The new file path to be saved.
*/
@Override
public void setHireMeFilePath(Path hireMeFilePath) {
requireNonNull(hireMeFilePath);
Expand All @@ -76,27 +117,53 @@ public void setHireMeFilePath(Path hireMeFilePath) {

//=========== AddressBook ================================================================================

/**
* Replaces the address book data with the provided {@code ReadOnlyAddressBook}.
*
* @param addressBook the new address book data.
*/
@Override
public void setAddressBook(ReadOnlyAddressBook<T> addressBook) {
this.addressBook.resetData(addressBook);
}

/**
* Gets the current address book.
*
* @return the current {@code ReadOnlyAddressBook}.
*/
@Override
public ReadOnlyAddressBook<T> getAddressBook() {
return addressBook;
}

/**
* Checks if the address book contains the specified item.
*
* @param item the item to check.
* @return true if the item exists in the address book, otherwise false.
*/
@Override
public boolean hasItem(T item) {
requireNonNull(item);
return addressBook.hasItem(item);
}

/**
* Deletes the specified item from the address book.
*
* @param target the item to delete.
*/
@Override
public void deleteItem(T target) {
addressBook.removeItem(target);
}

/**
* Adds a new item to the address book.
*
* @param item the item to add.
*/
@Override
public void addItem(T item) {
addressBook.addItem(item);
Expand All @@ -105,49 +172,61 @@ public void addItem(T item) {
updateFilteredList(showAll);
}

/**
* Replaces the specified item with the edited item in the address book.
*
* @param target the item to replace.
* @param edited the edited item.
*/
@Override
public void setItem(T target, T edited) {
CollectionUtil.requireAllNonNull(target, edited);

addressBook.setItem(target, edited);
}

//=========== Filtered List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of items backed by the internal list of
* {@code versionedAddressBook}
* Gets the filtered list of items.
*
* @return an unmodifiable view of the filtered list.
*/
@Override
public ObservableList<T> getFilteredList() {
return filtered;
}

/**
* Updates the predicate for the filtered list.
*
* @param predicate the new predicate to filter the list.
*/
@Override
public void updateFilteredList(Predicate<T> predicate) {
requireNonNull(predicate);
filtered.setPredicate(predicate);
}

/**
* Checks if this ModelManager is equal to another object.
*
* @param other the other object to compare with.
* @return true if both objects are equal, otherwise false.
*/
@Override
public boolean equals(Object other) {
// Check if the objects are the same instance
if (other == this) {
return true;
}

// Check if the other object is an instance of ModelManager and not null
if (!(other instanceof ModelManager<?>)) {
return false;
}

// Cast the other object to ModelManager
ModelManager<?> otherModelManager = (ModelManager<?>) other;

// Compare the state of addressBook, userPrefs, and filtered lists
return addressBook.equals(otherModelManager.addressBook)
&& userPrefs.equals(otherModelManager.userPrefs)
&& filtered.equals(otherModelManager.filtered);
}

}
16 changes: 12 additions & 4 deletions src/main/java/seedu/hireme/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,61 @@ public class StorageManager<T extends HireMeComparable<T>> implements Storage<T>
public StorageManager(AddressBookStorage<T> addressBookStorage, UserPrefsStorage userPrefsStorage) {
this.addressBookStorage = addressBookStorage;
this.userPrefsStorage = userPrefsStorage;
logger.info("StorageManager initialized with AddressBookStorage and UserPrefsStorage.");
}

// ================ UserPrefs methods ==============================

@Override
public Path getUserPrefsFilePath() {
logger.fine("Fetching UserPrefs file path.");
return userPrefsStorage.getUserPrefsFilePath();
}

@Override
public Optional<UserPrefs> readUserPrefs() throws DataLoadingException {
logger.info("Reading UserPrefs from storage.");
return userPrefsStorage.readUserPrefs();
}

@Override
public void saveUserPrefs(ReadOnlyUserPrefs userPrefs) throws IOException {
logger.info("Saving UserPrefs to storage.");
userPrefsStorage.saveUserPrefs(userPrefs);
logger.fine("UserPrefs saved successfully.");
}


// ================ AddressBook methods ==============================

@Override
public Path getAddressBookFilePath() {
logger.fine("Fetching AddressBook file path.");
return addressBookStorage.getAddressBookFilePath();
}

@Override
public Optional<ReadOnlyAddressBook<T>> readAddressBook() throws DataLoadingException {
logger.info("Reading AddressBook from default file path.");
return readAddressBook(addressBookStorage.getAddressBookFilePath());
}

@Override
public Optional<ReadOnlyAddressBook<T>> readAddressBook(Path filePath) throws DataLoadingException {
logger.fine("Attempting to read data from file: " + filePath);
logger.info("Reading AddressBook from file: " + filePath);
return addressBookStorage.readAddressBook(filePath);
}

@Override
public void saveAddressBook(ReadOnlyAddressBook<T> addressBook) throws IOException {
logger.info("Saving AddressBook to default file path.");
saveAddressBook(addressBook, addressBookStorage.getAddressBookFilePath());
logger.fine("AddressBook saved successfully.");
}

@Override
public void saveAddressBook(ReadOnlyAddressBook<T> addressBook, Path filePath) throws IOException {
logger.fine("Attempting to write to data file: " + filePath);
logger.info("Saving AddressBook to file: " + filePath);
addressBookStorage.saveAddressBook(addressBook, filePath);
logger.fine("AddressBook saved successfully to file: " + filePath);
}

}
12 changes: 12 additions & 0 deletions src/test/java/seedu/hireme/storage/StorageManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,16 @@ public void getAddressBookFilePath() {
assertNotNull(storageManager.getAddressBookFilePath());
}

@Test
public void getUserPrefsFilePath_correctPathReturned() {
// Setup: Ensure that the file path is not null and correct
Path expectedPath = getTempFilePath("prefs");

// Act: Get the UserPrefs file path from StorageManager
Path actualPath = storageManager.getUserPrefsFilePath();

// Assert: Verify that the path is not null and matches the expected path
assertNotNull(actualPath, "The UserPrefs file path should not be null.");
assertEquals(expectedPath, actualPath, "The UserPrefs file path should match the expected path.");
}
}

0 comments on commit 399d0f1

Please sign in to comment.