/* * File: ToDo.java * Copy: Copyright (c) by David G. Green, All Rights Reserved * Vers: 1.0.5 24 Jan 2014 dgg -- update javadoc content * Vers: 1.0.4 26 Dec 2006 dgg -- fixed typo * Vers: 1.0.3 07 Oct 2006 dgg -- original coding */ import java.util.Date; import java.text.DateFormat; import java.text.ParseException; /** * Model a ToDo item */ public class ToDo { // Date formatter private static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); private static long serialNumberCounter = 1; // next serial number to assign private String description; // Description of todo private boolean complete = false; // Is todo complete? private long serialNumber; // Serial number of this to do private long lastModified; // Time stamp corresponding to last change /** * Priority values */ public static final int LOW = 0; public static final int NORMAL = 1; public static final int HIGH = 2; private int priority = NORMAL; // Priority of the ToDo private Date dueDate; // Due date private String context; // context category /** * Constructor to build minimal ToDo given a text description of the ToDo */ public ToDo(String description) { this.serialNumber = serialNumberCounter++; this.description = description; updateLastModified(); } /** * Constructor - make a new ToDo using the values of an existing ToDo (duplicates serialNumber) */ public ToDo(ToDo todo) { this.complete = todo.complete; this.context = todo.context; this.description = todo.description; this.dueDate = todo.dueDate; this.lastModified = todo.lastModified; this.priority = todo.priority; this.serialNumber = todo.serialNumber; } /** * Query whether the ToDo is complete or not * @return the complete */ public boolean isComplete() { return complete; } /** * Command to set the complete status of a ToDo * @param complete the complete to set */ public void setComplete(boolean complete) { this.complete = complete; updateLastModified(); } /** * Query to obtain the description of the ToDo * @return the description */ public String getDescription() { return description; } /** * Command to set (change) the description of the ToDo * @param description the description to set */ public void setDescription(String description) { this.description = description; updateLastModified(); } /** * Query to retrieve the priority of the ToDo * @return the priority */ public int getPriority() { return priority; } /** * Query to determine if this ToDo is same as the supplied ToDo * where ToDo's are considered the same if they have the * same serial number and same last modified values * @return ToDo's are equal (same serial number and last modified) */ public boolean equals(Object obj) { // To override the inherited equals method, the equals method // must take an Object as a parameter // if same object return true if ( this == obj ) return true; // if supplied object is not a ToDo return false if ( ! (obj instanceof ToDo) ) return false; ToDo todo = (ToDo) obj; // compare the attributes per spec return this.serialNumber == todo.serialNumber && this.lastModified == todo.lastModified; } /** * Query to determin if this ToDo has "all" the same values as * @return ToDo's are same attributes (from cloning or synchronizing) */ public boolean same(ToDo todo) { return this.complete == todo.complete && this.context == todo.context && this.description == todo.description && this.dueDate == todo.dueDate && this.lastModified == todo.lastModified && this.priority == todo.priority && this.serialNumber == todo.serialNumber ; } /** * Command to set (change) the priority of a ToDo * @param priority the priority to set (ToDo.LOW, ToDo.NORMAL, ToDo.HIGH) */ public void setPriority(int priority) { if (priority == ToDo.LOW || priority == ToDo.NORMAL || priority == ToDo.HIGH ) { this.priority = priority; updateLastModified(); } } /** * Query to retrieve the serialNumberCount of the class. The next * ToDo will have this number as its serial number * @return the serialNumberCounter */ public static long getSerialNumberCounter() { return serialNumberCounter; } /** * Query retrieve the timestamp corresponding to when the ToDo was * last modified * @return the lastModified */ public long getLastModified() { return lastModified; } /** * Query to get the serialNumber of the ToDo * @return the serialNumber */ public long getSerialNumber() { return serialNumber; } /** * Query to return the context of the ToDo * @return the context */ public String getContext() { return context; } /** * Command to set the context of the ToDo * @param context the context to set */ public void setContext(String context) { this.context = context; updateLastModified(); } /** * Query to retrieve the due date of the ToDo * @return the dueDate */ public Date getDueDate() { return dueDate; } /** * Command to set the due date of the ToDo * @param dueDate the dueDate to set */ public void setDueDate(Date dueDate) { // round to a day this.dueDate = truncateDate(dueDate); updateLastModified(); } /** * If presented two ToDo's with the same serial number, * update the older ToDo to match other ToDo * @param a - first ToDo * @param b - second ToDo */ public static void sync( ToDo a, ToDo b) { if ( a.getSerialNumber() != b.getSerialNumber() ) return; if ( a.getLastModified() == b.getLastModified() ) return; if ( a.getLastModified() > b.getLastModified() ) update( a, b ); else update( b, a ); } // update the "to" ToDo from the "from" ToDo private static void update( ToDo from, ToDo to) { to.complete = from.complete; to.context = from.context; to.description = from.description; to.dueDate = from.dueDate; to.lastModified = from.lastModified; to.priority = from.priority; } /* * Command to update the lastModified attribute to now */ private void updateLastModified() { this.lastModified = (new Date()).getTime(); } /** * Query to return a simple string representation of the ToDo * @return [ ] description -or- * [X] description -or- * [ ] description Due: date -or- * [X] description Due: date */ public String toString() { String toDo = complete ? "[X] " : "[ ] "; toDo += description ; if ( dueDate != null ) toDo += " Due: " + df.format(dueDate); return toDo; } /** * Query to determine if the ToDo is overdue or not. It is overdue * if the due date is earlier (at least one day in current time zone) * than the supplied date * @param now * @return whether or not the supplied date is later (in days) than todo due date */ public boolean isOverDue(Date now) { // check to see if the due date day has past return ( truncateDate(now).getTime() > dueDate.getTime() ); } // truncate date in current timezone to be 0h 0m 0s 0 ms private Date truncateDate(Date aDate) { try { return df.parse(df.format(aDate)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }