View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.lang.reflect.Array;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Enumeration;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * A class containing convenience methods for working with arrays.
31   * 
32   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
33   * @version $id$
34   */
35  public final class ArrayHelper {
36  
37  	/**
38  	 * Works on non sorted arrays
39  	 */
40  	public static int firstGreaterThan(double[] array, double key) {
41  		return firstGreaterThan(array, key, 0, array.length - 1);
42  	}
43  
44  	/**
45  	 * Works on non sorted arrays
46  	 */
47  	public static int firstGreaterThan(double[] array, double key,
48  			int rangeStart, int rangeEnd) {
49  		for (int i = rangeStart; i < rangeEnd + 1; i++) {
50  			if (array[i] > key)
51  				return i;
52  		}
53  		return -1;
54  	}
55  
56  	/**
57  	 * Works fast on sorted arrays
58  	 */
59  	public static int firstGreaterThanInSorted(double[] array, double key) {
60  		return firstGreaterThanInSorted(array, key, 0, array.length - 1);
61  	}
62  
63  	/**
64  	 * Works fast on sorted arrays
65  	 */
66  	public static int firstGreaterThanInSorted(double[] array, double key,
67  			int startIndex, int endIndex) {
68  		if (array.length < 1)
69  			return -1;
70  		if (array[startIndex] > key)
71  			return startIndex;
72  		if (endIndex - startIndex > 3) {
73  			int mid = (startIndex + endIndex) / 2;
74  			if (array[mid] <= key)
75  				return firstGreaterThanInSorted(array, key, mid + 1, endIndex);
76  			else
77  				return firstGreaterThanInSorted(array, key, startIndex, mid);
78  		} else
79  			return firstGreaterThan(array, key, startIndex, endIndex);
80  	}
81  
82  	public static int firstIndexOf(Object array, Object key) {
83  		for (int i = 0; i < Array.getLength(array); i++) {
84  			if (Array.get(array, i) != null && Array.get(array, i).equals(key))
85  				return i;
86  		}
87  		return -1;
88  	}
89  
90  	public static boolean contains(Object[] array, Object key) {
91  		return firstIndexOf(array, key) != -1;
92  	}
93  
94  	public static Object[] getArrayFromEnumeration(Enumeration e) {
95  		assert (e != null);
96  		ArrayList temp = new ArrayList();
97  		while (e.hasMoreElements()) {
98  			temp.add(e.nextElement());
99  		}
100 		Object[] retVal = new Object[temp.size()];
101 		temp.toArray(retVal);
102 		return retVal;
103 	}
104 
105 	public static Object[] getArrayFromIterator(Iterator i) {
106 		assert (i != null);
107 		ArrayList temp = new ArrayList();
108 		while (i.hasNext()) {
109 			temp.add(i.next());
110 		}
111 		Object[] retVal = new Object[temp.size()];
112 		temp.toArray(retVal);
113 		return retVal;
114 	}
115 
116 	public static void flip(Object array) {
117 		if (array == null)
118 			return;
119 		Object v = null;
120 		int size = Array.getLength(array);
121 		if (size == 1)
122 			return;
123 		for (int i = 0; i < size / 2; i++) {
124 			v = Array.get(array, i);
125 			Array.set(array, i, Array.get(array, size - i - 1));
126 			Array.set(array, size - i - 1, v);
127 		}
128 	}
129 
130 	public static double[] toDoubleArray(Object array) {
131 		if (array instanceof double[])
132 			return (double[]) array;
133 		else if (array instanceof long[]) {
134 			long[] lAr = (long[]) array;
135 			double[] ret = new double[lAr.length];
136 			for (int i = 0; i < lAr.length; i++) {
137 				ret[i] = lAr[i];
138 			}
139 			return ret;
140 		} else if (array instanceof float[]) {
141 			float[] lAr = (float[]) array;
142 			double[] ret = new double[lAr.length];
143 			for (int i = 0; i < lAr.length; i++) {
144 				ret[i] = (double) lAr[i];
145 			}
146 			return ret;
147 		} else if (array instanceof int[]) {
148 			int[] lAr = (int[]) array;
149 			double[] ret = new double[lAr.length];
150 			for (int i = 0; i < lAr.length; i++) {
151 				ret[i] = lAr[i];
152 			}
153 			return ret;
154 		} else if (array instanceof short[]) {
155 			short[] lAr = (short[]) array;
156 			double[] ret = new double[lAr.length];
157 			for (int i = 0; i < lAr.length; i++) {
158 				ret[i] = lAr[i];
159 			}
160 			return ret;
161 		} else if (array instanceof byte[]) {
162 			byte[] lAr = (byte[]) array;
163 			double[] ret = new double[lAr.length];
164 			for (int i = 0; i < lAr.length; i++) {
165 				ret[i] = lAr[i];
166 			}
167 			return ret;
168 		} else if (array instanceof Number[]) {
169 			Number[] lAr = (Number[]) array;
170 			double[] ret = new double[lAr.length];
171 			for (int i = 0; i < lAr.length; i++) {
172 				ret[i] = lAr[i].doubleValue();
173 			}
174 			return ret;
175 		}
176 		return null;
177 	}
178 
179 	public static Object trim(Object[] array) {
180 		int end = firstIndexOf(array, null);
181 		if (end == -1)
182 			return array;
183 		// System.out.println("Trimming at" + end);
184 		Object ret = Array
185 				.newInstance(array.getClass().getComponentType(), end);
186 		System.arraycopy(array, 0, ret, 0, end);
187 		return ret;
188 	}
189 
190 	public static Integer[] toIntegerArray(int[] input) {
191 		Integer[] ret = new Integer[input.length];
192 		for (int i = input.length - 1; i >= 0; i--) {
193 			ret[i] = new Integer(input[i]);
194 		}
195 		return ret;
196 	}
197 
198 	public static int[] toIntArray(Number[] input) {
199 		int[] ret = new int[input.length];
200 		for (int i = input.length - 1; i >= 0; i--) {
201 			ret[i] = input[i].intValue();
202 		}
203 		return ret;
204 	}
205 
206 	/**
207 	 * Get a sub array of a list.
208 	 * 
209 	 * @param <T>
210 	 *            Type of list's elements.
211 	 * @param clazz
212 	 *            The type of list's elements.
213 	 * @param list
214 	 *            The list.
215 	 * @param offset
216 	 *            Offset in the list that will be the first element of the
217 	 *            resulting array.
218 	 * @param length
219 	 *            Number of elements in the list.
220 	 * @return Subarray of the list.
221 	 */
222 	@SuppressWarnings("unchecked")
223 	public static <T> T[] subArray(Class<T> clazz, List<T> list, int offset,
224 			int length) {
225 		int resultLen = list.size() - offset;
226 		if (resultLen > length) {
227 			resultLen = length;
228 		}
229 		if (resultLen < 0) {
230 			resultLen = 0;
231 		}
232 		T[] result = (T[]) Array.newInstance(clazz, resultLen);
233 		for (int i = 0; i < resultLen; ++i) {
234 			if (offset + i >= 0) {
235 				result[i] = list.get(offset + i);
236 			}
237 		}
238 		return result;
239 	}
240 }