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