View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * Encapsulates a task to process using the provided {@link ITaskHandler}. This
27   * object is not active and is required to be run using a {@link Thread}.
28   * 
29   * @author Ramon Servadei
30   * @param <TASK_TYPE>
31   *            the type of task
32   */
33  public final class Task<TASK_TYPE> implements Runnable
34  {
35      private final static AsyncLog LOG = new AsyncLog(Task.class);
36  
37      /** The handler that will process the {@link #task} */
38      private final ITaskHandler<TASK_TYPE> handler;
39  
40      /** The task to process */
41      private final TASK_TYPE task;
42  
43      /**
44       * Construct this async task with the handler and task to process
45       * 
46       * @param handler
47       *            the handler
48       * @param task
49       *            the task to process
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       * Executes the task using the handler.
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 }