1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.util.concurrent;
17
18 import static fulmine.util.Utils.COMMA;
19 import static fulmine.util.Utils.logException;
20 import static fulmine.util.Utils.safeToString;
21 import static fulmine.util.Utils.string;
22 import fulmine.util.log.AsyncLog;
23 import fulmine.util.reference.is;
24
25
26
27
28
29
30
31
32
33 public final class Task<TASK_TYPE> implements Runnable
34 {
35 private final static AsyncLog LOG = new AsyncLog(Task.class);
36
37
38 private final ITaskHandler<TASK_TYPE> handler;
39
40
41 private final TASK_TYPE task;
42
43
44
45
46
47
48
49
50
51 public Task(ITaskHandler<TASK_TYPE> handler, TASK_TYPE task)
52 {
53 super();
54 this.handler = handler;
55 this.task = task;
56 }
57
58
59
60
61 public void run()
62 {
63 try
64 {
65 this.handler.handleTask(this.task);
66 }
67 catch (Exception e)
68 {
69 logException(LOG, safeToString(this.handler)
70 + " could not process task " + safeToString(this.task), e);
71 }
72 }
73
74 @Override
75 public String toString()
76 {
77 return string(this, safeToString(this.handler) + COMMA
78 + safeToString(this.task));
79 }
80
81 @Override
82 public int hashCode()
83 {
84 final int prime = 31;
85 int result = 1;
86 result =
87 prime * result
88 + ((this.handler == null) ? 0 : this.handler.hashCode());
89 result =
90 prime * result + ((this.task == null) ? 0 : this.task.hashCode());
91 return result;
92 }
93
94 @SuppressWarnings("unchecked")
95 @Override
96 public boolean equals(Object obj)
97 {
98 if (is.same(this, obj))
99 {
100 return true;
101 }
102 if (is.differentClass(this, obj))
103 {
104 return false;
105 }
106 Task other = (Task) obj;
107 return is.eq(this.handler, other.handler)
108 && is.eq(this.task, other.task);
109 }
110 }