View Javadoc

    1   package org.apache.turbine.util.security;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23  import java.io.Serializable;
    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 1073174 2011-02-21 22:18:45Z tv $
    44   */
    45  public abstract class SecuritySet<T>
    46          implements Serializable, Iterable<T>
    47  {
    48      /** Serial version */
    49  	private static final long serialVersionUID = -1315871299888969431L;
    50
    51  	/** Map for "name" -> "security object" */
    52      protected Map<String, T> nameMap = null;
    53
    54      /** Map for "id" -> "security object" */
    55      protected Map<Integer, T> idMap = null;
    56
    57      /**
    58       * Constructs an empty Set
    59       */
    60      public SecuritySet()
    61      {
    62          nameMap = new TreeMap<String, T>();
    63          idMap = new TreeMap<Integer, T>();
    64      }
    65
    66      /**
    67       * Returns a set of security objects in this object.
    68       *
    69       * @return A Set Object
    70       *
    71       */
    72      public Set<? extends T> getSet()
    73      {
    74          return new HashSet<T>(nameMap.values());
    75      }
    76
    77      /**
    78       * Returns a set of Names in this Object.
    79       *
    80       * @return The Set of Names in this Object,
    81       *         backed by the actual data.
    82       */
    83      public Set<String> getNames()
    84      {
    85          return nameMap.keySet();
    86      }
    87
    88      /**
    89       * Returns a set of Id values in this Object.
    90       *
    91       * @return The Set of Ids in this Object,
    92       *         backed by the actual data.
    93       */
    94      public Set<Integer> getIds()
    95      {
    96          return idMap.keySet();
    97      }
    98
    99      /**
    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