KickJava   Java API By Example, From Geeks To Geeks.

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


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.Role;
64
65 /**
66  * This class represents a set of Roles. It makes it easy to build a
67  * UI that would allow someone to add a group of Roles to a User. It
68  * wraps a TreeSet object to enforce that only Role objects are
69  * allowed in the set and only relevant methods are available.
70  * TreeSet's contain only unique Objects (no 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: RoleSet.java,v 1.1 2004/11/12 10:25:42 epugh Exp $
75  */

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

84     public RoleSet()
85     {
86         set = new TreeSet JavaDoc();
87     }
88
89     /**
90      * Constructs a new RoleSet 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 roles A collection of roles to be contained in the set.
96      */

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

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

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

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

149     public boolean remove(Role role)
150     {
151         return set.remove((Object JavaDoc) role);
152     }
153
154     /**
155      * Removes all Roles from this RoleSet.
156      */

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

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

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

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

225     public Role [] getRolesArray()
226     {
227         return (Role []) set.toArray(new Role[0]);
228     }
229
230     /**
231      * Returns an Iterator for Roles in this RoleSet.
232      *
233      * @return An iterator for the RoleSet
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 RoleSet.
244      */

245     public int size()
246     {
247         return set.size();
248     }
249
250     /**
251      * list of role names in this set
252      *
253      * @return The string representation of this RoleSet.
254      */

255     public String JavaDoc toString()
256     {
257         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(12 * size());
258         Iterator JavaDoc i = set.iterator();
259         while (i.hasNext())
260         {
261             sbuf.append(((Role) i.next()).getName())
262                 .append(", ");
263         }
264         return sbuf.toString();
265     }
266 }
267
Popular Tags