View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.event;
19  
20  import java.text.MessageFormat;
21  import java.util.HashSet;
22  import java.util.Set;
23  import java.util.concurrent.locks.ReadWriteLock;
24  import java.util.concurrent.locks.ReentrantReadWriteLock;
25  import org.apache.log4j.Logger;
26  
27  /**
28   * Class which defines a action that is currently being executed on an event.
29   * This class has one-to-one association with the {@link BaseEvent}.
30   * 
31   * @author sanjin
32   */
33  public class CurrentAction implements ContextEventAction {
34  
35      private static final Logger LOGGER = Logger.getLogger(CurrentAction.class);
36  
37      private final ReadWriteLock lock = new ReentrantReadWriteLock();
38      private final Set<String> processedBy = new HashSet<String>();
39      private final String name;
40      private final boolean required;
41  
42      /**
43       * Create instance of {@link CurrentAction} with specified name and
44       * requirement.
45       * 
46       * @param name the name of the {@link CurrentAction}.
47       * @param required if the {@link CurrentAction} should be required.
48       * @throws NullPointerException if specified name is <code>null</code>.
49       * @throws IllegalArgumentException if specified name is an empty or blank
50       *         string.
51       */
52      public CurrentAction(final String name, final boolean required) {
53          super();
54  
55          if (null == name) {
56              throw new NullPointerException("Name cannot be null.");
57          }
58          if (name.trim().isEmpty()) {
59              throw new IllegalArgumentException(
60                      "Name cannot be an empty or blank string.");
61          }
62  
63          this.name = name;
64          this.required = required;
65  
66          if (LOGGER.isDebugEnabled()) {
67              LOGGER.debug(MessageFormat.format(
68                      "Created current action ''{0}''.", this.name));
69          }
70      }
71  
72      /**
73       * Create instance of the {@link CurrentAction} from specified
74       * {@link UnprocessedAction}. Name and required properties of the
75       * {@link UnprocessedAction} are copied.
76       * 
77       * @param action the {@link UnprocessedAction}.
78       * @throws NullPointerException if specified {@link UnprocessedAction} is
79       *         <code>null</code>.
80       */
81      public CurrentAction(final UnprocessedAction action) {
82          this(getName(action), action.isRequired());
83      }
84  
85      @Override
86      public String getName() {
87          return this.name;
88      }
89  
90      /**
91       * @return if the {@link CurrentAction} is required.
92       */
93      public boolean isRequired() {
94          return this.required;
95      }
96  
97      /**
98       * @return name of all processors that processed this {@link CurrentAction}.
99       */
100     public String[] getProcessedBy() {
101         final String[] result;
102 
103         this.lock.readLock().lock();
104         try {
105             result = this.processedBy.toArray(new String[this.processedBy.size()]);
106         } finally {
107             this.lock.readLock().unlock();
108         }
109 
110         return result;
111     }
112 
113     /**
114      * @return if the {@link CurrentAction} has been processed by at least one
115      *         processor.
116      */
117     public boolean isProcessed() {
118         final boolean result;
119 
120         this.lock.readLock().lock();
121         try {
122             result = !this.processedBy.isEmpty();
123         } finally {
124             this.lock.readLock().unlock();
125         }
126 
127         return result;
128     }
129 
130     /**
131      * Adds specified processor name to the array of processors that processed
132      * this {@link CurrentAction}. Processor name cannot be <code>null</code> or
133      * an empty or blank string.
134      * 
135      * @param processor the name of the processor.
136      * @throws NullPointerException if specified processor name is
137      *         <code>null</code>.
138      * @throws IllegalArgumentException if specified processor name is an empty
139      *         or blank string.
140      */
141     public void processedBy(final String processor) {
142         if (null == processor) {
143             throw new NullPointerException("Processor cannot be null.");
144         }
145         if (processor.trim().isEmpty()) {
146             throw new IllegalArgumentException(
147                     "Processor cannot be an empty or blank string.");
148         }
149 
150         this.lock.writeLock().lock();
151         try {
152             this.processedBy.add(processor);
153         } finally {
154             this.lock.writeLock().unlock();
155         }
156     }
157 
158     /**
159      * Returns hash code for this {@link CurrentAction}. The hash code for a
160      * {@link CurrentAction} object is hash code of its name.
161      * 
162      * @return a hash code value for this {@link CurrentAction}.
163      */
164     @Override
165     public int hashCode() {
166         return this.name.hashCode();
167     }
168 
169     /**
170      * Compares this {@link CurrentAction} to the specified {@link Object}. The
171      * result is <code>true</code> if and only if the argument is not
172      * <code>null</code> and is {@link CurrentAction} object that has same name,
173      * requirement and processors as this {@link CurrentAction}.
174      * 
175      * @param obj the {@link Object} to compare this {@link CurrentAction}
176      *        against.
177      * @return <code>true</code> if {@link CurrentAction}s are equal;
178      *         <code>false</code> otherwise.
179      */
180     @Override
181     public boolean equals(final Object obj) {
182         boolean result = false;
183 
184         if (null != obj) {
185             if (this == obj) {
186                 result = true;
187             } else if (obj instanceof CurrentAction) {
188                 final CurrentAction other = (CurrentAction) obj;
189 
190                 result = this.name.equals(other.getName())
191                         && (this.required == other.required)
192                         && (this.processedBy.equals(other.processedBy));
193             }
194         }
195 
196         return result;
197     }
198 
199     private static String getName(final UnprocessedAction action) {
200         if (null == action) {
201             throw new NullPointerException("Unprocessed action cannot be null.");
202         }
203 
204         return action.getName();
205     }
206 }