View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.domain.util;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.SortedMap;
26  import java.util.SortedSet;
27  import java.util.TreeMap;
28  import java.util.TreeSet;
29  
30  
31  /***
32   * Some utility methods to use <code>Collection</code>s.
33   *
34   * @author Carlos Sanchez
35   * @version $Id: CollectionUtils.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
36   */
37  public class CollectionUtils {
38      //~ Methods ================================================================
39  
40      public static boolean isCollection(Class theClass) {
41          return Collection.class.isAssignableFrom(theClass);
42      }
43  
44      public static boolean isMap(Class theClass) {
45          return Map.class.isAssignableFrom(theClass);
46      }
47  
48      /***
49       * Add an object to a <code>Set</code> and return the result.
50       *
51       * @param set
52       * @param object
53       *
54       * @return
55       */
56      public static <E> Set<E> add(Set<E> set, E object) {
57          set.add(object);
58  
59          return set;
60      }
61  
62      /***
63       * Add an object to a <code>List</code> and return the result.
64       *
65       * @param list
66       * @param object
67       *
68       * @return
69       */
70      public static <E> List<E> add(List<E> list, E object) {
71          list.add(object);
72  
73          return list;
74      }
75  
76      /***
77       * Clone a Collection copying all its elements to a new one. If map is
78       * <code>null</code> return <code>null</code>.
79       *
80       * @param collection
81       *
82       * @return
83       *
84       * @throws IllegalArgumentException DOCUMENT ME!
85       */
86      public static <E> Collection<E> clone(Collection<E> collection) {
87          if (collection == null) {
88              return null;
89          }
90  
91          Class clazz = collection.getClass();
92          Collection<E> clone = null;
93  
94          if (List.class.isAssignableFrom(clazz)) {
95              clone = new ArrayList<E>(collection);
96          } else if (SortedSet.class.isAssignableFrom(clazz)) {
97              clone = new TreeSet<E>(collection);
98          } else if (Set.class.isAssignableFrom(clazz)) {
99              clone = new HashSet<E>(collection);
100         } else {
101             throw new IllegalArgumentException("Unknown collection class: "
102                 + clazz);
103         }
104 
105         return clone;
106     }
107 
108     /***
109      * Clone a <code>Map</code> copying all its elements to a new one. If the
110      * passed argument is <code>null</code>, the method will return
111      * <code>null</code>.
112      *
113      * @param map to copy
114      *
115      * @return a copy of the <code>Map</code> passed as an argument
116      *
117      * @throws IllegalArgumentException if the <code>Map</code> implementation
118      *         is not supported by this method
119      */
120     public static <K,V> Map<K,V> clone(Map<K,V> map) {
121         if (map == null) {
122             return null;
123         }
124 
125         Class clazz = map.getClass();
126         Map<K,V> clone = null;
127 
128         if (SortedMap.class.isAssignableFrom(clazz)) {
129             clone = new TreeMap<K,V>(map);
130         } else if (Map.class.isAssignableFrom(clazz)) {
131             clone = new HashMap<K,V>(map);
132         } else {
133             throw new IllegalArgumentException("Unknown map class: " + clazz);
134         }
135 
136         return clone;
137     }
138 
139     /***
140      * Return a <code>List</code> (actually an {@link ArrayList}) with only
141      * that object.
142      *
143      * @param object
144      *
145      * @return
146      */
147     public static <E> List<E> newList(E object) {
148         return add(new ArrayList<E>(1), object);
149     }
150 
151     /***
152      * Return a <code>Set</code> (actually a {@link HashSet}) with only that
153      * object.
154      *
155      * @param object
156      *
157      * @return
158      */
159     public static <E> Set<E> newSet(E object) {
160         return add(new HashSet<E>(), object);
161     }
162 }