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