1 package org.apache.turbine.util.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.Serializable;
20
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.TreeMap;
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üttel</a>
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @version $Id: SecuritySet.java 264148 2005-08-29 14:21:04Z henning $
41 */
42 public abstract class SecuritySet
43 implements Serializable
44 {
45 /*** Map for "name" -> "security object" */
46 protected Map nameMap = null;
47
48 /*** Map for "id" -> "security object" */
49 protected Map idMap = null;
50
51 /***
52 * Constructs an empty Set
53 */
54 public SecuritySet()
55 {
56 nameMap = new TreeMap();
57 idMap = new TreeMap();
58 }
59
60 /***
61 * Returns a set of security objects in this object.
62 *
63 * @return A Set Object
64 *
65 */
66 public Set getSet()
67 {
68 return new HashSet(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 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 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 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 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(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 iterator()
148 {
149 return nameMap.values().iterator();
150 }
151
152 /***
153 * @deprecated Use iterator() instead.
154 */
155 public Iterator 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 toString()
176 {
177 StringBuffer sbuf = new StringBuffer(12 * size());
178 for(Iterator it = nameMap.keySet().iterator(); it.hasNext(); )
179 {
180 sbuf.append((String) it.next());
181
182 if(it.hasNext())
183 {
184 sbuf.append(", ");
185 }
186 }
187 return sbuf.toString();
188 }
189 }
190