001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval.internal.util; 014 015 import java.lang.reflect.Method; 016 import java.util.ArrayList; 017 import java.util.Collection; 018 import java.util.Collections; 019 import java.util.List; 020 021 /** 022 * @author Sebastian Thomschke 023 */ 024 public final class ArrayUtils 025 { 026 public static final Method[] EMPTY_METHOD_ARRAY = {}; 027 public static final Object[] EMPTY_OBJECT_ARRAY = {}; 028 public static final String[] EMPTY_STRING_ARRAY = {}; 029 030 /** 031 * @throws IllegalArgumentException if <code>collection == null</code> 032 */ 033 public static <T> int addAll(final Collection<T> collection, final T... elements) throws IllegalArgumentException 034 { 035 if (elements == null) return 0; 036 037 int count = 0; 038 for (final T elem : elements) 039 if (collection.add(elem)) count++; 040 return count; 041 } 042 043 public static List< ? > asList(final Object array) 044 { 045 if (array instanceof Object[]) 046 { 047 final Object[] arrayCasted = (Object[]) array; 048 final List<Object> result = new ArrayList<Object>(arrayCasted.length); 049 Collections.addAll(result, arrayCasted); 050 return result; 051 } 052 if (array instanceof byte[]) 053 { 054 final byte[] arrayCasted = (byte[]) array; 055 final List<Byte> result = new ArrayList<Byte>(arrayCasted.length); 056 for (final byte i : arrayCasted) 057 result.add(i); 058 return result; 059 } 060 if (array instanceof char[]) 061 { 062 final char[] arrayCasted = (char[]) array; 063 final List<Character> result = new ArrayList<Character>(arrayCasted.length); 064 for (final char i : arrayCasted) 065 result.add(i); 066 return result; 067 } 068 if (array instanceof short[]) 069 { 070 final short[] arrayCasted = (short[]) array; 071 final List<Short> result = new ArrayList<Short>(arrayCasted.length); 072 for (final short i : arrayCasted) 073 result.add(i); 074 return result; 075 } 076 if (array instanceof int[]) 077 { 078 final int[] arrayCasted = (int[]) array; 079 final List<Integer> result = new ArrayList<Integer>(arrayCasted.length); 080 for (final int i : arrayCasted) 081 result.add(i); 082 return result; 083 } 084 if (array instanceof long[]) 085 { 086 final long[] arrayCasted = (long[]) array; 087 final List<Long> result = new ArrayList<Long>(arrayCasted.length); 088 for (final long i : arrayCasted) 089 result.add(i); 090 return result; 091 } 092 if (array instanceof double[]) 093 { 094 final double[] arrayCasted = (double[]) array; 095 final List<Double> result = new ArrayList<Double>(arrayCasted.length); 096 for (final double i : arrayCasted) 097 result.add(i); 098 return result; 099 } 100 if (array instanceof float[]) 101 { 102 final float[] arrayCasted = (float[]) array; 103 final List<Float> result = new ArrayList<Float>(arrayCasted.length); 104 for (final float i : arrayCasted) 105 result.add(i); 106 return result; 107 } 108 if (array instanceof boolean[]) 109 { 110 final boolean[] arrayCasted = (boolean[]) array; 111 final List<Boolean> result = new ArrayList<Boolean>(arrayCasted.length); 112 for (final boolean i : arrayCasted) 113 result.add(i); 114 return result; 115 } 116 117 throw new IllegalArgumentException("Argument [array] must be an array"); 118 } 119 120 public static <T> List<T> asList(final T[] array) 121 { 122 final List<T> result = new ArrayList<T>(array.length); 123 Collections.addAll(result, array); 124 return result; 125 } 126 127 public static <T> boolean containsEqual(final T[] theArray, final T theItem) 128 { 129 for (final T t : theArray) 130 { 131 if (t == theItem) return true; 132 if (t != null && t.equals(theItem)) return true; 133 } 134 return false; 135 } 136 137 public static <T> boolean containsSame(final T[] theArray, final T theItem) 138 { 139 for (final T t : theArray) 140 if (t == theItem) return true; 141 return false; 142 } 143 144 private ArrayUtils() 145 { 146 super(); 147 } 148 }