View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.internal.util;
14  
15  import java.lang.reflect.Method;
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.Collections;
19  import java.util.List;
20  
21  /**
22   * @author Sebastian Thomschke
23   */
24  public final class ArrayUtils
25  {
26  	public static final Method[] EMPTY_METHOD_ARRAY = {};
27  	public static final Object[] EMPTY_OBJECT_ARRAY = {};
28  	public static final String[] EMPTY_STRING_ARRAY = {};
29  
30  	/**
31  	 * @throws IllegalArgumentException if <code>collection == null</code>
32  	 */
33  	public static <T> int addAll(final Collection<T> collection, final T... elements) throws IllegalArgumentException
34  	{
35  		if (elements == null) return 0;
36  
37  		int count = 0;
38  		for (final T elem : elements)
39  			if (collection.add(elem)) count++;
40  		return count;
41  	}
42  
43  	public static List< ? > asList(final Object array)
44  	{
45  		if (array instanceof Object[])
46  		{
47  			final Object[] arrayCasted = (Object[]) array;
48  			final List<Object> result = new ArrayList<Object>(arrayCasted.length);
49  			Collections.addAll(result, arrayCasted);
50  			return result;
51  		}
52  		if (array instanceof byte[])
53  		{
54  			final byte[] arrayCasted = (byte[]) array;
55  			final List<Byte> result = new ArrayList<Byte>(arrayCasted.length);
56  			for (final byte i : arrayCasted)
57  				result.add(i);
58  			return result;
59  		}
60  		if (array instanceof char[])
61  		{
62  			final char[] arrayCasted = (char[]) array;
63  			final List<Character> result = new ArrayList<Character>(arrayCasted.length);
64  			for (final char i : arrayCasted)
65  				result.add(i);
66  			return result;
67  		}
68  		if (array instanceof short[])
69  		{
70  			final short[] arrayCasted = (short[]) array;
71  			final List<Short> result = new ArrayList<Short>(arrayCasted.length);
72  			for (final short i : arrayCasted)
73  				result.add(i);
74  			return result;
75  		}
76  		if (array instanceof int[])
77  		{
78  			final int[] arrayCasted = (int[]) array;
79  			final List<Integer> result = new ArrayList<Integer>(arrayCasted.length);
80  			for (final int i : arrayCasted)
81  				result.add(i);
82  			return result;
83  		}
84  		if (array instanceof long[])
85  		{
86  			final long[] arrayCasted = (long[]) array;
87  			final List<Long> result = new ArrayList<Long>(arrayCasted.length);
88  			for (final long i : arrayCasted)
89  				result.add(i);
90  			return result;
91  		}
92  		if (array instanceof double[])
93  		{
94  			final double[] arrayCasted = (double[]) array;
95  			final List<Double> result = new ArrayList<Double>(arrayCasted.length);
96  			for (final double i : arrayCasted)
97  				result.add(i);
98  			return result;
99  		}
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 }