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.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Class which defines a list of {@link ContextEventAction}s and makes possible
26   * to manipulate and retrieve {@link ContextEventAction}. This class is not
27   * thread safe.
28   * 
29   * @param <S> the subclass of {@link ContextEventAction} that is used for
30   *        storage and retrieval.
31   * @param <T> the subclass of <code>S</code> that is allowed to be appended and
32   *        inserted.
33   * @author sanjin
34   */
35  public class ActionList<S extends ContextEventAction, T extends S> {
36  
37      private static final ContextEventAction[] EMPTY_ACTION_ARRAY = new ContextEventAction[0];
38  
39      private final List<S> actions = new ArrayList<S>();
40  
41      /**
42       * Appends specified {@link ContextEventAction}.
43       * 
44       * @param action the {@link ContextEventAction}.
45       * @throws NullPointerException if specified {@link ContextEventAction} is
46       *         <code>null</code>.
47       */
48      public void appendAction(final T action) {
49          if (action == null)
50              throw new NullPointerException("Action cannot be null.");
51  
52          this.actions.add(action);
53      }
54  
55      /**
56       * Appends specified {@link ContextEventAction}s.
57       * 
58       * @param actions the {@link ContextEventAction}s.
59       * @throws NullPointerException if any {@link ContextEventAction} is
60       *         <code>null</code>.
61       */
62      public void appendActions(@SuppressWarnings("hiding") final T... actions) {
63          if (actions == null)
64              throw new NullPointerException("Actions cannot be null.");
65          for (final T action : actions)
66              if (action == null)
67                  throw new NullPointerException("Action cannot be null.");
68  
69          for (final T action : actions)
70              this.actions.add(action);
71      }
72  
73      /**
74       * Inserts specified {@link ContextEventAction} at specified index.
75       * 
76       * @param index the index at which the {@link ContextEventAction} is
77       *        inserted.
78       * @param action the {@link ContextEventAction}.
79       * @throws NullPointerException if specified {@link ContextEventAction} is
80       *         <code>null</code>.
81       * @throws IllegalArgumentException if specified index has negative value or
82       *         is greater or equal than {@link #numberOfActions()}.
83       */
84      public void insertAction(final int index, final T action) {
85          if (action == null)
86              throw new NullPointerException("Action cannot be null.");
87          if (index < 0)
88              throw new IllegalArgumentException("Index cannot be negative.");
89          if (index > numberOfActions())
90              throw new IllegalArgumentException(
91                      "Index cannot be greater than number of actions.");
92  
93          this.actions.add(index, action);
94      }
95  
96      /**
97       * Returns {@link ContextEventAction} at specified index.
98       * 
99       * @param index the index of the {@link ContextEventAction} to return.
100      * @return the {@link ContextEventAction}.
101      * @throws IllegalArgumentException if specified index has negative value or
102      *         is greater or equal than {@link #numberOfActions()}.
103      */
104     public S getAction(final int index) {
105         if (index < 0)
106             throw new IllegalArgumentException("Index cannot be negative.");
107         if (index > numberOfActions())
108             throw new IllegalArgumentException(
109                     "Index cannot be greater than number of actions.");
110 
111         return this.actions.get(index);
112     }
113 
114     /**
115      * Removes an {@link ContextEventAction} at specified position.
116      * 
117      * @param index the position of the {@link ContextEventAction} to remove.
118      * @throws IllegalArgumentException if specified index has negative value or
119      *         is greater or equal than {@link #numberOfActions()}.
120      */
121     public void removeAction(final int index) {
122         if (index < 0)
123             throw new IllegalArgumentException("Index cannot be negative.");
124         if (index > numberOfActions())
125             throw new IllegalArgumentException(
126                     "Index cannot be greater than number of actions.");
127 
128         this.actions.remove(index);
129     }
130 
131     /**
132      * Returns the number of {@link ContextEventAction}.
133      * 
134      * @return the number of {@link ContextEventAction}.
135      */
136     public int numberOfActions() {
137         return this.actions.size();
138     }
139 
140     /**
141      * If there is at least one {@link ContextEventAction} with specified name.
142      * 
143      * @param name the name of the {@link ContextEventAction}.
144      * @return <code>true</code> if there is at least one
145      *         {@link ContextEventAction} with specified name;
146      *         <code>false</code> otherwise.
147      */
148     public boolean hasAction(final String name) {
149         boolean result = false;
150 
151         final Iterator<S> iter = this.actions.iterator();
152         while (iter.hasNext()) {
153             final S action = iter.next();
154             if (name.equals(action.getName())) {
155                 result = true;
156                 break;
157             }
158         }
159 
160         return result;
161     }
162 
163     /**
164      * Returns the array of current {@link ContextEventAction}s.
165      * 
166      * @return the array of current {@link ContextEventAction}s.
167      */
168     public ContextEventAction[] getActions() {
169         final ContextEventAction[] result;
170 
171         if (this.actions.isEmpty())
172             result = EMPTY_ACTION_ARRAY;
173         else
174             result = this.actions.toArray(new ContextEventAction[this.actions.size()]);
175 
176         return result;
177     }
178 
179     /**
180      * Adds specified {@link ContextEventAction}s and removes all previous
181      * actions.
182      * 
183      * @param actions the array of {@link ContextEventAction}s.
184      * @throws NullPointerException if any {@link ContextEventAction} is
185      *         <code>null</code>.
186      */
187     protected void setActions(final S... actions) {
188         if (actions == null)
189             throw new NullPointerException("Actions cannot be null.");
190         for (final S action : actions)
191             if (action == null)
192                 throw new NullPointerException("Action cannot be null.");
193 
194         this.actions.clear();
195         for (final S action : actions)
196             this.actions.add(action);
197     }
198 
199     /**
200      * Sets specified {@link ContextEventAction} at specified index.
201      * 
202      * @param index the index at which to set the {@link ContextEventAction}.
203      * @param action the {@link ContextEventAction}.
204      * @throws NullPointerException if specified {@link ContextEventAction} is
205      *         <code>null</code>.
206      * @throws IllegalArgumentException if specified index has negative value or
207      *         is greater or equal than {@link #numberOfActions()}.
208      */
209     protected void setAction(final int index, final S action) {
210         if (action == null)
211             throw new NullPointerException("Action cannot be null.");
212         if (index < 0)
213             throw new IllegalArgumentException("Index cannot be negative.");
214         if (index > numberOfActions())
215             throw new IllegalArgumentException(
216                     "Index cannot be greater than number of actions.");
217 
218         this.actions.remove(index);
219         this.actions.add(index, action);
220     }
221 
222     /**
223      * Removes all {@link ContextEventAction}s.
224      */
225     protected void removeActions() {
226         this.actions.clear();
227     }
228 }