KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > CollectionUtils


1 /*
2  * Copyright 2005-2007 the original author or authors.
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.dozer.util.mapping.util;
17
18 import java.lang.reflect.Array JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.SortedSet JavaDoc;
26 import java.util.TreeSet JavaDoc;
27
28 /**
29  * @author tierney.matt
30  * @author garsombke.franz
31  */

32 public class CollectionUtils {
33   
34   public boolean isArray(Class JavaDoc aClass) {
35     return aClass.isArray();
36   }
37
38   public boolean isCollection(Class JavaDoc aClass) {
39     return Collection JavaDoc.class.isAssignableFrom(aClass);
40   }
41
42   public boolean isIterator(Class JavaDoc aClass) {
43     return Iterator JavaDoc.class.isAssignableFrom(aClass);
44   }
45   
46   public boolean isList(Class JavaDoc aClass) {
47     return List JavaDoc.class.isAssignableFrom(aClass);
48   }
49
50   public boolean isSet(Class JavaDoc aClass) {
51     return Set JavaDoc.class.isAssignableFrom(aClass);
52   }
53   
54   public boolean isPrimitiveArray(Class JavaDoc aClass) {
55     return aClass.isArray() && aClass.getComponentType().isPrimitive();
56   }
57
58   public int getLengthOfCollection(Object JavaDoc value) {
59     if (isArray(value.getClass())) {
60       return Array.getLength(value);
61     } else {
62       return ((Collection JavaDoc) value).size();
63     }
64   }
65
66   public Object JavaDoc getValueFromCollection(Object JavaDoc collection, int index) {
67     if (isArray(collection.getClass())) {
68       return Array.get(collection, index);
69     } else
70     // is collection
71
{
72       Collection JavaDoc collectionTo = (Collection JavaDoc) collection;
73
74       return collectionTo.toArray()[index];
75     }
76   }
77   
78   public Set JavaDoc createNewSet(Class JavaDoc destType) {
79     return createNewSet(destType, null);
80   }
81
82   public Set JavaDoc createNewSet(Class JavaDoc destType, Collection JavaDoc srcValue) {
83     Set JavaDoc result = null;
84     if (SortedSet JavaDoc.class.isAssignableFrom(destType)) {
85       result = new TreeSet JavaDoc();
86     } else {
87       result = new HashSet JavaDoc();
88     }
89     if (srcValue != null) {
90       result.addAll(srcValue);
91     }
92     return result;
93   }
94   
95   public Object JavaDoc convertListToArray(List JavaDoc list, Class JavaDoc destEntryType) {
96     Object JavaDoc outArray = Array.newInstance(destEntryType, list.size());
97     int count = 0;
98     int size = list.size();
99     for (int i = 0; i < size; i++) {
100       Object JavaDoc element = list.get(i);
101       Array.set(outArray, count, element);
102       count++;
103     }
104     return outArray;
105   }
106   
107   public List JavaDoc convertPrimitiveArrayToList(Object JavaDoc primitiveArray) {
108     int length = Array.getLength(primitiveArray);
109     List JavaDoc result = new ArrayList JavaDoc(length);
110
111     // wrap and copy elements
112
for (int i = 0; i < length; i++) {
113         result.add(Array.get(primitiveArray, i));
114     }
115     return result;
116   }
117   
118 }
Popular Tags