KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > security > SecuritySet


1 package org.apache.turbine.util.security;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.Serializable JavaDoc;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeMap JavaDoc;
26
27 import org.apache.commons.lang.StringUtils;
28
29 /**
30  * This class represents a set of Security Entities.
31  * It makes it easy to build a UI.
32  * It wraps a TreeSet object to enforce that only relevant
33  * methods are available.
34  * TreeSet's contain only unique Objects (no duplicates).
35  *
36  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
37  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
38  * @author <a HREF="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
39  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40  * @version $Id: SecuritySet.java,v 1.3.2.2 2004/05/20 03:27:24 seade Exp $
41  */

42 public abstract class SecuritySet
43         implements Serializable JavaDoc
44 {
45     /** Map for "name" -> "security object" */
46     protected Map JavaDoc nameMap = null;
47
48     /** Map for "id" -> "security object" */
49     protected Map JavaDoc idMap = null;
50
51     /**
52      * Constructs an empty Set
53      */

54     public SecuritySet()
55     {
56         nameMap = new TreeMap JavaDoc();
57         idMap = new TreeMap JavaDoc();
58     }
59
60     /**
61      * Returns a set of security objects in this object.
62      *
63      * @return A Set Object
64      *
65      */

66     public Set JavaDoc getSet()
67     {
68         return new HashSet JavaDoc(nameMap.values());
69     }
70
71     /**
72      * Returns a set of Names in this Object.
73      *
74      * @return The Set of Names in this Object,
75      * backed by the actual data.
76      */

77     public Set JavaDoc getNames()
78     {
79         return nameMap.keySet();
80     }
81
82     /**
83      * Returns a set of Id values in this Object.
84      *
85      * @return The Set of Ids in this Object,
86      * backed by the actual data.
87      */

88     public Set JavaDoc getIds()
89     {
90         return idMap.keySet();
91     }
92
93     /**
94      * Removes all Objects from this Set.
95      */

96     public void clear()
97     {
98         nameMap.clear();
99         idMap.clear();
100     }
101
102     /**
103      * Searches if an Object with a given name is in the
104      * Set
105      *
106      * @param roleName Name of the Security Object.
107      * @return True if argument matched an Object in this Set; false
108      * if no match.
109      * @deprecated Use containsName(groupName) instead.
110      */

111     public boolean contains(String JavaDoc groupName)
112     {
113         return containsName(groupName);
114     }
115
116     /**
117      * Searches if an Object with a given name is in the
118      * Set
119      *
120      * @param roleName Name of the Security Object.
121      * @return True if argument matched an Object in this Set; false
122      * if no match.
123      */

124     public boolean containsName(String JavaDoc name)
125     {
126         return (StringUtils.isNotEmpty(name)) ? nameMap.containsKey(name) : false;
127     }
128
129     /**
130      * Searches if an Object with a given Id is in the
131      * Set
132      *
133      * @param id Id of the Security Object.
134      * @return True if argument matched an Object in this Set; false
135      * if no match.
136      */

137     public boolean containsId(int id)
138     {
139         return (id == 0) ? false: idMap.containsKey(new Integer JavaDoc(id));
140     }
141
142     /**
143      * Returns an Iterator for Objects in this Set.
144      *
145      * @return An iterator for the Set
146      */

147     public Iterator JavaDoc iterator()
148     {
149         return nameMap.values().iterator();
150     }
151
152     /**
153      * @deprecated Use iterator() instead.
154      */

155     public Iterator JavaDoc elements()
156     {
157         return iterator();
158     }
159
160     /**
161      * Returns size (cardinality) of this set.
162      *
163      * @return The cardinality of this Set.
164      */

165     public int size()
166     {
167         return nameMap.size();
168     }
169
170     /**
171      * list of role names in this set
172      *
173      * @return The string representation of this Set.
174      */

175     public String JavaDoc toString()
176     {
177         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(12 * size());
178         for(Iterator JavaDoc it = nameMap.keySet().iterator(); it.hasNext(); )
179         {
180             sbuf.append((String JavaDoc) it.next());
181
182             if(it.hasNext())
183             {
184                 sbuf.append(", ");
185             }
186         }
187         return sbuf.toString();
188     }
189 }
190
191
Popular Tags