Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#108 from chrystalquek/autosort…
Browse files Browse the repository at this point in the history
…-person-list

Autosort person list
  • Loading branch information
chrystalquek authored Oct 27, 2020
2 parents 4e4d5cd + 27de31f commit 6227a24
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/person/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public AddressBook(ReadOnlyAddressBook toBeCopied) {
resetData(toBeCopied);
}

/**
* Sorts the content of the contact list by Name in ascending order.
*/
public void sortContacts() {
persons.sortList();
}

//// list overwrite operations

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/person/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Represents a Person's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {
public class Name implements Comparable<Name> {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
Expand Down Expand Up @@ -56,4 +56,9 @@ public int hashCode() {
return fullName.hashCode();
}

@Override
public int compareTo(Name name) {
requireNonNull(name);
return fullName.compareTo(name.fullName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.person.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.person.exceptions.PersonNotFoundException;
import seedu.address.model.person.util.PersonComparator;

/**
* A list of persons that enforces uniqueness between its elements and does not allow nulls.
Expand All @@ -27,6 +29,7 @@ public class UniquePersonList implements Iterable<Person> {
private final ObservableList<Person> internalList = FXCollections.observableArrayList();
private final ObservableList<Person> internalUnmodifiableList =
FXCollections.unmodifiableObservableList(internalList);
private final PersonComparator personComparator = new PersonComparator();

/**
* Returns true if the list contains an equivalent person as the given argument.
Expand All @@ -46,6 +49,7 @@ public void add(Person toAdd) {
throw new DuplicatePersonException();
}
internalList.add(toAdd);
sortList();
}

/**
Expand All @@ -66,6 +70,7 @@ public void setPerson(Person target, Person editedPerson) {
}

internalList.set(index, editedPerson);
sortList();
}

/**
Expand All @@ -82,6 +87,7 @@ public void remove(Person toRemove) {
public void setPersons(UniquePersonList replacement) {
requireNonNull(replacement);
internalList.setAll(replacement.internalList);
sortList();
}

/**
Expand All @@ -95,6 +101,7 @@ public void setPersons(List<Person> persons) {
}

internalList.setAll(persons);
sortList();
}

/**
Expand All @@ -104,8 +111,17 @@ public ObservableList<Person> asUnmodifiableObservableList() {
return internalUnmodifiableList;
}

/**
* Sort the list by Name's value
*
*/
public void sortList() {
Collections.sort(internalList, personComparator);
}

@Override
public Iterator<Person> iterator() {
sortList();
return internalList.iterator();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.model.person.util;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Comparator;

import seedu.address.model.person.person.Name;
import seedu.address.model.person.person.Person;

/**
* Represents a Comparator for Person which sorts by Name.
*/
public class PersonComparator implements Comparator<Person> {
/**
* Compares Persons by Name in ascending order.
*/
public int compare(Person a, Person b) {
requireAllNonNull(a, b);
Name aName = a.getName();
Name bName = b.getName();

return aName.compareTo(bName);
}
}
15 changes: 15 additions & 0 deletions src/test/java/seedu/address/model/person/person/NameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ public void isValidName() {
assertTrue(Name.isValidName("Capital Tan")); // with capital letters
assertTrue(Name.isValidName("David Roger Jackson Ray Jr 2nd")); // long names
}

@Test
public void compareTo() {
Name peterJack = new Name("peter jack");
Name peterJackCaps = new Name("Peter Jack");
Name peter = new Name("peter");

// small letter > big letter
assertTrue(peterJack.compareTo(peterJackCaps) > 0);

// longer name > shorter name
assertTrue(peterJack.compareTo(peter) > 0);

assertThrows(NullPointerException.class, () -> peterJack.compareTo(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BOB;
import static seedu.address.testutil.TypicalPersons.CARL;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -57,6 +58,16 @@ public void add_duplicatePerson_throwsDuplicatePersonException() {
assertThrows(DuplicatePersonException.class, () -> uniquePersonList.add(ALICE));
}

@Test
public void add_person_sortSuccess() {
uniquePersonList.add(ALICE);
uniquePersonList.add(BOB);
UniquePersonList expectedUniquePersonList = new UniquePersonList();
expectedUniquePersonList.add(BOB);
expectedUniquePersonList.add(ALICE);
assertEquals(expectedUniquePersonList, uniquePersonList);
}

@Test
public void setPerson_nullTargetPerson_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> uniquePersonList.setPerson(null, ALICE));
Expand Down Expand Up @@ -108,6 +119,17 @@ public void setPerson_editedPersonHasNonUniqueIdentity_throwsDuplicatePersonExce
assertThrows(DuplicatePersonException.class, () -> uniquePersonList.setPerson(ALICE, BOB));
}

@Test
public void setPerson_sortSuccess() {
uniquePersonList.add(ALICE);
uniquePersonList.add(BOB);
UniquePersonList expectedUniquePersonList = new UniquePersonList();
expectedUniquePersonList.add(BOB);
expectedUniquePersonList.add(CARL);
expectedUniquePersonList.setPerson(CARL, ALICE);
assertEquals(expectedUniquePersonList, uniquePersonList);
}

@Test
public void remove_nullPerson_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> uniquePersonList.remove(null));
Expand All @@ -126,6 +148,18 @@ public void remove_existingPerson_removesPerson() {
assertEquals(expectedUniquePersonList, uniquePersonList);
}

@Test
public void remove_person_sortSuccess() {
uniquePersonList.add(ALICE);
uniquePersonList.add(CARL);
UniquePersonList expectedUniquePersonList = new UniquePersonList();
expectedUniquePersonList.add(ALICE);
expectedUniquePersonList.add(BOB);
expectedUniquePersonList.add(CARL);
expectedUniquePersonList.remove(BOB);
assertEquals(expectedUniquePersonList, uniquePersonList);
}

@Test
public void setPersons_nullUniquePersonList_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> uniquePersonList.setPersons((UniquePersonList) null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.model.person.util;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;

import org.junit.jupiter.api.Test;

import seedu.address.model.person.person.Person;
import seedu.address.testutil.PersonBuilder;

public class PersonComparatorTest {

private final PersonComparator personComparator = new PersonComparator();

@Test
public void isCorrectOrder() {
assertTrue(personComparator.compare(ALICE, ALICE) == 0);

assertTrue(personComparator.compare(ALICE, BENSON) < 0);
assertTrue(personComparator.compare(BENSON, ALICE) > 0);

// Alice Pauline < alice Pauline
Person editedAlice = new PersonBuilder(ALICE).withName("alice Pauline").build();
assertTrue(personComparator.compare(ALICE, editedAlice) < 0);

// Alice Pauline > Alice
editedAlice = new PersonBuilder(ALICE).withName("Alice").build();
assertTrue(personComparator.compare(ALICE, editedAlice) > 0);

// null Persons
assertThrows(NullPointerException.class, () -> personComparator.compare(null, ALICE));
assertThrows(NullPointerException.class, () -> personComparator.compare(ALICE, null));
}
}

0 comments on commit 6227a24

Please sign in to comment.