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.ContextEvent;
23  
24  /**
25   * Class which defines type for each {@link ContextEvent}, i.e. each
26   * {@link ContextEvent} must have a corresponding {@link ContextEventType}
27   * associated with it. The name of the {@link ContextEventType} is also the name
28   * of each {@link ContextEvent} associated with this type and is a globally
29   * unique name on which {@link ContextPublisher}s and {@link ContextListener}s
30   * have to agree on and use it to publish and listen on events respectively.
31   * This class is not thread safe.
32   * 
33   * @author sanjin
34   */
35  public class ContextEventType extends ActionList<DefaultAction, DefaultAction> {
36  
37      private static final Logger LOGGER = Logger.getLogger(ContextEventType.class);
38  
39      private final String name;
40  
41      /**
42       * Creates an instance of {@link ContextEventType}. The <code>name</code>
43       * must be globally unique name of the event and cannot be <code>null</code>
44       * .
45       * 
46       * @param name the globally unique name of the {@link ContextEventType}.
47       * @throws NullPointerException if specified name <code>null</code>.
48       * @throws IllegalArgumentException if specified name is an empty or blank
49       *         string.
50       */
51      public ContextEventType(final String name) {
52          super();
53  
54          if (name == null)
55              throw new NullPointerException("Name of event type cannot be null.");
56          if (name.trim().isEmpty())
57              throw new IllegalArgumentException(
58                      "Name of event type cannot be an empty or blank string.");
59  
60          this.name = name;
61  
62          if (LOGGER.isDebugEnabled())
63              LOGGER.debug(MessageFormat.format("Created event type ''{0}''.",
64                      this.name));
65      }
66  
67      /**
68       * @return the globally unique name of the {@link ContextEventType}.
69       */
70      public String getName() {
71          return this.name;
72      }
73  
74      /**
75       * Replaces the action at specified index with specified
76       * {@link DefaultAction} in this list.
77       * 
78       * @param index the position at which the {@link DefaultAction} is replaced.
79       * @param action the {@link DefaultAction} to replace with at specified
80       *        index.
81       * @throws NullPointerException if specified {@link DefaultAction} is
82       *         <code>null</code>.
83       * @throws IllegalArgumentException if specified index has negative value or
84       *         is greater or equal than {@link #numberOfActions()}.
85       */
86      public void replaceAction(final int index, final DefaultAction action) {
87          super.setAction(index, action);
88      }
89  
90      @Override
91      public DefaultAction[] getActions() {
92          final ContextEventAction[] actions = super.getActions();
93          final DefaultAction[] result = new DefaultAction[actions.length];
94  
95          for (int i = 0; i < result.length; i++)
96              result[i] = (DefaultAction) actions[i];
97  
98          return result;
99      }
100 
101     /**
102      * Returns hash code for this {@link ContextEventType}. The hash code for a
103      * {@link ContextEventType} object is hash code of its name.
104      * 
105      * @return a hash code value for this {@link ContextEventType}.
106      */
107     @Override
108     public int hashCode() {
109         return this.name.hashCode();
110     }
111 
112     /**
113      * Compares this {@link ContextEventType} to the specified {@link Object}.
114      * The result is <code>true</code> if and only if the argument is not
115      * <code>null</code> and is a {@link ContextEventType} object that has same
116      * name as this {@link ContextEventType}.
117      * 
118      * @param obj the {@link Object} to compare this {@link ContextEventType}
119      *        against.
120      * @return <code>true</code> if {@link ContextEventType}s are equal;
121      *         <code>false</code> otherwise.
122      */
123     @Override
124     public boolean equals(final Object obj) {
125         boolean result = false;
126 
127         if (obj != null)
128             if (this == obj)
129                 result = true;
130             else if (this.getClass() == obj.getClass()) {
131                 final ContextEventType other = (ContextEventType) obj;
132 
133                 result = this.name.equals(other.name);
134             }
135 
136         return result;
137     }
138 }