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.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  
90          this.actions.add(index, action);
91      }
92  
93      /**
94       * Returns {@link ContextEventAction} at specified index.
95       * 
96       * @param index the index of the {@link ContextEventAction} to return.
97       * @return the {@link ContextEventAction}.
98       * @throws IllegalArgumentException if specified index has negative value or
99       *         is greater or equal than {@link #numberOfActions()}.
100      */
101     public S getAction(final int index) {
102         if (index < 0)
103             throw new IllegalArgumentException("Index cannot be negative.");
104 
105         return this.actions.get(index);
106     }
107 
108     /**
109      * Removes an {@link ContextEventAction} at specified position.
110      * 
111      * @param index the position of the {@link ContextEventAction} to remove.
112      * @throws IllegalArgumentException if specified index has negative value or
113      *         is greater or equal than {@link #numberOfActions()}.
114      */
115     public void removeAction(final int index) {
116         this.actions.remove(index);
117     }
118 
119     /**
120      * Returns the number of {@link ContextEventAction}.
121      * 
122      * @return the number of {@link ContextEventAction}.
123      */
124     public int numberOfActions() {
125         return this.actions.size();
126     }
127 
128     /**
129      * If there is at least one {@link ContextEventAction} with specified name.
130      * 
131      * @param name the name of the {@link ContextEventAction}.
132      * @return <code>true</code> if there is at least one
133      *         {@link ContextEventAction} with specified name;
134      *         <code>false</code> otherwise.
135      */
136     public boolean hasAction(final String name) {
137         boolean result = false;
138 
139         final Iterator<S> iter = this.actions.iterator();
140         while (iter.hasNext()) {
141             final S action = iter.next();
142             if (name.equals(action.getName())) {
143                 result = true;
144                 break;
145             }
146         }
147 
148         return result;
149     }
150 
151     /**
152      * Returns the array of current {@link ContextEventAction}s.
153      * 
154      * @return the array of current {@link ContextEventAction}s.
155      */
156     public ContextEventAction[] getActions() {
157         final ContextEventAction[] result;
158 
159         if (this.actions.isEmpty())
160             result = EMPTY_ACTION_ARRAY;
161         else
162             result = this.actions.toArray(new ContextEventAction[this.actions.size()]);
163 
164         return result;
165     }
166 
167     /**
168      * Adds specified {@link ContextEventAction}s and removes all previous
169      * actions.
170      * 
171      * @param actions the array of {@link ContextEventAction}s.
172      * @throws NullPointerException if any {@link ContextEventAction} is
173      *         <code>null</code>.
174      */
175     protected void setActions(final S... actions) {
176         if (actions == null)
177             throw new NullPointerException("Actions cannot be null.");
178         for (final S action : actions)
179             if (action == null)
180                 throw new NullPointerException("Action cannot be null.");
181 
182         this.actions.clear();
183         for (final S action : actions)
184             this.actions.add(action);
185     }
186 
187     /**
188      * Sets specified {@link ContextEventAction} at specified index.
189      * 
190      * @param index the index at which to set the {@link ContextEventAction}.
191      * @param action the {@link ContextEventAction}.
192      * @throws NullPointerException if specified {@link ContextEventAction} is
193      *         <code>null</code>.
194      * @throws IllegalArgumentException if specified index has negative value or
195      *         is greater or equal than {@link #numberOfActions()}.
196      */
197     protected void setAction(final int index, final S action) {
198         if (action == null)
199             throw new NullPointerException("Action cannot be null.");
200         if (index < 0)
201             throw new IllegalArgumentException("Index cannot be negative.");
202 
203         this.actions.remove(index);
204         this.actions.add(index, action);
205     }
206 
207     /**
208      * Removes all {@link ContextEventAction}s.
209      */
210     protected void removeActions() {
211         this.actions.clear();
212     }
213 }