View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.core.entity;
24  
25  import java.util.HashSet;
26  import java.util.Iterator;
27  
28  /***
29   * Extends HashSet to add some handy methods for Agents 
30   * 
31   * @author turadg
32   *
33   */
34  public class AgentSet<E> extends HashSet {
35  
36  	/***
37  	 * 
38  	 */
39  	private static final long serialVersionUID = 8185517197325172183L;
40  
41  	/***
42  	 * Assumes there is only one agent in the set
43  	 * @throws MismatchedAgentSetSizeException 
44  	 */
45  	public IAgent getSingle() throws MismatchedAgentSetSizeException {
46  		// TODO make a checked exception for this
47  		if (size() > 1)
48  			throw new MismatchedAgentSetSizeException(1, size());
49  		if (isEmpty())
50  			throw new MismatchedAgentSetSizeException(1, 0);
51  		// otherwise, length is 1
52  		return (IAgent) iterator().next();
53  	}
54  
55  	public AgentSet select(Role role) {
56  		AgentSet<E> subset = new AgentSet<E>();
57  		for (Iterator iter = iterator(); iter.hasNext();) {
58  			IAgent agent = (IAgent) iter.next();
59  			if (agent.getRole().equals(role))
60  				subset.add(agent);
61  		}
62  		return subset;
63  	}
64  }