View Javadoc

1   package org.apache.turbine.util.security;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.TreeMap;
29  
30  import org.apache.commons.lang.StringUtils;
31  
32  /***
33   * This class represents a set of Security Entities.
34   * It makes it easy to build a UI.
35   * It wraps a TreeSet object to enforce that only relevant
36   * methods are available.
37   * TreeSet's contain only unique Objects (no duplicates).
38   *
39   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
40   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
41   * @author <a href="mailto:marco@intermeta.de">Marco Kn&uuml;ttel</a>
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @version $Id: SecuritySet.java 534527 2007-05-02 16:10:59Z tv $
44   */
45  public abstract class SecuritySet
46          implements Serializable
47  {
48      /*** Map for "name" -> "security object" */
49      protected Map nameMap = null;
50  
51      /*** Map for "id" -> "security object" */
52      protected Map idMap = null;
53  
54      /***
55       * Constructs an empty Set
56       */
57      public SecuritySet()
58      {
59          nameMap = new TreeMap();
60          idMap = new TreeMap();
61      }
62  
63      /***
64       * Returns a set of security objects in this object.
65       *
66       * @return A Set Object
67       *
68       */
69      public Set getSet()
70      {
71          return new HashSet(nameMap.values());
72      }
73  
74      /***
75       * Returns a set of Names in this Object.
76       *
77       * @return The Set of Names in this Object,
78       *         backed by the actual data.
79       */
80      public Set getNames()
81      {
82          return nameMap.keySet();
83      }
84  
85      /***
86       * Returns a set of Id values in this Object.
87       *
88       * @return The Set of Ids in this Object,
89       *         backed by the actual data.
90       */
91      public Set getIds()
92      {
93          return idMap.keySet();
94      }
95  
96      /***
97       * Removes all Objects from this Set.
98       */
99      public void clear()
100     {
101         nameMap.clear();
102         idMap.clear();
103     }
104 
105     /***
106      * Searches if an Object with a given name is in the
107      * Set
108      *
109      * @param roleName Name of the Security Object.
110      * @return True if argument matched an Object in this Set; false
111      * if no match.
112      * @deprecated Use containsName(groupName) instead.
113      */
114     public boolean contains(String groupName)
115     {
116         return containsName(groupName);
117     }
118 
119     /***
120      * Searches if an Object with a given name is in the
121      * Set
122      *
123      * @param roleName Name of the Security Object.
124      * @return True if argument matched an Object in this Set; false
125      * if no match.
126      */
127     public boolean containsName(String name)
128     {
129         return (StringUtils.isNotEmpty(name)) ? nameMap.containsKey(name) : false;
130     }
131 
132     /***
133      * Searches if an Object with a given Id is in the
134      * Set
135      *
136      * @param id Id of the Security Object.
137      * @return True if argument matched an Object in this Set; false
138      * if no match.
139      */
140     public boolean containsId(int id)
141     {
142         return (id == 0) ? false:  idMap.containsKey(new Integer(id));
143     }
144 
145     /***
146      * Returns an Iterator for Objects in this Set.
147      *
148      * @return An iterator for the Set
149      */
150     public Iterator iterator()
151     {
152         return nameMap.values().iterator();
153     }
154 
155     /***
156      * @deprecated Use iterator() instead.
157      */
158     public Iterator elements()
159     {
160         return iterator();
161     }
162 
163     /***
164      * Returns size (cardinality) of this set.
165      *
166      * @return The cardinality of this Set.
167      */
168     public int size()
169     {
170         return nameMap.size();
171     }
172 
173     /***
174      * list of role names in this set
175      *
176      * @return The string representation of this Set.
177      */
178     public String toString()
179     {
180         StringBuffer sbuf = new StringBuffer(12 * size());
181         for(Iterator it = nameMap.keySet().iterator(); it.hasNext(); )
182         {
183             sbuf.append((String) it.next());
184 
185             if(it.hasNext())
186             {
187                 sbuf.append(", ");
188             }
189         }
190         return sbuf.toString();
191     }
192 }
193