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;
19  
20  import java.text.MessageFormat;
21  import org.apache.log4j.Logger;
22  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventAction;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventType;
24  
25  /**
26   * Class which defines action which a {@link ContextProcessor} can process. The
27   * name and the event name of processor action uniquely defines the
28   * {@link ContextEventAction} and its associated {@link ContextEventType} using
29   * same values of {@link ContextEventAction#getName()} and
30   * {@link ContextEventType#getName()} respectively. Each action also defines the
31   * names of events returned by the processor after processing the action.
32   * 
33   * @author sanjin
34   */
35  public class ProcessorAction {
36  
37      private static final Logger LOGGER = Logger.getLogger(ProcessorAction.class);
38  
39      private final String name;
40      private final String eventType;
41      private final String resultType;
42  
43      /**
44       * Creates an instance of processor action. The <code>name</code> is the
45       * globally unique name of the {@link ContextEventAction} and cannot be
46       * <code>null</code>. The <code>eventType</code> is the globally unique name
47       * of the {@link ContextEventType} and also cannot be <code>null</code>. The
48       * <code>result</code> is globally unique name of the
49       * {@link ContextEventType} of event returned by the processor as result of
50       * processing this action. If <code>result</code> is <code>null</code> then
51       * processor will not produce a result.
52       * 
53       * @param name the globally unique name of the {@link ContextEventAction}.
54       * @param eventType the globally unique name of the {@link ContextEventType}
55       *        .
56       * @param resultType the globally unique name of the
57       *        {@link ContextEventType} for processor's result.
58       * @throws NullPointerException if specified name or event name is
59       *         <code>null</code>.
60       * @throws IllegalArgumentException if specified name or event name is an
61       *         empty or blank string.
62       */
63      public ProcessorAction(final String name, final String eventType,
64              final String resultType) {
65          super();
66  
67          if (name == null)
68              throw new NullPointerException("Actions's name cannot be null.");
69          if (eventType == null)
70              throw new NullPointerException(
71                      "Actions's event type cannot be null.");
72          if (name.trim().isEmpty())
73              throw new IllegalArgumentException(
74                      "Actions's name cannot be blank string.");
75          if (eventType.trim().isEmpty())
76              throw new IllegalArgumentException(
77                      "Actions's event type cannot be blank string.");
78  
79          this.name = name;
80          this.eventType = eventType;
81          this.resultType = resultType;
82  
83          if (LOGGER.isDebugEnabled())
84              LOGGER.debug(MessageFormat.format(
85                      "Created processor action ''{0}''.", this.name));
86      }
87  
88      /**
89       * Creates an instance of processor action without a result. The
90       * <code>name</code> is the globally unique name of the
91       * {@link ContextEventAction} and cannot be <code>null</code>. The
92       * <code>eventType</code> is the globally unique name of the
93       * {@link ContextEventType} and also cannot be <code>null</code>.
94       * 
95       * @param name the globally unique name of the {@link ContextEventAction}.
96       * @param eventType the globally unique name of the {@link ContextEventType}
97       *        .
98       * @throws NullPointerException if specified name or event name is
99       *         <code>null</code>.
100      * @throws IllegalArgumentException if specified name or event name is an
101      *         empty or blank string.
102      */
103     public ProcessorAction(final String name, final String eventType) {
104         this(name, eventType, null);
105     }
106 
107     /**
108      * @return the globally unique name of the {@link ContextEventAction}.
109      */
110     public String getName() {
111         return this.name;
112     }
113 
114     /**
115      * @return the globally unique name of the {@link ContextEventType}.
116      */
117     public String getEventType() {
118         return this.eventType;
119     }
120 
121     /**
122      * @return the name of {@link ContextEventType} for processor's result.
123      */
124     public String getResultType() {
125         return this.resultType;
126     }
127 
128     /**
129      * @return if this processor action produces a result.
130      */
131     public boolean hasResult() {
132         return this.resultType != null;
133     }
134 
135     /**
136      * Returns hash code for this processor action. The hash code for a
137      * <code>ProcessorAction</code> object is computed as:
138      * 
139      * <pre>
140      * name * 31 + event type
141      * </pre>
142      * 
143      * using integer arithmetic.
144      * 
145      * @return a hash code value for this processor action.
146      */
147     @Override
148     public int hashCode() {
149         return this.name.hashCode() * 31 + this.eventType.hashCode();
150     }
151 
152     /**
153      * Compares this processor action to the specified object. The result is
154      * <code>true</code> if and only if the argument is not <code>null</code>
155      * and is a <code>ProcessorAction</code> object that has same name and event
156      * name as this processor action.
157      * 
158      * @param obj the object to compare this processor action against.
159      * @return <code>true</code> if processor actions are equal;
160      *         <code>false</code> otherwise.
161      */
162     @Override
163     public boolean equals(final Object obj) {
164         boolean equal = false;
165 
166         if (obj != null)
167             if (this == obj)
168                 equal = true;
169             else if (getClass() == obj.getClass()) {
170                 final ProcessorAction other = (ProcessorAction) obj;
171 
172                 equal = this.name.equals(other.name)
173                         && this.eventType.equals(other.eventType);
174             }
175 
176         return equal;
177     }
178 }