001: package org.apache.turbine.util.security;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.Serializable;
020:
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025: import java.util.TreeMap;
026:
027: import org.apache.commons.lang.StringUtils;
028:
029: /**
030: * This class represents a set of Security Entities.
031: * It makes it easy to build a UI.
032: * It wraps a TreeSet object to enforce that only relevant
033: * methods are available.
034: * TreeSet's contain only unique Objects (no duplicates).
035: *
036: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
037: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
038: * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: SecuritySet.java 264148 2005-08-29 14:21:04Z henning $
041: */
042: public abstract class SecuritySet implements Serializable {
043: /** Map for "name" -> "security object" */
044: protected Map nameMap = null;
045:
046: /** Map for "id" -> "security object" */
047: protected Map idMap = null;
048:
049: /**
050: * Constructs an empty Set
051: */
052: public SecuritySet() {
053: nameMap = new TreeMap();
054: idMap = new TreeMap();
055: }
056:
057: /**
058: * Returns a set of security objects in this object.
059: *
060: * @return A Set Object
061: *
062: */
063: public Set getSet() {
064: return new HashSet(nameMap.values());
065: }
066:
067: /**
068: * Returns a set of Names in this Object.
069: *
070: * @return The Set of Names in this Object,
071: * backed by the actual data.
072: */
073: public Set getNames() {
074: return nameMap.keySet();
075: }
076:
077: /**
078: * Returns a set of Id values in this Object.
079: *
080: * @return The Set of Ids in this Object,
081: * backed by the actual data.
082: */
083: public Set getIds() {
084: return idMap.keySet();
085: }
086:
087: /**
088: * Removes all Objects from this Set.
089: */
090: public void clear() {
091: nameMap.clear();
092: idMap.clear();
093: }
094:
095: /**
096: * Searches if an Object with a given name is in the
097: * Set
098: *
099: * @param roleName Name of the Security Object.
100: * @return True if argument matched an Object in this Set; false
101: * if no match.
102: * @deprecated Use containsName(groupName) instead.
103: */
104: public boolean contains(String groupName) {
105: return containsName(groupName);
106: }
107:
108: /**
109: * Searches if an Object with a given name is in the
110: * Set
111: *
112: * @param roleName Name of the Security Object.
113: * @return True if argument matched an Object in this Set; false
114: * if no match.
115: */
116: public boolean containsName(String name) {
117: return (StringUtils.isNotEmpty(name)) ? nameMap
118: .containsKey(name) : false;
119: }
120:
121: /**
122: * Searches if an Object with a given Id is in the
123: * Set
124: *
125: * @param id Id of the Security Object.
126: * @return True if argument matched an Object in this Set; false
127: * if no match.
128: */
129: public boolean containsId(int id) {
130: return (id == 0) ? false : idMap.containsKey(new Integer(id));
131: }
132:
133: /**
134: * Returns an Iterator for Objects in this Set.
135: *
136: * @return An iterator for the Set
137: */
138: public Iterator iterator() {
139: return nameMap.values().iterator();
140: }
141:
142: /**
143: * @deprecated Use iterator() instead.
144: */
145: public Iterator elements() {
146: return iterator();
147: }
148:
149: /**
150: * Returns size (cardinality) of this set.
151: *
152: * @return The cardinality of this Set.
153: */
154: public int size() {
155: return nameMap.size();
156: }
157:
158: /**
159: * list of role names in this set
160: *
161: * @return The string representation of this Set.
162: */
163: public String toString() {
164: StringBuffer sbuf = new StringBuffer(12 * size());
165: for (Iterator it = nameMap.keySet().iterator(); it.hasNext();) {
166: sbuf.append((String) it.next());
167:
168: if (it.hasNext()) {
169: sbuf.append(", ");
170: }
171: }
172: return sbuf.toString();
173: }
174: }
|