Java Reference
In-Depth Information
Example A.242 TaskImpl.java
1. import java.util.Date;
2. import java.io.Serializable;
3. import java.util.ArrayList;
4. public class TaskImpl implements Task{
5. private String taskID;
6. private Date lastEditDate;
7. private String taskName;
8. private String taskDetails;
9. private ArrayList subTasks = new ArrayList();
10.
11. public TaskImpl(){
12. lastEditDate = new Date();
13. taskName = "";
14. taskDetails = "";
15. }
16. public TaskImpl(String newTaskName, String newTaskDetails,
17. Date newEditDate, ArrayList newSubTasks){
18. lastEditDate = newEditDate;
19. taskName = newTaskName;
20. taskDetails = newTaskDetails;
21. if (newSubTasks != null){ subTasks = newSubTasks; }
22. }
23.
24. public String getTaskID(){
25. return taskID;
26. }
27. public Date getLastEditDate(){ return lastEditDate; }
28. public String getTaskName(){ return taskName; }
29. public String getTaskDetails(){ return taskDetails; }
30. public ArrayList getSubTasks(){ return subTasks; }
31.
32. public void setLastEditDate(Date newDate){
33. if (newDate.after(lastEditDate)){
34. lastEditDate = newDate;
35. }
36. }
37. public void setTaskName(String newName){ taskName = newName; }
38. public void setTaskDetails(String newDetails){ taskDetails = newDetails; }
39. public void addSubTask(Task task){
40. if (!subTasks.contains(task)){
41. subTasks.add(task);
42. }
43. }
44. public void removeSubTask(Task task){
45. subTasks.remove(task);
46. }
47.
48. public String toString(){
49. return taskName + " " + taskDetails;
50. }
51. }
Example A.243 UpdateException.java
1. public class UpdateException extends Exception{
2. public static final int TASK_UNCHANGED = 1;
3. public static final int TASK_OUT_OF_DATE = 2;
4. private int errorCode;
5.
6. public UpdateException(String cause, int newErrorCode){
7. super(cause);
8. errorCode = newErrorCode;
9. }
10. public UpdateException(String cause){ super(cause); }
11.
12. public int getErrorCode(){ return errorCode; }
13. }
RunPattern demonstrates how updates of a Task can be propagated to multiple clients. The main method creates
a ClientPullServer and two PullClient objects. Both clients are used to request a common Task , then one of
the PullClients makes an update to the Task . The change is reflected in the other client as its worker thread, the
ClientPullRequester , polls the server for changes.
Example A.244 RunPattern.java
1. import java.io.IOException;
Search WWH ::




Custom Search