Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2223S1#74 from peppapighs/73-add-cont…
Browse files Browse the repository at this point in the history
…act-task-bridge

Add Contact Task Bridge Relation
  • Loading branch information
jasonqiu212 authored Oct 17, 2022
2 parents 7b2a5d7 + d2bad62 commit 2561def
Show file tree
Hide file tree
Showing 53 changed files with 1,470 additions and 368 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
"java.configuration.updateBuildConfiguration": "interactive",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m -Xlog:disable"
}
5 changes: 5 additions & 0 deletions src/main/java/swift/commons/core/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static Index fromOneBased(int oneBasedIndex) {
return new Index(oneBasedIndex - 1);
}

@Override
public int hashCode() {
return zeroBasedIndex;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/swift/logic/commands/AddTaskCommand.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package swift.logic.commands;

import static java.util.Objects.requireNonNull;
import static swift.commons.util.CollectionUtil.requireAllNonNull;
import static swift.logic.parser.CliSyntax.PREFIX_CONTACT;
import static swift.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.Collection;

import swift.commons.core.index.Index;
import swift.logic.commands.exceptions.CommandException;
import swift.model.Model;
import swift.model.task.Task;
Expand All @@ -19,35 +22,41 @@ public class AddTaskCommand extends Command {
+ ": Adds a task to the task list.\n"
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_CONTACT + "CONTACT\n"
+ "[" + PREFIX_CONTACT + "CONTACT]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "CS2103T Tutorial "
+ PREFIX_CONTACT + "1";
+ PREFIX_CONTACT + "1 "
+ PREFIX_CONTACT + "2";

public static final String MESSAGE_SUCCESS = "New task added: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the address book";

private final Task toAdd;
private final Collection<Index> contactIndices;

/**
* Creates an AddTaskCommand to add the specified {@code Task}
*
* @param task Task to be added.
*/
public AddTaskCommand(Task task) {
requireNonNull(task);
public AddTaskCommand(Task task, Collection<Index> indices) {
requireAllNonNull(task, indices);
toAdd = task;
contactIndices = indices;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
requireAllNonNull(model);

if (model.hasTask(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_TASK);
}

model.addTask(toAdd);
for (Index index : contactIndices) {
model.addBridge(model.getAddressBook().getPersonList().get(index.getZeroBased()), toAdd);
}

return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/swift/logic/commands/EditContactCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import swift.commons.core.Messages;
import swift.commons.core.index.Index;
Expand Down Expand Up @@ -93,13 +94,14 @@ public CommandResult execute(Model model) throws CommandException {
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;

UUID updatedId = personToEdit.getId();
Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedId, updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/swift/logic/commands/EditTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import swift.commons.core.Messages;
import swift.commons.core.index.Index;
Expand Down Expand Up @@ -80,10 +81,10 @@ public CommandResult execute(Model model) throws CommandException {
private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTaskDescriptor) {
assert taskToEdit != null;

UUID updatedId = taskToEdit.getId();
TaskName updatedTaskName = editTaskDescriptor.getTaskName().orElse(taskToEdit.getTaskName());
Index updatedContactIndex = editTaskDescriptor.getContactIndex().orElse(taskToEdit.getContactIndex());

return new Task(updatedTaskName, updatedContactIndex);
return new Task(updatedId, updatedTaskName);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/swift/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static swift.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import swift.logic.commands.AddContactCommand;
Expand Down Expand Up @@ -38,13 +39,14 @@ public AddContactCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddContactCommand.MESSAGE_USAGE));
}

UUID id = UUID.randomUUID();
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, address, tagList);
Person person = new Person(id, name, phone, email, address, tagList);

return new AddContactCommand(person);
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/swift/logic/parser/AddTaskCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static swift.logic.parser.CliSyntax.PREFIX_CONTACT;
import static swift.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import swift.commons.core.index.Index;
Expand All @@ -27,18 +29,17 @@ public class AddTaskCommandParser implements Parser<AddTaskCommand> {
public AddTaskCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_CONTACT);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_CONTACT)
|| !argMultimap.getPreamble().isEmpty()) {
if (!arePrefixesPresent(argMultimap, PREFIX_NAME) || !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTaskCommand.MESSAGE_USAGE));
}

try {
TaskName name = ParserUtil.parseTaskName(argMultimap.getValue(PREFIX_NAME).get());
Index index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT).get());
Set<Index> indices = ParserUtil.parseIndices(argMultimap.getAllValues(PREFIX_CONTACT));

Task task = new Task(name, index);
Task task = new Task(UUID.randomUUID(), name);

return new AddTaskCommand(task);
return new AddTaskCommand(task, indices);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTaskCommand.MESSAGE_USAGE), pe);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/swift/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static Index parseIndex(String oneBasedIndex) throws ParseException {
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses {@code Collection<String> oneBasedIndices} into a {@code Set<Index>}.
*/
public static Set<Index> parseIndices(Collection<String> oneBasedIndices) throws ParseException {
requireNonNull(oneBasedIndices);
final Set<Index> indexSet = new HashSet<>();
for (String oneBasedIndex : oneBasedIndices) {
indexSet.add(parseIndex(oneBasedIndex));
}
return indexSet;
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/swift/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;

import javafx.collections.ObservableList;
import swift.model.bridge.PersonTaskBridge;
import swift.model.bridge.PersonTaskBridgeList;
import swift.model.person.Person;
import swift.model.person.UniquePersonList;
import swift.model.task.Task;
Expand All @@ -18,6 +20,7 @@ public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final UniqueTaskList tasks;
private final PersonTaskBridgeList bridges;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
Expand All @@ -29,6 +32,7 @@ public class AddressBook implements ReadOnlyAddressBook {
{
persons = new UniquePersonList();
tasks = new UniqueTaskList();
bridges = new PersonTaskBridgeList();
}

public AddressBook() {
Expand Down Expand Up @@ -144,6 +148,24 @@ public void removeTask(Task key) {
tasks.remove(key);
}

//// bridge-level operations

/**
* Returns true if a bridge with the same person and task as {@code bridge} exists in the address book.
*/
public boolean hasBridge(PersonTaskBridge bridge) {
requireNonNull(bridge);
return bridges.contains(bridge);
}

/**
* Adds a bridge to the address book.
* The bridge must not already exist in the address book.
*/
public void addBridge(PersonTaskBridge b) {
bridges.add(b);
}

//// util methods

@Override
Expand All @@ -163,6 +185,11 @@ public ObservableList<Task> getTaskList() {
return tasks.asUnmodifiableObservableList();
}

@Override
public ObservableList<PersonTaskBridge> getBridgeList() {
return bridges.asUnmodifiableObservableList();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/swift/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import swift.commons.core.GuiSettings;
import swift.model.bridge.PersonTaskBridge;
import swift.model.person.Person;
import swift.model.task.Task;

Expand Down Expand Up @@ -113,10 +114,33 @@ public interface Model {
*/
void addTask(Task task);

/**
* Returns true if a bridge exists in the address book.
*
* @param bridge the bridge to check
* @return true if the bridge exists
*/
boolean hasBridge(PersonTaskBridge bridge);

/**
* Replaces the given task {@code target} with {@code editedTask}.
* {@code target} must exist in the address book.
* The task name and owner of {@code editedTask} must not be the same as another existing task in the address book.
*/
void setTask(Task target, Task editedTask);

/**
* Adds a relationship between the given task and person.
*
* @param person The person to be added to the task.
* @param task The task to be added to the person.
*/
void addBridge(Person person, Task task);

/**
* Adds a relationship between the given task and person.
*
* @param bridge The bridge to be added.
*/
void addBridge(PersonTaskBridge bridge);
}
19 changes: 19 additions & 0 deletions src/main/java/swift/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import swift.commons.core.GuiSettings;
import swift.commons.core.LogsCenter;
import swift.model.bridge.PersonTaskBridge;
import swift.model.person.Person;
import swift.model.task.Task;

Expand Down Expand Up @@ -137,6 +138,24 @@ public void setTask(Task target, Task editedTask) {
addressBook.setTask(target, editedTask);
}

@Override
public boolean hasBridge(PersonTaskBridge bridge) {
requireNonNull(bridge);
return addressBook.hasBridge(bridge);
}

@Override
public void addBridge(Person person, Task task) {
requireAllNonNull(person, task);
addressBook.addBridge(new PersonTaskBridge(person.getId(), task.getId()));
}

@Override
public void addBridge(PersonTaskBridge bridge) {
requireNonNull(bridge);
addressBook.addBridge(bridge);
}

//=========== Filtered Task List Accessors =============================================================
/**
* Returns an unmodifiable view of the list of {@code Task} backed by the internal list of
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/swift/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package swift.model;

import javafx.collections.ObservableList;
import swift.model.bridge.PersonTaskBridge;
import swift.model.person.Person;
import swift.model.task.Task;

Expand All @@ -21,4 +22,9 @@ public interface ReadOnlyAddressBook {
*/
ObservableList<Task> getTaskList();

/**
* Returns an unmodifiable view of the person-task bridge list.
* This list will not contain any duplicate bridges.
*/
ObservableList<PersonTaskBridge> getBridgeList();
}
Loading

0 comments on commit 2561def

Please sign in to comment.