KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > util > GroupSet


1 package org.apache.fulcrum.security.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.Serializable JavaDoc;
58
59 import java.util.Collection JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.TreeSet JavaDoc;
62
63 import org.apache.fulcrum.security.entity.Group;
64
65 /**
66  * This class represents a set of Groups. It's useful for building
67  * administration UI. It wraps a TreeSet object to enforce that only
68  * Group objects are allowed in the set and only relevant methods
69  * are available. TreeSet's contain only unique Objects (no
70  * duplicates).
71  *
72  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
73  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
74  * @version $Id: GroupSet.java,v 1.1 2004/11/12 10:25:42 epugh Exp $
75  */

76 public class GroupSet implements Serializable JavaDoc
77 {
78     /** Set to hold the Group Set */
79     private TreeSet JavaDoc set;
80
81     /**
82      * Constructs an empty GroupSet
83      */

84     public GroupSet()
85     {
86         set = new TreeSet JavaDoc();
87     }
88
89     /**
90      * Constructs a new GroupSet with specifed contents.
91      *
92      * If the given collection contains multiple objects that are
93      * identical WRT equals() method, some objects will be overwriten.
94      *
95      * @param groups A collection of groups to be contained in the set.
96      */

97     public GroupSet(Collection JavaDoc groups)
98     {
99         this();
100         add(groups);
101     }
102
103     /**
104      * Adds a Group to this GroupSet.
105      *
106      * @param group A Group.
107      * @return True if Group was added; false if GroupSet
108      * already contained the Group.
109      */

110     public boolean add(Group group)
111     {
112         return set.add((Object JavaDoc) group);
113     }
114
115     /**
116      * Adds the Groups in a Collection to this GroupSet.
117      *
118      * @param groups A Collection of Groups.
119      *
120      * @return True if this GroupSet changed as a result; false
121      * if no change to this GroupSet occurred (this GroupSet
122      * already contained all members of the added GroupSet).
123      */

124     public boolean add(Collection JavaDoc groups)
125     {
126         return set.addAll(groups);
127     }
128
129     /**
130      * Adds the Groups in another GroupSet to this GroupSet.
131      *
132      * @param groupSet A GroupSet.
133      * @return True if this GroupSet changed as a result; false
134      * if no change to this GroupSet occurred (this GroupSet
135      * already contained all members of the added GroupSet).
136      */

137     public boolean add(GroupSet groupSet)
138     {
139         return set.addAll(groupSet.set);
140     }
141
142     /**
143      * Removes a Group from this GroupSet.
144      *
145      * @param group A Group.
146      * @return True if this GroupSet contained the Group
147      * before it was removed.
148      */

149     public boolean remove(Group group)
150     {
151         return set.remove((Object JavaDoc) group);
152     }
153
154     /**
155      * Removes all Groups from this GroupSet.
156      */

157     public void clear()
158     {
159         set.clear();
160     }
161
162     /**
163      * Checks whether this GroupSet contains a Group.
164      *
165      * @param group A Group.
166      * @return True if this GroupSet contains the Group,
167      * false otherwise.
168      */

169     public boolean contains(Group group)
170     {
171         return set.contains((Object JavaDoc) group);
172     }
173
174     /**
175      * Compares by name a Group with the Groups contained in
176      * this GroupSet.
177      *
178      * @param groupName Name of Group.
179      * @return True if argument matched a Group in this
180      * GroupSet; false if no match.
181      */

182     public boolean contains(String JavaDoc groupName)
183     {
184         Iterator JavaDoc iter = set.iterator();
185         while (iter.hasNext())
186         {
187             Group group = (Group) iter.next();
188             if (groupName != null &&
189                  groupName.equals(group.getName()))
190             {
191                 return true;
192             }
193         }
194         return false;
195     }
196
197     /**
198      * Returns a Group with the given name, if it is contained in
199      * this GroupSet.
200      *
201      * @param groupName Name of Group.
202      * @return Group if argument matched a Group in this
203      * GroupSet; null if no match.
204      */

205     public Group getGroup(String JavaDoc groupName)
206     {
207         Iterator JavaDoc iter = set.iterator();
208         while (iter.hasNext())
209         {
210             Group group = (Group) iter.next();
211             if (groupName != null &&
212                  groupName.equals(group.getName()))
213             {
214                 return group;
215             }
216         }
217         return null;
218     }
219
220     /**
221      * Returns an Groups [] of Groups in this GroupSet.
222      *
223      * @return A Group [].
224      */

225     public Group [] getGroupsArray()
226     {
227         return (Group []) set.toArray(new Group[0]);
228     }
229
230     /**
231      * Returns an Iterator for Groups in this GroupSet.
232      *
233      * @return An Iterator for the GroupSet.
234      */

235     public Iterator JavaDoc elements()
236     {
237         return set.iterator();
238     }
239
240     /**
241      * Returns size (cardinality) of this set.
242      *
243      * @return The cardinality of this GroupSet.
244      */

245     public int size()
246     {
247         return set.size();
248     }
249 }
250
Popular Tags