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 org.apache.log4j.Logger;
22  
23  /**
24   * Class which defines an action that will be executed on an event. This class
25   * has one-to-many association with the {@link BaseEvent}.This class is not
26   * thread safe.
27   * 
28   * @author sanjin
29   */
30  public class UnprocessedAction implements ContextEventAction {
31  
32      /**
33       * Default requirement of {@link UnprocessedAction}.
34       */
35      public static final boolean REQUIREMENT_DEFAULT = DefaultAction.REQUIREMENT_DEFAULT;
36  
37      private static final Logger LOGGER = Logger.getLogger(UnprocessedAction.class);
38  
39      private final String name;
40      private boolean required;
41  
42      /**
43       * Create instance of {@link UnprocessedAction} with specified name and
44       * requirement.
45       * 
46       * @param name the name of the {@link UnprocessedAction}.
47       * @param required if the {@link UnprocessedAction} 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 UnprocessedAction(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 unprocessed action ''{0}''.", this.name));
69          }
70      }
71  
72      /**
73       * Create instance of {@link UnprocessedAction} with specified name and
74       * requirement set to {@link #REQUIREMENT_DEFAULT}. Name cannot be
75       * <code>null</code> or an empty or blank string.
76       * 
77       * @param name the name of the {@link UnprocessedAction}.
78       * @throws NullPointerException if specified name is <code>null</code>.
79       * @throws IllegalArgumentException if specified name is an empty or blank
80       *         string.
81       */
82      public UnprocessedAction(final String name) {
83          this(name, REQUIREMENT_DEFAULT);
84      }
85  
86      /**
87       * Create instance of {@link UnprocessedAction} from specified
88       * {@link DefaultAction}. Name and required properties of the
89       * {@link DefaultAction} are copied.
90       * 
91       * @param action the {@link DefaultAction}.
92       * @throws NullPointerException if specified {@link DefaultAction} is
93       *         <code>null</code>.
94       */
95      public UnprocessedAction(final DefaultAction action) {
96          this(getName(action), action.isRequired());
97      }
98  
99      @Override
100     public String getName() {
101         return this.name;
102     }
103 
104     /**
105      * @param required if the {@link UnprocessedAction} should be required.
106      */
107     public void setRequired(final boolean required) {
108         this.required = required;
109     }
110 
111     /**
112      * @return if the {@link UnprocessedAction} is required.
113      */
114     public boolean isRequired() {
115         return this.required;
116     }
117 
118     /**
119      * Returns hash code for this {@link UnprocessedAction}. The hash code for a
120      * {@link UnprocessedAction} object is hash code of its name.
121      * 
122      * @return a hash code value for this {@link UnprocessedAction}.
123      */
124     @Override
125     public int hashCode() {
126         return this.name.hashCode();
127     }
128 
129     /**
130      * Compares this {@link UnprocessedAction} to the specified {@link Object}.
131      * The result is <code>true</code> if and only if the argument is not
132      * <code>null</code> and is {@link UnprocessedAction} object that has same
133      * name and requirement as this {@link UnprocessedAction}.
134      * 
135      * @param obj the {@link Object} to compare this {@link UnprocessedAction}
136      *        against.
137      * @return <code>true</code> if {@link UnprocessedAction}s are equal;
138      *         <code>false</code> otherwise.
139      */
140     @Override
141     public boolean equals(final Object obj) {
142         boolean result = false;
143 
144         if (null != obj) {
145             if (this == obj) {
146                 result = true;
147             } else if (obj instanceof UnprocessedAction) {
148                 final UnprocessedAction other = (UnprocessedAction) obj;
149 
150                 result = this.name.equals(other.getName())
151                         && (this.required == other.required);
152             }
153         }
154 
155         return result;
156     }
157 
158     private static String getName(final DefaultAction action) {
159         if (null == action) {
160             throw new NullPointerException("Default action cannot be null.");
161         }
162 
163         return action.getName();
164     }
165 }