Source code: org/geotools/resources/XArray.java
1 /*
2 * -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
3 * :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
4 * ex: set tabstop=4 expandtab:
5 *
6 * MrPostman - webmail <-> email gateway
7 * Copyright (C) 2002-2003 MrPostman Development Group
8 * Projectpage: http://mrbook.org/mrpostman/
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * In particular, this implies that users are responsible for
21 * using MrPostman after reading the terms and conditions given
22 * by their web-mail provider.
23 *
24 * You should have received a copy of the GNU General Public License
25 * Named LICENSE in the base directory of this distribution,
26 * if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 /*
31 * Geotools - OpenSource mapping toolkit
32 * (C) 2002, Centre for Computational Geography
33 * (C) 2001, Institut de Recherche pour le Développement
34 *
35 * This library is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU Lesser General Public
37 * License as published by the Free Software Foundation; either
38 * version 2.1 of the License, or (at your option) any later version.
39 *
40 * This library is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * Lesser General Public License for more details.
44 *
45 * You should have received a copy of the GNU Lesser General Public
46 * License along with this library; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 *
49 *
50 * Contacts:
51 * UNITED KINGDOM: James Macgill
52 * mailto:j.macgill@geog.leeds.ac.uk
53 *
54 * FRANCE: Surveillance de l'Environnement Assistée par Satellite
55 * Institut de Recherche pour le Développement / US-Espace
56 * mailto:seasnet@teledetection.fr
57 *
58 * CANADA: Observatoire du Saint-Laurent
59 * Institut Maurice-Lamontagne
60 * mailto:osl@osl.gc.ca
61 */
62 package org.geotools.resources;
63
64 import java.lang.reflect.Array;
65
66
67 /**
68 * Simple operations on arrays. This class provides a central place for
69 * inserting and deleting elements in an array, as well as resizing the array.
70 * This class may be removed if JavaSoft provide some language construct
71 * functionally equivalent to C/C++'s <code>realloc</code>.
72 *
73 * @version $Id: XArray.java,v 1.5 2003/02/09 23:38:12 lbruand Exp $
74 * @author Martin Desruisseaux
75 */
76 public final class XArray {
77 public static final String CVSID = "$Id: XArray.java,v 1.5 2003/02/09 23:38:12 lbruand Exp $";
78
79 /**
80 * All object constructions of this class are forbidden.
81 */
82 private XArray() {
83 }
84
85 /**
86 * Returns a new table which contains the same elements as
87 * <code>array</code> but with the <code>length</code> specified.
88 * If the desired <code>length</code> is longer than the initial
89 * length of the <code>array</code> table, the returned table will contain
90 * all the elements of <code>array</code> as well as the elements
91 * initialised to <code>null</code> at the end of the table. If, on the
92 * contrary, the desired <code>length</code> is shorter than the initial
93 * length of the <code>array</code> table, the table will be truncated
94 * (that is to say the surplus <code>array</code> elements will be
95 * forgotten). If the length of <code>array</code> is equal to
96 * <code>length</code>, then <code>array</code> will be returned as it stands.
97 *
98 * @param array Table to copy
99 * @param length Length of the desired table.
100 * @return Table of the same type as <code>array</code>, of length
101 * <code>length</code> and containing the data from
102 * <code>array</code>.
103 */
104 private static Object doResize(final Object array, final int length) {
105 final int current = Array.getLength(array);
106
107 if (current != length) {
108 final Object newArray = Array.newInstance(array.getClass().getComponentType(), length);
109 System.arraycopy(array, 0, newArray, 0, Math.min(current, length));
110 return newArray;
111 } else {
112 return array;
113 }
114 }
115
116 /**
117 * Returns a new table which contains the same elements as
118 * <code>array</code> but with the <code>length</code> specified.
119 * If the desired <code>length</code> is longer than the initial
120 * length of the <code>array</code> table, the returned table will contain
121 * all the elements of <code>array</code> as well as the elements
122 * initialised to <code>null</code> at the end of the table. If, on the
123 * contrary, the desired <code>length</code> is shorter than the initial
124 * length of the <code>array</code> table, the table will be truncated
125 * (that is to say the surplus <code>array</code> elements will be
126 * forgotten). If the length of <code>array</code> is equal to
127 * <code>length</code>, then <code>array</code> will be returned as it
128 * stands.
129 *
130 * @param array Table to copy.
131 * @param length Length of the desired table.
132 * @return Table of the same type as <code>array</code>, of length
133 * <code>length</code> and containing the data from
134 * <code>array</code>.
135 */
136 public static Object[] resize(final Object[] array, final int length) {
137 return (Object[]) doResize(array, length);
138 }
139
140 /**
141 * Returns a new table which contains the same elements as
142 * <code>array</code> but with the <code>length</code> specified.
143 * If the desired <code>length</code> is longer than the initial
144 * length of the <code>array</code> table, the returned table will contain
145 * all the elements of <code>array</code> as well as the elements
146 * initialised to <code>null</code> at the end of the table. If, on the
147 * contrary, the desired <code>length</code> is shorter than the initial
148 * length of the <code>array</code> table, the table will be truncated
149 * (that is to say the surplus <code>array</code> elements will be
150 * forgotten). If the length of <code>array</code> is equal to
151 * <code>length</code>, then <code>array</code> will be returned as it
152 * stands.
153 *
154 * @param array Table to copy.
155 * @param length Length of the desired table.
156 * @return Table of the same type as <code>array</code>, of length
157 * <code>length</code> and containing the data from
158 * <code>array</code>.
159 */
160 public static double[] resize(final double[] array, final int length) {
161 return (double[]) doResize(array, length);
162 }
163
164 /**
165 * Returns a new table which contains the same elements as
166 * <code>array</code> but with the <code>length</code> specified.
167 * If the desired <code>length</code> is longer than the initial
168 * length of the <code>array</code> table, the returned table will contain
169 * all the elements of <code>array</code> as well as the elements
170 * initialised to <code>null</code> at the end of the table. If, on the
171 * contrary, the desired <code>length</code> is shorter than the initial
172 * length of the <code>array</code> table, the table will be truncated
173 * (that is to say the surplus <code>array</code> elements will be
174 * forgotten). If the length of <code>array</code> is equal to
175 * <code>length</code>, then <code>array</code> will be returned as it
176 * stands.
177 *
178 * @param array Table to copy.
179 * @param length Length of the desired table.
180 * @return Table of the same type as <code>array</code>, of length
181 * <code>length</code> and containing the data from
182 * <code>array</code>.
183 */
184 public static float[] resize(final float[] array, final int length) {
185 return (float[]) doResize(array, length);
186 }
187
188 /**
189 * Returns a new table which contains the same elements as
190 * <code>array</code> but with the <code>length</code> specified.
191 * If the desired <code>length</code> is longer than the initial
192 * length of the <code>array</code> table, the returned table will contain
193 * all the elements of <code>array</code> as well as the elements
194 * initialised to <code>null</code> at the end of the table. If, on the
195 * contrary, the desired <code>length</code> is shorter than the initial
196 * length of the <code>array</code> table, the table will be truncated
197 * (that is to say the surplus <code>array</code> elements will be
198 * forgotten). If the length of <code>array</code> is equal to
199 * <code>length</code>, then <code>array</code> will be returned as it
200 * stands.
201 *
202 * @param array Table to copy.
203 * @param length Length of the desired table.
204 * @return Table of the same type as <code>array</code>, of length
205 * <code>length</code> and containing the data from
206 * <code>array</code>.
207 */
208 public static long[] resize(final long[] array, final int length) {
209 return (long[]) doResize(array, length);
210 }
211
212 /**
213 * Returns a new table which contains the same elements as
214 * <code>array</code> but with the <code>length</code> specified.
215 * If the desired <code>length</code> is longer than the initial
216 * length of the <code>array</code> table, the returned table will contain
217 * all the elements of <code>array</code> as well as the elements
218 * initialised to <code>null</code> at the end of the table. If, on the
219 * contrary, the desired <code>length</code> is shorter than the initial
220 * length of the <code>array</code> table, the table will be truncated
221 * (that is to say the surplus <code>array</code> elements will be
222 * forgotten). If the length of <code>array</code> is equal to
223 * <code>length</code>, then <code>array</code> will be returned as it
224 * stands.
225 *
226 * @param array Table to copy.
227 * @param length Length of the desired table.
228 * @return Table of the same type as <code>array</code>, of length
229 * <code>length</code> and containing the data from
230 * <code>array</code>.
231 */
232 public static int[] resize(final int[] array, final int length) {
233 return (int[]) doResize(array, length);
234 }
235
236 /**
237 * Returns a new table which contains the same elements as
238 * <code>array</code> but with the <code>length</code> specified.
239 * If the desired <code>length</code> is longer than the initial
240 * length of the <code>array</code> table, the returned table will contain
241 * all the elements of <code>array</code> as well as the elements
242 * initialised to <code>null</code> at the end of the table. If, on the
243 * contrary, the desired <code>length</code> is shorter than the initial
244 * length of the <code>array</code> table, the table will be truncated
245 * (that is to say the surplus <code>array</code> elements will be
246 * forgotten). If the length of <code>array</code> is equal to
247 * <code>length</code>, then <code>array</code> will be returned as it
248 * stands.
249 *
250 * @param array Table to copy.
251 * @param length Length of the desired table.
252 * @return Table of the same type as <code>array</code>, of length
253 * <code>length</code> and containing the data from
254 * <code>array</code>.
255 */
256 public static short[] resize(final short[] array, final int length) {
257 return (short[]) doResize(array, length);
258 }
259
260 /**
261 * Returns a new table which contains the same elements as
262 * <code>array</code> but with the <code>length</code> specified.
263 * If the desired <code>length</code> is longer than the initial
264 * length of the <code>array</code> table, the returned table will contain
265 * all the elements of <code>array</code> as well as the elements
266 * initialised to <code>null</code> at the end of the table. If, on the
267 * contrary, the desired <code>length</code> is shorter than the initial
268 * length of the <code>array</code> table, the table will be truncated
269 * (that is to say the surplus <code>array</code> elements will be
270 * forgotten). If the length of <code>array</code> is equal to
271 * <code>length</code>, then <code>array</code> will be returned as it
272 * stands.
273 *
274 * @param array Table to copy.
275 * @param length Length of the desired table.
276 * @return Table of the same type as <code>array</code>, of length
277 * <code>length</code> and containing the data from
278 * <code>array</code>.
279 */
280 public static byte[] resize(final byte[] array, final int length) {
281 return (byte[]) doResize(array, length);
282 }
283
284 /**
285 * Returns a new table which contains the same elements as
286 * <code>array</code> but with the <code>length</code> specified.
287 * If the desired <code>length</code> is longer than the initial
288 * length of the <code>array</code> table, the returned table will contain
289 * all the elements of <code>array</code> as well as the elements
290 * initialised to <code>null</code> at the end of the table. If, on the
291 * contrary, the desired <code>length</code> is shorter than the initial
292 * length of the <code>array</code> table, the table will be truncated
293 * (that is to say the surplus <code>array</code> elements will be
294 * forgotten). If the length of <code>array</code> is equal to
295 * <code>length</code>, then <code>array</code> will be returned as it
296 * stands.
297 *
298 * @param array Table to copy.
299 * @param length Length of the desired table.
300 * @return Table of the same type as <code>array</code>, of length
301 * <code>length</code> and containing the data from
302 * <code>array</code>.
303 */
304 public static char[] resize(final char[] array, final int length) {
305 return (char[]) doResize(array, length);
306 }
307
308 /**
309 * Returns a new table which contains the same elements as
310 * <code>array</code> but with the <code>length</code> specified.
311 * If the desired <code>length</code> is longer than the initial
312 * length of the <code>array</code> table, the returned table will contain
313 * all the elements of <code>array</code> as well as the elements
314 * initialised to <code>null</code> at the end of the table. If, on the
315 * contrary, the desired <code>length</code> is shorter than the initial
316 * length of the <code>array</code> table, the table will be truncated
317 * (that is to say the surplus <code>array</code> elements will be
318 * forgotten). If the length of <code>array</code> is equal to
319 * <code>length</code>, then <code>array</code> will be returned as it
320 * stands.
321 *
322 * @param array Table to copy.
323 * @param length Length of the desired table.
324 * @return Table of the same type as <code>array</code>, of length
325 * <code>length</code> and containing the data from
326 * <code>array</code>.
327 */
328 public static boolean[] resize(final boolean[] array, final int length) {
329 return (boolean[]) doResize(array, length);
330 }
331
332 /**
333 * Grabs elements from the middle of a table.
334 *
335 * @param array Table from which to grab elements.
336 * @param index <code>array</code> index of the first element to grab.
337 * All subsequent elements of <code>array</code>
338 * can be moved forward.
339 * @param length Number of elements to grab.
340 * @return Table which contains the <code>array</code> data with the
341 * extracted elements. This method can directly return
342 * <code>dst</code>, but most often it returns a newly created
343 * table.
344 */
345 private static Object doRemove(final Object array, final int index, final int length) {
346 if (length == 0) {
347 return array;
348 }
349 int array_length = Array.getLength(array);
350 final Object newArray = Array.newInstance(array.getClass().getComponentType(), array_length -= length);
351 System.arraycopy(array, 0, newArray, 0, index);
352 System.arraycopy(array, index + length, newArray, index, array_length - index);
353 return newArray;
354 }
355
356 /**
357 * Grabs elements from the middle of a table.
358 *
359 * @param array Table from which to grab the elements.
360 * @param index <code>array</code> index of the first element to grab.
361 * All subsequent <code>array</code> elements can be moved forward.
362 * @param length Number of elements to grab.
363 * @return Table which contains the <code>array</code> data with the
364 * extracted elements. This method can directly return
365 * <code>dst</code>, but most often it returns a newly created
366 * table.
367 */
368 public static Object[] remove(final Object[] array, final int index, final int length) {
369 return (Object[]) doRemove(array, index, length);
370 }
371
372 /**
373 * Grabs elements from the middle of a table.
374 *
375 * @param array Table from which to grab elements.
376 * @param index <code>array</code> index of the first element to grab.
377 * All subsequent elements of <code>array</code>
378 * can be moved forward.
379 * @param length Number of elements to grab.
380 * @return Table which contains the <code>array</code> data with the
381 * extracted elements. This method can directly return
382 * <code>dst</code>, but most often it returns a newly created
383 * table.
384 */
385 public static double[] remove(final double[] array, final int index, final int length) {
386 return (double[]) doRemove(array, index, length);
387 }
388
389 /**
390 * Grabs elements from the middle of a table.
391 *
392 * @param array Table from which to grab elements.
393 * @param index <code>array</code> index of the first element to grab.
394 * All subsequent elements of <code>array</code>
395 * can be moved forward.
396 * @param length Number of elements to grab.
397 * @return Table which contains the <code>array</code> data with the
398 * extracted elements. This method can directly return
399 * <code>dst</code>, but most often it returns a newly created
400 * table.
401 */
402 public static float[] remove(final float[] array, final int index, final int length) {
403 return (float[]) doRemove(array, index, length);
404 }
405
406 /**
407 * Grabs elements from the middle of a table.
408 *
409 * @param array Table from which to grab elements.
410 * @param index <code>array</code> index of the first element to grab.
411 * All subsequent elements of <code>array</code>
412 * can be moved forward.
413 * @param length Number of elements to grab.
414 * @return Table which contains the <code>array</code> data with the
415 * extracted elements. This method can directly return
416 * <code>dst</code>, but most often it returns a newly created
417 * table.
418 */
419 public static long[] remove(final long[] array, final int index, final int length) {
420 return (long[]) doRemove(array, index, length);
421 }
422
423 /**
424 * Grabs elements from the middle of a table.
425 *
426 * @param array Table from which to grab elements.
427 * @param index <code>array</code> index of the first element to grab.
428 * All subsequent elements of <code>array</code>
429 * can be moved forward.
430 * @param length Number of elements to grab.
431 * @return Table which contains the <code>array</code> data with the
432 * extracted elements. This method can directly return
433 * <code>dst</code>, but most often it returns a newly created
434 * table.
435 */
436 public static int[] remove(final int[] array, final int index, final int length) {
437 return (int[]) doRemove(array, index, length);
438 }
439
440 /**
441 * Grabs elements from the middle of a table.
442 *
443 * @param array Table from which to grab elements.
444 * @param index <code>array</code> index of the first element to grab.
445 * All subsequent elements of <code>array</code>
446 * can be moved forward.
447 * @param length Number of elements to grab.
448 * @return Table which contains the <code>array</code> data with the
449 * extracted elements. This method can directly return
450 * <code>dst</code>, but most often it returns a newly created
451 * table.
452 */
453 public static short[] remove(final short[] array, final int index, final int length) {
454 return (short[]) doRemove(array, index, length);
455 }
456
457 /**
458 * Grabs elements from the middle of a table.
459 *
460 * @param array Table from which to grab elements.
461 * @param index <code>array</code> index of the first element to grab.
462 * All subsequent elements of <code>array</code>
463 * can be moved forward.
464 * @param length Number of elements to grab.
465 * @return Table which contains the <code>array</code> data with the
466 * extracted elements. This method can directly return
467 * <code>dst</code>, but most often it returns a newly created
468 * table.
469 */
470 public static byte[] remove(final byte[] array, final int index, final int length) {
471 return (byte[]) doRemove(array, index, length);
472 }
473
474 /**
475 * Grabs elements from the middle of a table.
476 *
477 * @param array Table from which to grab elements.
478 * @param index <code>array</code> index of the first element to grab.
479 * All subsequent elements of <code>array</code>
480 * can be moved forward.
481 * @param length Number of elements to grab.
482 * @return Table which contains the <code>array</code> data with the
483 * extracted elements. This method can directly return
484 * <code>dst</code>, but most often it returns a newly created
485 * table.
486 */
487 public static char[] remove(final char[] array, final int index, final int length) {
488 return (char[]) doRemove(array, index, length);
489 }
490
491 /**
492 * Grabs elements from the middle of a table.
493 *
494 * @param array Table from which to grab elements.
495 * @param index <code>array</code> index of the first element to grab.
496 * All subsequent elements of <code>array</code>
497 * can be moved forward.
498 * @param length Number of elements to grab.
499 * @return Table which contains the <code>array</code> data with the
500 * extracted elements. This method can directly return
501 * <code>dst</code>, but most often it returns a newly created
502 * table.
503 */
504 public static boolean[] remove(final boolean[] array, final int index, final int length) {
505 return (boolean[]) doRemove(array, index, length);
506 }
507
508 /**
509 * Inserts spaces into the middle of a table. These "spaces" will be made
510 * up of null elements.
511 *
512 * @param array Table in which to insert spaces.
513 * @param index <code>array</code> index where spaces should be inserted.
514 * All <code>array</code> elements which have an index equal
515 * to or higher than <code>index</code> will be moved
516 * forward.
517 * @param length Number of spaces to insert.
518 * @return Table which contains the <code>array</code> data with the
519 * additional space. This method can directly return
520 * <code>dst</code>, but most often it returns a newly
521 * created table.
522 */
523 private static Object doInsert(final Object array, final int index, final int length) {
524 if (length == 0) {
525 return array;
526 }
527 final int array_length = Array.getLength(array);
528 final Object newArray = Array.newInstance(array.getClass().getComponentType(), array_length + length);
529 System.arraycopy(array, 0, newArray, 0, index);
530 System.arraycopy(array, index, newArray, index + length, array_length - index);
531 return newArray;
532 }
533
534 /**
535 * Inserts spaces into the middle of a table. These "spaces" will be made
536 * up of null elements.
537 *
538 * @param array Table in which to insert spaces.
539 * @param index <code>array</code> index where spaces should be inserted.
540 * All <code>array</code> elements which have an index equal
541 * to or higher than <code>index</code> will be moved
542 * forward.
543 * @param length Number of spaces to insert.
544 * @return Table which contains the <code>array</code> data with the
545 * additional space. This method can directly return
546 * <code>dst</code>, but most often it returns a newly
547 * created table.
548 */
549 public static Object[] insert(final Object[] array, final int index, final int length) {
550 return (Object[]) doInsert(array, index, length);
551 }
552
553 /**
554 * Inserts spaces into the middle of a table. These "spaces" will be made
555 * up of zeros.
556 *
557 * @param array Table in which to insert spaces.
558 * @param index <code>array</code> index where spaces should be inserted.
559 * All <code>array</code> elements which have an index equal
560 * to or higher than <code>index</code> will be moved
561 * forward.
562 * @param length Number of spaces to insert.
563 * @return Table which contains the <code>array</code> data with the
564 * additional space. This method can directly return
565 * <code>dst</code>, but most often it returns a newly
566 * created table.
567 */
568 public static double[] insert(final double[] array, final int index, final int length) {
569 return (double[]) doInsert(array, index, length);
570 }
571
572 /**
573 * Inserts spaces into the middle of a table. These "spaces" will be made
574 * up of zeros.
575 *
576 * @param array Table in which to insert spaces.
577 * @param index <code>array</code> index where spaces should be inserted.
578 * All <code>array</code> elements which have an index equal
579 * to or higher than <code>index</code> will be moved
580 * forward.
581 * @param length Number of spaces to insert.
582 * @return Table which contains the <code>array</code> data with the
583 * additional space. This method can directly return
584 * <code>dst</code>, but most often it returns a newly
585 * created table.
586 */
587 public static float[] insert(final float[] array, final int index, final int length) {
588 return (float[]) doInsert(array, index, length);
589 }
590
591 /**
592 * Inserts spaces into the middle of a table. These "spaces" will be made
593 * up of zeros.
594 *
595 * @param array Table in which to insert spaces.
596 * @param index <code>array</code> index where spaces should be inserted.
597 * All <code>array</code> elements which have an index equal
598 * to or higher than <code>index</code> will be moved
599 * forward.
600 * @param length Number of spaces to insert.
601 * @return Table which contains the <code>array</code> data with the
602 * additional space. This method can directly return
603 * <code>dst</code>, but most often it returns a newly
604 * created table.
605 */
606 public static long[] insert(final long[] array, final int index, final int length) {
607 return (long[]) doInsert(array, index, length);
608 }
609
610 /**
611 * Inserts spaces into the middle of a table. These "spaces" will be made
612 * up of zeros.
613 *
614 * @param array Table in which to insert spaces.
615 * @param index <code>array</code> index where spaces should be inserted.
616 * All <code>array</code> elements which have an index equal
617 * to or higher than <code>index</code> will be moved
618 * forward.
619 * @param length Number of spaces to insert.
620 * @return Table which contains the <code>array</code> data with the
621 * additional space. This method can directly return
622 * <code>dst</code>, but most often it returns a newly
623 * created table.
624 */
625 public static int[] insert(final int[] array, final int index, final int length) {
626 return (int[]) doInsert(array, index, length);
627 }
628
629 /**
630 * Inserts spaces into the middle of a table. These "spaces" will be made
631 * up of zeros.
632 *
633 * @param array Table in which to insert spaces.
634 * @param index <code>array</code> index where spaces should be inserted.
635 * All <code>array</code> elements which have an index equal
636 * to or higher than <code>index</code> will be moved
637 * forward.
638 * @param length Number of spaces to insert.
639 * @return Table which contains the <code>array</code> data with the
640 * additional space. This method can directly return
641 * <code>dst</code>, but most often it returns a newly
642 * created table.
643 */
644 public static short[] insert(final short[] array, final int index, final int length) {
645 return (short[]) doInsert(array, index, length);
646 }
647
648 /**
649 * Inserts spaces into the middle of a table. These "spaces" will be made
650 * up of zeros.
651 *
652 * @param array Table in which to insert spaces.
653 * @param index <code>array</code> index where spaces should be inserted.
654 * All <code>array</code> elements which have an index equal
655 * to or higher than <code>index</code> will be moved
656 * forward.
657 * @param length Number of spaces to insert.
658 * @return Table which contains the <code>array</code> data with the
659 * additional space. This method can directly return
660 * <code>dst</code>, but most often it returns a newly
661 * created table.
662 */
663 public static byte[] insert(final byte[] array, final int index, final int length) {
664 return (byte[]) doInsert(array, index, length);
665 }
666
667 /**
668 * Inserts spaces into the middle of a table. These "spaces" will be made
669 * up of zeros.
670 *
671 * @param array Table in which to insert spaces.
672 * @param index <code>array</code> index where spaces should be inserted.
673 * All <code>array</code> elements which have an index equal
674 * to or higher than <code>index</code> will be moved
675 * forward.
676 * @param length Number of spaces to insert.
677 * @return Table which contains the <code>array</code> data with the
678 * additional space. This method can directly return
679 * <code>dst</code>, but most often it returns a newly
680 * created table.
681 */
682 public static char[] insert(final char[] array, final int index, final int length) {
683 return (char[]) doInsert(array, index, length);
684 }
685
686 /**
687 * Inserts spaces into the middle of a table. These "spaces" will be made
688 * up of <code>false</code>.
689 *
690 * @param array Table in which to insert spaces.
691 * @param index <code>array</code> index where spaces should be inserted.
692 * All <code>array</code> elements which have an index equal
693 * to or higher than <code>index</code> will be moved
694 * forward.
695 * @param length Number of spaces to insert.
696 * @return Table which contains the <code>array</code> data with the
697 * additional space. This method can directly return
698 * <code>dst</code>, but most often it returns a newly
699 * created table.
700 */
701 public static boolean[] insert(final boolean[] array, final int index, final int length) {
702 return (boolean[]) doInsert(array, index, length);
703 }
704
705 /**
706 * Inserts a table slice into another table. The <code>src</code> table
707 * will be entirely or partially inserted into the <code>dst</code> table.
708 *
709 * @param src Table to insert into <code>dst</code>.
710 * @param src_pos Index of the first data item of <code>src</code> to
711 * insert into <code>dst</code>.
712 * @param dst Table in which to insert <code>src</code> data.
713 * @param dst_pos <code>dst</code> index in which to insert
714 * <code>src</code> data. All elements of
715 * <code>dst</code> whose index is equal to or greater than
716 * <code>dst_pos</code> will be moved forward.
717 * @param length Number of <code>src</code> data items to insert.
718 * @return Table which contains the combination of <code>src</code>
719 * and <code>dst</code>. This method can directly return
720 * <code>dst</code>, but never <code>src</code>. It most
721 * often returns a newly created table.
722 */
723 private static Object doInsert(final Object src, final int src_pos, final Object dst, final int dst_pos,
724 final int length) {
725 if (length == 0) {
726 return dst;
727 }
728 final int dst_length = Array.getLength(dst);
729 final Object newArray = Array.newInstance(dst.getClass().getComponentType(), dst_length + length);
730 System.arraycopy(dst, 0, newArray, 0, dst_pos);
731 System.arraycopy(src, src_pos, newArray, dst_pos, length);
732 System.arraycopy(dst, dst_pos, newArray, dst_pos + length, dst_length - dst_pos);
733 return newArray;
734 }
735
736 /**
737 * Inserts a table slice into another table. The <code>src</code> table
738 * will be entirely or partially inserted into the <code>dst</code> table.
739 *
740 * @param src Tablea to insert into <code>dst</code>.
741 * @param src_pos Index of the first data item of <code>src</code> to
742 * insert into <code>dst</code>.
743 * @param dst Table in which to insert <code>src</code> data.
744 * @param dst_pos <code>dst</code> index in which to insert
745 * <code>src</code> data. All elements of
746 * <code>dst</code> whose index is equal to or greater than
747 * <code>dst_pos</code> will be moved forward.
748 * @param length Number of <code>src</code> data items to insert.
749 * @return Table which contains the combination of <code>src</code>
750 * and <code>dst</code>. This method can directly return
751 * <code>dst</code>, but never <code>src</code>. It most
752 * often returns a newly created table.
753 */
754 public static Object[] insert(final Object[] src, final int src_pos, final Object[] dst, final int dst_pos,
755 final int length) {
756 return (Object[]) doInsert(src, src_pos, dst, dst_pos, length);
757 }
758
759 /**
760 * Inserts a table slice into another table. The <code>src</code> table
761 * will be entirely or partially inserted into the <code>dst</code> table.
762 *
763 * @param src Tablea to insert into <code>dst</code>.
764 * @param src_pos Index of the first data item of <code>src</code> to
765 * insert into <code>dst</code>.
766 * @param dst Table in which to insert <code>src</code> data.
767 * @param dst_pos <code>dst</code> index in which to insert
768 * <code>src</code> data. All elements of
769 * <code>dst</code> whose index is equal to or greater than
770 * <code>dst_pos</code> will be moved forward.
771 * @param length Number of <code>src</code> data items to insert.
772 * @return Table which contains the combination of <code>src</code>
773 * and <code>dst</code>. This method can directly return
774 * <code>dst</code>, but never <code>src</code>. It most
775 * often returns a newly created table.
776 */
777 public static double[] insert(final double[] src, final int src_pos, final double[] dst, final int dst_pos,
778 final int length) {
779 return (double[]) doInsert(src, src_pos, dst, dst_pos, length);
780 }
781
782 /**
783 * Inserts a table slice into another table. The <code>src</code> table
784 * will be entirely or partially inserted into the <code>dst</code> table.
785 *
786 * @param src Tablea to insert into <code>dst</code>.
787 * @param src_pos Index of the first data item of <code>src</code> to
788 * insert into <code>dst</code>.
789 * @param dst Table in which to insert <code>src</code> data.
790 * @param dst_pos <code>dst</code> index in which to insert
791 * <code>src</code> data. All elements of
792 * <code>dst</code> whose index is equal to or greater than
793 * <code>dst_pos</code> will be moved forward.
794 * @param length Number of <code>src</code> data items to insert.
795 * @return Table which contains the combination of <code>src</code>
796 * and <code>dst</code>. This method can directly return
797 * <code>dst</code>, but never <code>src</code>. It most
798 * often returns a newly created table.
799 */
800 public static float[] insert(final float[] src, final int src_pos, final float[] dst, final int dst_pos,
801 final int length) {
802 return (float[]) doInsert(src, src_pos, dst, dst_pos, length);
803 }
804
805 /**
806 * Inserts a table slice into another table. The <code>src</code> table
807 * will be entirely or partially inserted into the <code>dst</code> table.
808 *
809 * @param src Tablea to insert into <code>dst</code>.
810 * @param src_pos Index of the first data item of <code>src</code> to
811 * insert into <code>dst</code>.
812 * @param dst Table in which to insert <code>src</code> data.
813 * @param dst_pos <code>dst</code> index in which to insert
814 * <code>src</code> data. All elements of
815 * <code>dst</code> whose index is equal to or greater than
816 * <code>dst_pos</code> will be moved forward.
817 * @param length Number of <code>src</code> data items to insert.
818 * @return Table which contains the combination of <code>src</code>
819 * and <code>dst</code>. This method can directly return
820 * <code>dst</code>, but never <code>src</code>. It most
821 * often returns a newly created table.
822 */
823 public static long[] insert(final long[] src, final int src_pos, final long[] dst, final int dst_pos,
824 final int length) {
825 return (long[]) doInsert(src, src_pos, dst, dst_pos, length);
826 }
827
828 /**
829 * Inserts a table slice into another table. The <code>src</code> table
830 * will be entirely or partially inserted into the <code>dst</code> table.
831 *
832 * @param src Tablea to insert into <code>dst</code>.
833 * @param src_pos Index of the first data item of <code>src</code> to
834 * insert into <code>dst</code>.
835 * @param dst Table in which to insert <code>src</code> data.
836 * @param dst_pos <code>dst</code> index in which to insert
837 * <code>src</code> data. All elements of
838 * <code>dst</code> whose index is equal to or greater than
839 * <code>dst_pos</code> will be moved forward.
840 * @param length Number of <code>src</code> data items to insert.
841 * @return Table which contains the combination of <code>src</code>
842 * and <code>dst</code>. This method can directly return
843 * <code>dst</code>, but never <code>src</code>. It most
844 * often returns a newly created table.
845 */
846 public static int[] insert(final int[] src, final int src_pos, final int[] dst, final int dst_pos, final int length) {
847 return (int[]) doInsert(src, src_pos, dst, dst_pos, length);
848 }
849
850 /**
851 * Inserts a table slice into another table. The <code>src</code> table
852 * will be entirely or partially inserted into the <code>dst</code> table.
853 *
854 * @param src Tablea to insert into <code>dst</code>.
855 * @param src_pos Index of the first data item of <code>src</code> to
856 * insert into <code>dst</code>.
857 * @param dst Table in which to insert <code>src</code> data.
858 * @param dst_pos <code>dst</code> index in which to insert
859 * <code>src</code> data. All elements of
860 * <code>dst</code> whose index is equal to or greater than
861 * <code>dst_pos</code> will be moved forward.
862 * @param length Number of <code>src</code> data items to insert.
863 * @return Table which contains the combination of <code>src</code>
864 * and <code>dst</code>. This method can directly return
865 * <code>dst</code>, but never <code>src</code>. It most
866 * often returns a newly created table.
867 */
868 public static short[] insert(final short[] src, final int src_pos, final short[] dst, final int dst_pos,
869 final int length) {
870 return (short[]) doInsert(src, src_pos, dst, dst_pos, length);
871 }
872
873 /**
874 * Inserts a table slice into another table. The <code>src</code> table
875 * will be entirely or partially inserted into the <code>dst</code> table.
876 *
877 * @param src Tablea to insert into <code>dst</code>.
878 * @param src_pos Index of the first data item of <code>src</code> to
879 * insert into <code>dst</code>.
880 * @param dst Table in which to insert <code>src</code> data.
881 * @param dst_pos <code>dst</code> index in which to insert
882 * <code>src</code> data. All elements of
883 * <code>dst</code> whose index is equal to or greater than
884 * <code>dst_pos</code> will be moved forward.
885 * @param length Number of <code>src</code> data items to insert.
886 * @return Table which contains the combination of <code>src</code>
887 * and <code>dst</code>. This method can directly return
888 * <code>dst</code>, but never <code>src</code>. It most
889 * often returns a newly created table.
890 */
891 public static byte[] insert(final byte[] src, final int src_pos, final byte[] dst, final int dst_pos,
892 final int length) {
893 return (byte[]) doInsert(src, src_pos, dst, dst_pos, length);
894 }
895
896 /**
897 * Inserts a table slice into another table. The <code>src</code> table
898 * will be entirely or partially inserted into the <code>dst</code> table.
899 *
900 * @param src Tablea to insert into <code>dst</code>.
901 * @param src_pos Index of the first data item of <code>src</code> to
902 * insert into <code>dst</code>.
903 * @param dst Table in which to insert <code>src</code> data.
904 * @param dst_pos <code>dst</code> index in which to insert
905 * <code>src</code> data. All elements of
906 * <code>dst</code> whose index is equal to or greater than
907 * <code>dst_pos</code> will be moved forward.
908 * @param length Number of <code>src</code> data items to insert.
909 * @return Table which contains the combination of <code>src</code>
910 * and <code>dst</code>. This method can directly return
911 * <code>dst</code>, but never <code>src</code>. It most
912 * often returns a newly created table.
913 */
914 public static char[] insert(final char[] src, final int src_pos, final char[] dst, final int dst_pos,
915 final int length) {
916 return (char[]) doInsert(src, src_pos, dst, dst_pos, length);
917 }
918
919 /**
920 * Inserts a table slice into another table. The <code>src</code> table
921 * will be entirely or partially inserted into the <code>dst</code> table.
922 *
923 * @param src Tablea to insert into <code>dst</code>.
924 * @param src_pos Index of the first data item of <code>src</code> to
925 * insert into <code>dst</code>.
926 * @param dst Table in which to insert <code>src</code> data.
927 * @param dst_pos <code>dst</code> index in which to insert
928 * <code>src</code> data. All elements of
929 * <code>dst</code> whose index is equal to or greater than
930 * <code>dst_pos</code> will be moved forward.
931 * @param length Number of <code>src</code> data items to insert.
932 * @return Table which contains the combination of <code>src</code>
933 * and <code>dst</code>. This method can directly return
934 * <code>dst</code>, but never <code>src</code>. It most
935 * often returns a newly created table.
936 */
937 public static boolean[] insert(final boolean[] src, final int src_pos, final boolean[] dst, final int dst_pos,
938 final int length) {
939 return (boolean[]) doInsert(src, src_pos, dst, dst_pos, length);
940 }
941
942 /**
943 * Returns <code>true</code> if all elements in the specified array are in increasing order.
944 * This method is usefull in assertions.
945 */
946 public static boolean isSorted(final byte[] array) {
947 for (int i = 1; i < array.length; i++) {
948 if (array[i] < array[i - 1]) {
949 return false;
950 }
951 }
952 return true;
953 }
954
955 /**
956 * Returns <code>true</code> if all elements in the specified array are in increasing order.
957 * This method is usefull in assertions.
958 */
959 public static boolean isSorted(final short[] array) {
960 for (int i = 1; i < array.length; i++) {
961 if (array[i] < array[i - 1]) {
962 return false;
963 }
964 }
965 return true;
966 }
967
968 /**
969 * Returns <code>true</code> if all elements in the specified array are in increasing order.
970 * This method is usefull in assertions.
971 */
972 public static boolean isSorted(final int[] array) {
973 for (int i = 1; i < array.length; i++) {
974 if (array[i] < array[i - 1]) {
975 return false;
976 }
977 }
978 return true;
979 }
980
981 /**
982 * Returns <code>true</code> if all elements in the specified array are in increasing order.
983 * This method is usefull in assertions.
984 */
985 public static boolean isSorted(final long[] array) {
986 for (int i = 1; i < array.length; i++) {
987 if (array[i] < array[i - 1]) {
988 return false;
989 }
990 }
991 return true;
992 }
993
994 /**
995 * Returns <code>true</code> if all elements in the specified array are in increasing order.
996 * Since <code>NaN</code> values are unordered, they may appears anywhere in the array; they
997 * will be ignored. This method is usefull in assertions.
998 */
999 public static boolean isSorted(final float[] array) {
1000 int previous = 0;
1001
1002 for (int i = 1; i < array.length; i++) {
1003 final float value = array[i];
1004
1005 if (value < array[previous]) {
1006 return false;
1007 }
1008
1009 if (!Float.isNaN(value)) {
1010 previous = i;
1011 }
1012 }
1013 return true;
1014 }
1015
1016 /**
1017 * Returns <code>true</code> if all elements in the specified array are in increasing order.
1018 * Since <code>NaN</code> values are unordered, they may appears anywhere in the array; they
1019 * will be ignored. This method is usefull in assertions.
1020 */
1021 public static boolean isSorted(final double[] array) {
1022 int previous = 0;
1023
1024 for (int i = 1; i < array.length; i++) {
1025 final double value = array[i];
1026
1027 if (value < array[previous]) {
1028 return false;
1029 }
1030
1031 if (!Double.isNaN(value)) {
1032 previous = i;
1033 }
1034 }
1035 return true;
1036 }
1037}