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 a default action that can be executed on an event. This
25   * class has one-to-many association with the {@link ContextEventType}.
26   * 
27   * @author sanjin
28   */
29  public class DefaultAction implements ContextEventAction {
30  
31      /**
32       * Default requirement of {@link DefaultAction}.
33       */
34      public static final boolean REQUIREMENT_DEFAULT = true;
35  
36      private static final Logger LOGGER = Logger.getLogger(DefaultAction.class);
37  
38      private final String name;
39      private boolean required;
40  
41      /**
42       * Create instance of {@link DefaultAction} with specified name and
43       * requirement.
44       * 
45       * @param name the name of the {@link DefaultAction}.
46       * @param required if the {@link DefaultAction} should be required.
47       * @throws NullPointerException if specified name is <code>null</code>.
48       * @throws IllegalArgumentException if specified name is an empty or blank
49       *         string.
50       */
51      public DefaultAction(final String name, final boolean required) {
52          super();
53  
54          if (null == name) {
55              throw new NullPointerException("Name cannot be null.");
56          }
57          if (name.trim().isEmpty()) {
58              throw new IllegalArgumentException(
59                      "Name cannot be an empty or blank string.");
60          }
61  
62          this.name = name;
63          this.required = required;
64  
65          if (LOGGER.isDebugEnabled()) {
66              LOGGER.debug(MessageFormat.format(
67                      "Created default action ''{0}''.", this.name));
68          }
69      }
70  
71      /**
72       * Create instance of {@link DefaultAction} with specified name and
73       * requirement set to {@link #REQUIREMENT_DEFAULT}.
74       * 
75       * @param name the name of the {@link DefaultAction}.
76       * @throws NullPointerException if specified name is <code>null</code>.
77       * @throws IllegalArgumentException if specified name is an empty or blank
78       *         string.
79       */
80      public DefaultAction(final String name) {
81          this(name, REQUIREMENT_DEFAULT);
82      }
83  
84      @Override
85      public String getName() {
86          return this.name;
87      }
88  
89      /**
90       * @param required if the {@link DefaultAction} should be required.
91       */
92      public void setRequired(final boolean required) {
93          this.required = required;
94      }
95  
96      /**
97       * @return if the {@link DefaultAction} is required.
98       */
99      public boolean isRequired() {
100         return this.required;
101     }
102 
103     /**
104      * Returns hash code for this {@link DefaultAction}. The hash code for a
105      * {@link DefaultAction} object is hash code of its name.
106      * 
107      * @return a hash code value for this {@link DefaultAction}.
108      */
109     @Override
110     public int hashCode() {
111         return this.name.hashCode();
112     }
113 
114     /**
115      * Compares this {@link DefaultAction} to the specified {@link Object}. The
116      * result is <code>true</code> if and only if the argument is not
117      * <code>null</code> and is {@link DefaultAction} object that has same name
118      * and requirement as this {@link DefaultAction}.
119      * 
120      * @param obj the {@link Object} to compare this {@link DefaultAction}
121      *        against.
122      * @return <code>true</code> if {@link DefaultAction}s are equal;
123      *         <code>false</code> otherwise.
124      */
125     @Override
126     public boolean equals(final Object obj) {
127         boolean result = false;
128 
129         if (null != obj) {
130             if (this == obj) {
131                 result = true;
132             } else if (obj instanceof DefaultAction) {
133                 final DefaultAction other = (DefaultAction) obj;
134 
135                 result = ((this.name.equals(other.getName())) && (this.required == other.required));
136             }
137         }
138 
139         return result;
140     }
141 }