Ignore:
Timestamp:
08/14/10 15:10:12 (22 months ago)
Author:
kasper
Message:

Made it possible to configure jobs using "configurable bean" acting as a prototype for actual executed beans

File:
1 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/util/CollectionUtils.java

    r962 r976  
    11package org.eobjects.analyzer.util; 
    22 
     3import java.lang.reflect.Array; 
    34import java.util.ArrayList; 
    45import java.util.HashSet; 
     
    1112                // prevent instantiation 
    1213        } 
    13          
     14 
    1415        public static <T> List<T> list(T... elements) { 
    1516                return list(ArrayList.class, elements); 
     
    2930                } 
    3031        } 
    31          
     32 
    3233        public static <T> Set<T> set(T... elements) { 
    3334                return set(HashSet.class, elements); 
     
    4647                } 
    4748        } 
     49 
     50        @SuppressWarnings("unchecked") 
     51        public static <E> E[] array(E[] existingArray, E... elements) { 
     52                if (existingArray == null) { 
     53                        return elements; 
     54                } 
     55                Object result = Array.newInstance(elements.getClass() 
     56                                .getComponentType(), existingArray.length + elements.length); 
     57                System.arraycopy(existingArray, 0, result, 0, existingArray.length); 
     58                System.arraycopy(elements, 0, result, existingArray.length, 
     59                                elements.length); 
     60                return (E[]) result; 
     61        } 
     62 
     63        @SuppressWarnings("unchecked") 
     64        public static <E> E[] array(Class<E> elementClass, Object existingArray, E... elements) { 
     65                if (existingArray == null) { 
     66                        return elements; 
     67                } 
     68                E[] result; 
     69                if (existingArray.getClass().isArray()) { 
     70                        int length = Array.getLength(existingArray); 
     71                        result = (E[]) Array.newInstance(elementClass, length + elements.length); 
     72                        System.arraycopy(existingArray, 0, result, 0, length); 
     73                        System.arraycopy(elements, 0, result, length, elements.length); 
     74                } else { 
     75                        result = (E[]) Array.newInstance(elementClass, 1 + elements.length); 
     76                        result[0] = (E) existingArray; 
     77                        System.arraycopy(elements, 0, result, 1, elements.length); 
     78                } 
     79 
     80                return result; 
     81        } 
     82 
     83        public static <E> E[] arrayRemove(E[] array, E elementToRemove) { 
     84                boolean found = false; 
     85                @SuppressWarnings("unchecked") 
     86                E[] result = (E[]) Array.newInstance(array.getClass() 
     87                                .getComponentType(), array.length - 1); 
     88                int nextIndex = 0; 
     89                for (E e : array) { 
     90                        if (e == elementToRemove) { 
     91                                found = true; 
     92                        } else { 
     93                                result[nextIndex] = e; 
     94                                nextIndex++; 
     95                        } 
     96                } 
     97                if (!found) { 
     98                        return array; 
     99                } 
     100                return result; 
     101        } 
     102 
     103        public static Object arrayRemove(Object array, Object elementToRemove) { 
     104                boolean found = false; 
     105                int oldLength = Array.getLength(array); 
     106                Object result = Array.newInstance(array.getClass().getComponentType(), 
     107                                oldLength - 1); 
     108                int nextIndex = 0; 
     109                for (int i = 0; i < oldLength; i++) { 
     110                        Object e = Array.get(array, i); 
     111                        if (e == elementToRemove) { 
     112                                found = true; 
     113                        } else { 
     114                                Array.set(result, nextIndex, e); 
     115                                nextIndex++; 
     116                        } 
     117                } 
     118                if (!found) { 
     119                        return array; 
     120                } 
     121                return result; 
     122        } 
     123 
     124        @SuppressWarnings("unchecked") 
     125        public static <E> E[] arrayOf(Class<E> elementClass, Object arrayOrElement) { 
     126                if (arrayOrElement == null) { 
     127                        return null; 
     128                } 
     129                if (arrayOrElement.getClass().isArray()) { 
     130                        return (E[]) arrayOrElement; 
     131                } 
     132                Object result = Array.newInstance(elementClass, 1); 
     133                Array.set(result, 0, arrayOrElement); 
     134                return (E[]) result; 
     135        } 
    48136} 
Note: See TracChangeset for help on using the changeset viewer.