protected Iterator<String> keysImpl() { @SuppressWarnings("unchecked") Enumeration<String> headerNames = request.getHeaderNames(); return headerNames == null ? null : CollectionUtil.iterator(headerNames); } }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static <T> T[] subArray(T[] pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Merges two arrays into a new array. Elements from array1 and array2 will * be copied into a new array, that has array1.length + array2.length * elements. * * @param pArray1 First array * @param pArray2 Second array, must be compatible with (assignable from) * the first array * @return A new array, containing the values of array1 and array2. The * array (wrapped as an object), will have the length of array1 + * array2, and can be safely cast to the type of the array1 * parameter. * @see #mergeArrays(Object,int,int,Object,int,int) * @see java.lang.System#arraycopy(Object,int,Object,int,int) */ public static Object mergeArrays(Object pArray1, Object pArray2) { return mergeArrays(pArray1, 0, Array.getLength(pArray1), pArray2, 0, Array.getLength(pArray2)); }
Set set = new HashSet(list); List<String> strs0 = CollectionUtil.generify(list, String.class); List<Object> objs0 = CollectionUtil.generify(list, String.class); List<String> strs1 = CollectionUtil.generify(set, String.class); // Not ok, runtime CCE unless set is null ArrayList<String> strs01 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null Set<String> setstr1 = CollectionUtil.generify(set, String.class); Set<Object> setobj1 = CollectionUtil.generify(set, String.class); try { Set<Object> setobj44 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null List<String> strs2 = CollectionUtil.<List<String>, String>generify2(list); List<Object> objs2 = CollectionUtil.<List<Object>, String>generify2(list); List<String> strs3 = CollectionUtil.<List<String>, String>generify2(set); // Not ok, runtime CCE unless set is null
private static List<String> toList(final Iterator<String> pValues) { List<String> list = new ArrayList<String>(); CollectionUtil.addAll(list, pValues); return Collections.unmodifiableList(list); }
/** * Creates an inverted mapping of the key/value pairs in the given map. * * @param pSource the source map * @return a new {@code Map} of same type as {@code pSource} * @throws IllegalArgumentException if {@code pSource == null}, * or if a new map can't be instantiated, * or if source map contains duplicates. * * @see #invert(java.util.Map, java.util.Map, DuplicateHandler) */ public static <K, V> Map<V, K> invert(Map<K, V> pSource) { return invert(pSource, null, null); }
Set set = new HashSet(list); List<String> strs0 = CollectionUtil.generify(list, String.class); List<Object> objs0 = CollectionUtil.generify(list, String.class); List<String> strs1 = CollectionUtil.generify(set, String.class); // Not ok, runtime CCE unless set is null ArrayList<String> strs01 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null Set<String> setstr1 = CollectionUtil.generify(set, String.class); Set<Object> setobj1 = CollectionUtil.generify(set, String.class); try { Set<Object> setobj44 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null List<String> strs2 = CollectionUtil.<List<String>, String>generify2(list); List<Object> objs2 = CollectionUtil.<List<Object>, String>generify2(list); List<String> strs3 = CollectionUtil.<List<String>, String>generify2(set); // Not ok, runtime CCE unless set is null
@Test public void testProviders() { // Providers Iterator<DummySPI> providers = registry.providers(DummySPI.class); List<DummySPI> providerList = new ArrayList<DummySPI>(); CollectionUtil.addAll(providerList, providers); assertEquals(2, providerList.size()); // Order should be as in configuration file assertNotNull(providerList.get(0)); assertEquals(DummySPIImpl.class, providerList.get(0).getClass()); assertNotNull(providerList.get(1)); assertEquals(DummySPIToo.class, providerList.get(1).getClass()); }
/** * Creates an inverted mapping of the key/value pairs in the given map. * * @param pSource the source map * @return a new {@code Map} of same type as {@code pSource} * @throws IllegalArgumentException if {@code pSource == null}, * or if a new map can't be instantiated, * or if source map contains duplaicates. * * @see #invert(java.util.Map, java.util.Map, DuplicateHandler) */ public static <K, V> Map<V, K> invert(Map<K, V> pSource) { return invert(pSource, null, null); }
protected List<String> valueImpl(final String pName) { @SuppressWarnings("unchecked") Enumeration<String> headers = request.getHeaders(pName); return headers == null ? null : toList(CollectionUtil.iterator(headers)); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} or * if {@code pArray} is not an array. * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static Object subArray(Object pArray, int pStart) { return subArray(pArray, pStart, -1); }
@Test(expected = ArrayStoreException.class) public void testMergeArraysObjectIllegalType() { String[] strings = {"foo", "bar", "baz"}; Integer[] integers = {1, 2, 3}; // Integer not assignable to String CollectionUtil.mergeArrays(strings, integers); }
Set set = new HashSet(list); List<String> strs0 = CollectionUtil.generify(list, String.class); List<Object> objs0 = CollectionUtil.generify(list, String.class); List<String> strs1 = CollectionUtil.generify(set, String.class); // Not ok, runtime CCE unless set is null ArrayList<String> strs01 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null Set<String> setstr1 = CollectionUtil.generify(set, String.class); Set<Object> setobj1 = CollectionUtil.generify(set, String.class); try { Set<Object> setobj44 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null List<String> strs2 = CollectionUtil.<List<String>, String>generify2(list); List<Object> objs2 = CollectionUtil.<List<Object>, String>generify2(list); List<String> strs3 = CollectionUtil.<List<String>, String>generify2(set); // Not ok, runtime CCE unless set is null
@Test public void testRegister() { // Register DummySPI dummy = new DummySPI() {}; assertTrue(registry.register(dummy)); // Should now have category Iterator<Class<?>> categories = registry.containingCategories(dummy); assertTrue(categories.hasNext()); assertEquals(DummySPI.class, categories.next()); assertFalse(categories.hasNext()); // Should now be in providers Iterator<DummySPI> providers = registry.providers(DummySPI.class); List<DummySPI> providerList = new ArrayList<DummySPI>(); CollectionUtil.addAll(providerList, providers); assertEquals(3, providerList.size()); assertNotNull(providerList.get(1)); assertSame(dummy, providerList.get(2)); }
/** * Creates an inverted mapping of the key/value pairs in the given map. * * @param pSource the source map * @return a new {@code Map} of same type as {@code pSource} * @throws IllegalArgumentException if {@code pSource == null}, * or if a new map can't be instantiated, * or if source map contains duplicates. * * @see #invert(java.util.Map, java.util.Map, DuplicateHandler) */ public static <K, V> Map<V, K> invert(Map<K, V> pSource) { return invert(pSource, null, null); }
protected Iterator<String> keysImpl() { @SuppressWarnings("unchecked") Enumeration<String> names = request.getParameterNames(); return names == null ? null : CollectionUtil.iterator(names); } }
/** * Creates an array containing a subset of the original array. * If the {@code pLength} parameter is negative, it will be ignored. * If there are not {@code pLength} elements in the original array * after {@code pStart}, the {@code pLength} parameter will be * ignored. * If the sub array is same length as the original, the original array will * be returned. * * @param pArray the original array * @param pStart the start index of the original array * @param pLength the length of the new array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0 and {@code pLength} is either * negative, or greater or equal to {@code pArray.length}. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ @SuppressWarnings("unchecked") public static <T> T[] subArray(T[] pArray, int pStart, int pLength) { return (T[]) subArray((Object) pArray, pStart, pLength); }
@Test(expected = ArrayStoreException.class) public void testMergeArraysNativeIllegalType() { char[] chars = {'a', 'b', 'c'}; int[] integers = {1, 2, 3}; // Integer not assignable to String CollectionUtil.mergeArrays(chars, integers); }
Set set = new HashSet(list); List<String> strs0 = CollectionUtil.generify(list, String.class); List<Object> objs0 = CollectionUtil.generify(list, String.class); List<String> strs1 = CollectionUtil.generify(set, String.class); // Not ok, runtime CCE unless set is null ArrayList<String> strs01 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null Set<String> setstr1 = CollectionUtil.generify(set, String.class); Set<Object> setobj1 = CollectionUtil.generify(set, String.class); try { Set<Object> setobj44 = CollectionUtil.generify(list, String.class); // Not ok, runtime CCE unless list is null List<String> strs2 = CollectionUtil.<List<String>, String>generify2(list); List<Object> objs2 = CollectionUtil.<List<Object>, String>generify2(list); List<String> strs3 = CollectionUtil.<List<String>, String>generify2(set); // Not ok, runtime CCE unless set is null
@Test public void testProviders() { // Providers Iterator<DummySPI> providers = registry.providers(DummySPI.class); List<DummySPI> providerList = new ArrayList<DummySPI>(); CollectionUtil.addAll(providerList, providers); assertEquals(2, providerList.size()); // Order should be as in configuration file assertNotNull(providerList.get(0)); assertEquals(DummySPIImpl.class, providerList.get(0).getClass()); assertNotNull(providerList.get(1)); assertEquals(DummySPIToo.class, providerList.get(1).getClass()); }