| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.util; |
| 17 | |
|
| 18 | |
import java.lang.reflect.Array; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.HashSet; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Set; |
| 24 | |
import java.util.SortedSet; |
| 25 | |
import java.util.TreeSet; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public final class CollectionUtils { |
| 35 | |
|
| 36 | 0 | private CollectionUtils() {} |
| 37 | |
|
| 38 | |
public static boolean isArray(Class<?> aClass) { |
| 39 | 175785 | return aClass.isArray(); |
| 40 | |
} |
| 41 | |
|
| 42 | |
public static boolean isCollection(Class<?> aClass) { |
| 43 | 175003 | return Collection.class.isAssignableFrom(aClass); |
| 44 | |
} |
| 45 | |
|
| 46 | |
public static boolean isList(Class<?> aClass) { |
| 47 | 1235 | return List.class.isAssignableFrom(aClass); |
| 48 | |
} |
| 49 | |
|
| 50 | |
public static boolean isSet(Class<?> aClass) { |
| 51 | 938 | return Set.class.isAssignableFrom(aClass); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public static boolean isPrimitiveArray(Class<?> aClass) { |
| 55 | 232 | return aClass.isArray() && aClass.getComponentType().isPrimitive(); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public static int getLengthOfCollection(Object value) { |
| 59 | 109 | if (isArray(value.getClass())) { |
| 60 | 19 | return Array.getLength(value); |
| 61 | |
} else { |
| 62 | 90 | return ((Collection<?>) value).size(); |
| 63 | |
} |
| 64 | |
} |
| 65 | |
|
| 66 | |
public static Object getValueFromCollection(Object collection, int index) { |
| 67 | 40 | if (isArray(collection.getClass())) { |
| 68 | 3 | return Array.get(collection, index); |
| 69 | |
} else { |
| 70 | 37 | return ((Collection<?>) collection).toArray()[index]; |
| 71 | |
} |
| 72 | |
} |
| 73 | |
|
| 74 | |
public static <T extends Set<?>> Set<?> createNewSet(Class<T> destType) { |
| 75 | |
Set<Object> result; |
| 76 | 55 | if (SortedSet.class.isAssignableFrom(destType)) { |
| 77 | 8 | result = new TreeSet<Object>(); |
| 78 | |
} else { |
| 79 | 47 | result = new HashSet<Object>(); |
| 80 | |
} |
| 81 | 55 | return result; |
| 82 | |
} |
| 83 | |
|
| 84 | |
public static <T extends Set<?>> Set<?> createNewSet(Class<T> destSetType, Collection<?> srcValue) { |
| 85 | 52 | Set<Object> result = (Set<Object>) createNewSet(destSetType); |
| 86 | 52 | if (srcValue != null) { |
| 87 | 52 | result.addAll(srcValue); |
| 88 | |
} |
| 89 | 52 | return result; |
| 90 | |
} |
| 91 | |
|
| 92 | |
public static <T> Object convertListToArray(List<T> list, Class<T> destEntryType) { |
| 93 | |
|
| 94 | 99 | Object outArray = Array.newInstance(destEntryType, list.size()); |
| 95 | 99 | int count = 0; |
| 96 | 99 | int size = list.size(); |
| 97 | 41318 | for (int i = 0; i < size; i++) { |
| 98 | 41219 | Object element = list.get(i); |
| 99 | 41219 | Array.set(outArray, count, element); |
| 100 | 41219 | count++; |
| 101 | |
} |
| 102 | 99 | if(destEntryType.isPrimitive()) |
| 103 | 2 | return outArray; |
| 104 | |
else |
| 105 | 97 | return (T[]) outArray; |
| 106 | |
} |
| 107 | |
|
| 108 | |
public static List<Object> convertPrimitiveArrayToList(Object primitiveArray) { |
| 109 | 9 | int length = Array.getLength(primitiveArray); |
| 110 | 9 | List<Object> result = new ArrayList<Object>(length); |
| 111 | |
|
| 112 | |
|
| 113 | 36 | for (int i = 0; i < length; i++) { |
| 114 | 27 | result.add(Array.get(primitiveArray, i)); |
| 115 | |
} |
| 116 | 9 | return result; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
public static <E> Set<E> intersection(final Set<E> set1, final Set<?> set2) { |
| 121 | 8647 | Set<E> intersection = new HashSet<E>(set1); |
| 122 | 8647 | intersection.retainAll(set2); |
| 123 | 8647 | return intersection; |
| 124 | |
} |
| 125 | |
} |