forked from nus-cs2103-AY1617S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CS2103AUG2016-F10-C3/Add-Task-package
Added relevant classes in a new package
- Loading branch information
Showing
5 changed files
with
244 additions
and
1 deletion.
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,54 @@ | ||
package seedu.address.model.task; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
|
||
/** | ||
* Represents a Task's due date in the address book. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidDueDate(String)} | ||
*/ | ||
public class DueDate { | ||
|
||
public static final String MESSAGE_DUEDATE_CONSTRAINTS = "Task duedate numbers should only contain numbers"; | ||
public static final String DUEDATE_VALIDATION_REGEX = "\\d+"; | ||
|
||
public final String value; | ||
|
||
/** | ||
* Validates given due date. | ||
* | ||
* @throws IllegalValueException if given due date string is invalid. | ||
*/ | ||
public DueDate(String duedate) throws IllegalValueException { | ||
assert duedate != null; | ||
duedate = duedate.trim(); | ||
if (!isValidDueDate(duedate)) { | ||
throw new IllegalValueException(MESSAGE_DUEDATE_CONSTRAINTS); | ||
} | ||
this.value = duedate; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid person due date number. | ||
*/ | ||
public static boolean isValidDueDate(String test) { | ||
return test.matches(DUEDATE_VALIDATION_REGEX); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DueDate // instanceof handles nulls | ||
&& this.value.equals(((DueDate) other).value)); // state check | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
} |
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,55 @@ | ||
package seedu.address.model.task; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
|
||
/** | ||
* Represents a Person's name in the address book. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)} | ||
*/ | ||
public class Name { | ||
|
||
public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters"; | ||
public static final String NAME_VALIDATION_REGEX = "[\\p{Alnum} ]+"; | ||
|
||
public final String fullName; | ||
|
||
/** | ||
* Validates given name. | ||
* | ||
* @throws IllegalValueException if given name string is invalid. | ||
*/ | ||
public Name(String name) throws IllegalValueException { | ||
assert name != null; | ||
name = name.trim(); | ||
if (!isValidName(name)) { | ||
throw new IllegalValueException(MESSAGE_NAME_CONSTRAINTS); | ||
} | ||
this.fullName = name; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid person name. | ||
*/ | ||
public static boolean isValidName(String test) { | ||
return test.matches(NAME_VALIDATION_REGEX); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return fullName; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof Name // instanceof handles nulls | ||
&& this.fullName.equals(((Name) other).fullName)); // state check | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return fullName.hashCode(); | ||
} | ||
|
||
} |
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,57 @@ | ||
package seedu.address.model.task; | ||
|
||
import seedu.address.model.tag.UniqueTagList; | ||
|
||
/** | ||
* A read-only immutable interface for a Task in the GTD. | ||
* Implementations should guarantee: details are present and not null, field values are validated. | ||
*/ | ||
public interface ReadOnlyTask { | ||
|
||
Name getName(); | ||
DueDate getDueDate(); | ||
|
||
/** | ||
* The returned TagList is a deep copy of the internal TagList, | ||
* changes on the returned list will not affect the task's internal tags. | ||
*/ | ||
UniqueTagList getTags(); | ||
|
||
/** | ||
* Returns true if both have the same state. (interfaces cannot override .equals) | ||
*/ | ||
default boolean isSameStateAs(ReadOnlyTask other) { | ||
return other == this // short circuit if same object | ||
|| (other != null // this is first to avoid NPE below | ||
&& other.getName().equals(this.getName()) // state checks here onwards | ||
&& other.getDueDate().equals(this.getDueDate())); | ||
} | ||
|
||
/** | ||
* Formats the task as text, showing all contact details. | ||
*/ | ||
default String getAsText() { | ||
final StringBuilder builder = new StringBuilder(); | ||
builder.append(getName()) | ||
.append(" DueDate: ") | ||
.append(getDueDate()) | ||
.append(" Tags: "); | ||
getTags().forEach(builder::append); | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* Returns a string representation of this Task's tags | ||
*/ | ||
default String tagsString() { | ||
final StringBuffer buffer = new StringBuffer(); | ||
final String separator = ", "; | ||
getTags().forEach(tag -> buffer.append(tag).append(separator)); | ||
if (buffer.length() == 0) { | ||
return ""; | ||
} else { | ||
return buffer.substring(0, buffer.length() - separator.length()); | ||
} | ||
} | ||
|
||
} |
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,77 @@ | ||
package seedu.address.model.task; | ||
|
||
import seedu.address.commons.util.CollectionUtil; | ||
import seedu.address.model.person.ReadOnlyPerson; | ||
import seedu.address.model.tag.UniqueTagList; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a Person in the address book. | ||
* Guarantees: details are present and not null, field values are validated. | ||
*/ | ||
public class Task implements ReadOnlyTask { | ||
|
||
private Name name; | ||
private DueDate dueDate; | ||
|
||
private UniqueTagList tags; | ||
|
||
/** | ||
* Every field must be present and not null. | ||
*/ | ||
public Task(Name name, DueDate dueDate, UniqueTagList tags) { | ||
assert !CollectionUtil.isAnyNull(name, dueDate, tags); | ||
this.name = name; | ||
this.dueDate = dueDate; | ||
this.tags = new UniqueTagList(tags); // protect internal tags from changes in the arg list | ||
} | ||
|
||
/** | ||
* Copy constructor. | ||
*/ | ||
public Task(ReadOnlyTask source) { | ||
this(source.getName(), source.getDueDate() , source.getTags()); | ||
} | ||
|
||
@Override | ||
public Name getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public DueDate getDueDate() { | ||
return dueDate; | ||
} | ||
|
||
@Override | ||
public UniqueTagList getTags() { | ||
return new UniqueTagList(tags); | ||
} | ||
|
||
/** | ||
* Replaces this person's tags with the tags in the argument tag list. | ||
*/ | ||
public void setTags(UniqueTagList replacement) { | ||
tags.setTags(replacement); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof ReadOnlyTask // instanceof handles nulls | ||
&& this.isSameStateAs((ReadOnlyTask) other)); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
// use this method for custom fields hashing instead of implementing your own | ||
return Objects.hash(name, dueDate, tags); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getAsText(); | ||
} | ||
|
||
} |