View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.common.model.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   * Some utility methods to use Collections
32   * 
33   * @author Carlos Sanchez
34   * @version $Revision: 1.4 $
35   */
36  public class CollectionUtils {
37  
38      /***
39       * Add an object to a Set and return the result.
40       * 
41       * @param set
42       * @param object
43       * @return
44       */
45      public static Set add(Set set, Object object) {
46          set.add(object);
47          return set;
48      }
49  
50      /***
51       * Add an object to a List and return the result.
52       * 
53       * @param set
54       * @param object
55       * @return
56       */
57      public static List add(List list, Object object) {
58          list.add(object);
59          return list;
60      }
61  
62      /***
63       * Return a Set (actually a HashSet) with only that object.
64       * 
65       * @param value
66       * @return
67       */
68      public static Set newSet(Object object) {
69          return add(new HashSet(), object);
70      }
71  
72      /***
73       * Return a List (actually an ArrayList) with only that object.
74       * 
75       * @param value
76       * @return
77       */
78      public static List newList(Object object) {
79          return add(new ArrayList(1), object);
80      }
81  
82      public static boolean isCollection(Class theClass) {
83          return Collection.class.isAssignableFrom(theClass);
84      }
85  
86      public static boolean isMap(Class theClass) {
87          return Map.class.isAssignableFrom(theClass);
88      }
89  
90      /***
91       * Clone a Collection copying all its elements to a new one. If map is
92       * <code>null</code> return <code>null</code>.
93       * 
94       * @param collection
95       * @return
96       */
97      public static Collection clone(Collection collection) {
98          if (collection == null)
99              return null;
100         Class clazz = collection.getClass();
101         Collection clone = null;
102         if (List.class.isAssignableFrom(clazz)) {
103             clone = new ArrayList(collection);
104         } else if (SortedSet.class.isAssignableFrom(clazz)) {
105             clone = new TreeSet(collection);
106         } else if (Set.class.isAssignableFrom(clazz)) {
107             clone = new HashSet(collection);
108         } else
109             throw new IllegalArgumentException("Unknown collection class: "
110                     + clazz);
111         return clone;
112     }
113 
114     /***
115      * Clone a Map copying all its elements to a new one. If map is
116      * <code>null</code> return <code>null</code>.
117      * 
118      * @param map
119      * @return
120      */
121     public static Map clone(Map map) {
122         if (map == null)
123             return null;
124         Class clazz = map.getClass();
125         Map clone = null;
126         if (SortedMap.class.isAssignableFrom(clazz)) {
127             clone = new TreeMap(map);
128         } else if (Map.class.isAssignableFrom(clazz)) {
129             clone = new HashMap(map);
130         } else
131             throw new IllegalArgumentException("Unknown map class: " + clazz);
132         return clone;
133     }
134 
135 }