0001: /*
0002: * GeoTools - OpenSource mapping toolkit
0003: * http://geotools.org
0004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
0005: * (C) 2001, Institut de Recherche pour le Développement
0006: *
0007: * This library is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU Lesser General Public
0009: * License as published by the Free Software Foundation; either
0010: * version 2.1 of the License, or (at your option) any later version.
0011: *
0012: * This library is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * Lesser General Public License for more details.
0016: */
0017: package org.geotools.resources;
0018:
0019: // Miscellaneous
0020: import java.lang.reflect.Array;
0021: import java.text.FieldPosition;
0022: import java.text.NumberFormat;
0023: import java.util.Locale;
0024:
0025: /**
0026: * Simple operations on arrays. This class provides a central place for
0027: * inserting and deleting elements in an array, as well as resizing the array.
0028: * This class may be removed if JavaSoft provide some language construct
0029: * functionally equivalent to C/C++'s {@code realloc}.
0030: *
0031: * @since 2.0
0032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/resources/XArray.java $
0033: * @version $Id: XArray.java 22443 2006-10-27 20:47:22Z desruisseaux $
0034: * @author Martin Desruisseaux
0035: *
0036: * @todo Replace all {@code resize} methods by {@code Arrays.copyOf} when we will be allowed to
0037: * compile for JSE 6.0.
0038: */
0039: public final class XArray {
0040: /**
0041: * All object constructions of this class are forbidden.
0042: */
0043: private XArray() {
0044: }
0045:
0046: /**
0047: * Returns a new table which contains the same elements as
0048: * {@code array} but with the {@code length} specified.
0049: * If the desired {@code length} is longer than the initial
0050: * length of the {@code array} table, the returned table will contain
0051: * all the elements of {@code array} as well as the elements
0052: * initialised to {@code null} at the end of the table. If, on the
0053: * contrary, the desired {@code length} is shorter than the initial
0054: * length of the {@code array} table, the table will be truncated
0055: * (that is to say the surplus {@code array} elements will be
0056: * forgotten). If the length of {@code array} is equal to
0057: * {@code length}, then {@code array} will be returned as it stands.
0058: *
0059: * @param array Table to copy
0060: * @param length Length of the desired table.
0061: * @return Table of the same type as {@code array}, of length
0062: * {@code length} and containing the data from
0063: * {@code array}.
0064: */
0065: private static Object doResize(final Object array, final int length) {
0066: final int current = array == null ? 0 : Array.getLength(array);
0067: if (current != length) {
0068: final Object newArray = Array.newInstance(array.getClass()
0069: .getComponentType(), length);
0070: System.arraycopy(array, 0, newArray, 0, Math.min(current,
0071: length));
0072: return newArray;
0073: } else {
0074: return array;
0075: }
0076: }
0077:
0078: /**
0079: * Returns a new table which contains the same elements as
0080: * {@code array} but with the {@code length} specified.
0081: * If the desired {@code length} is longer than the initial
0082: * length of the {@code array} table, the returned table will contain
0083: * all the elements of {@code array} as well as the elements
0084: * initialised to {@code null} at the end of the table. If, on the
0085: * contrary, the desired {@code length} is shorter than the initial
0086: * length of the {@code array} table, the table will be truncated
0087: * (that is to say the surplus {@code array} elements will be
0088: * forgotten). If the length of {@code array} is equal to
0089: * {@code length}, then {@code array} will be returned as it
0090: * stands.
0091: *
0092: * @param array Table to copy.
0093: * @param length Length of the desired table.
0094: * @return Table of the same type as {@code array}, of length
0095: * {@code length} and containing the data from
0096: * {@code array}.
0097: */
0098: public static Object[] resize(final Object[] array, final int length) {
0099: return (Object[]) doResize(array, length);
0100: }
0101:
0102: /**
0103: * Returns a new table which contains the same elements as
0104: * {@code array} but with the {@code length} specified.
0105: * If the desired {@code length} is longer than the initial
0106: * length of the {@code array} table, the returned table will contain
0107: * all the elements of {@code array} as well as the elements
0108: * initialised to {@code null} at the end of the table. If, on the
0109: * contrary, the desired {@code length} is shorter than the initial
0110: * length of the {@code array} table, the table will be truncated
0111: * (that is to say the surplus {@code array} elements will be
0112: * forgotten). If the length of {@code array} is equal to
0113: * {@code length}, then {@code array} will be returned as it
0114: * stands.
0115: *
0116: * @param array Table to copy.
0117: * @param length Length of the desired table.
0118: * @return Table of the same type as {@code array}, of length
0119: * {@code length} and containing the data from
0120: * {@code array}.
0121: */
0122: public static double[] resize(final double[] array, final int length) {
0123: return (double[]) doResize(array, length);
0124: }
0125:
0126: /**
0127: * Returns a new table which contains the same elements as
0128: * {@code array} but with the {@code length} specified.
0129: * If the desired {@code length} is longer than the initial
0130: * length of the {@code array} table, the returned table will contain
0131: * all the elements of {@code array} as well as the elements
0132: * initialised to {@code null} at the end of the table. If, on the
0133: * contrary, the desired {@code length} is shorter than the initial
0134: * length of the {@code array} table, the table will be truncated
0135: * (that is to say the surplus {@code array} elements will be
0136: * forgotten). If the length of {@code array} is equal to
0137: * {@code length}, then {@code array} will be returned as it
0138: * stands.
0139: *
0140: * @param array Table to copy.
0141: * @param length Length of the desired table.
0142: * @return Table of the same type as {@code array}, of length
0143: * {@code length} and containing the data from
0144: * {@code array}.
0145: */
0146: public static float[] resize(final float[] array, final int length) {
0147: return (float[]) doResize(array, length);
0148: }
0149:
0150: /**
0151: * Returns a new table which contains the same elements as
0152: * {@code array} but with the {@code length} specified.
0153: * If the desired {@code length} is longer than the initial
0154: * length of the {@code array} table, the returned table will contain
0155: * all the elements of {@code array} as well as the elements
0156: * initialised to {@code null} at the end of the table. If, on the
0157: * contrary, the desired {@code length} is shorter than the initial
0158: * length of the {@code array} table, the table will be truncated
0159: * (that is to say the surplus {@code array} elements will be
0160: * forgotten). If the length of {@code array} is equal to
0161: * {@code length}, then {@code array} will be returned as it
0162: * stands.
0163: *
0164: * @param array Table to copy.
0165: * @param length Length of the desired table.
0166: * @return Table of the same type as {@code array}, of length
0167: * {@code length} and containing the data from
0168: * {@code array}.
0169: */
0170: public static long[] resize(final long[] array, final int length) {
0171: return (long[]) doResize(array, length);
0172: }
0173:
0174: /**
0175: * Returns a new table which contains the same elements as
0176: * {@code array} but with the {@code length} specified.
0177: * If the desired {@code length} is longer than the initial
0178: * length of the {@code array} table, the returned table will contain
0179: * all the elements of {@code array} as well as the elements
0180: * initialised to {@code null} at the end of the table. If, on the
0181: * contrary, the desired {@code length} is shorter than the initial
0182: * length of the {@code array} table, the table will be truncated
0183: * (that is to say the surplus {@code array} elements will be
0184: * forgotten). If the length of {@code array} is equal to
0185: * {@code length}, then {@code array} will be returned as it
0186: * stands.
0187: *
0188: * @param array Table to copy.
0189: * @param length Length of the desired table.
0190: * @return Table of the same type as {@code array}, of length
0191: * {@code length} and containing the data from
0192: * {@code array}.
0193: */
0194: public static int[] resize(final int[] array, final int length) {
0195: return (int[]) doResize(array, length);
0196: }
0197:
0198: /**
0199: * Returns a new table which contains the same elements as
0200: * {@code array} but with the {@code length} specified.
0201: * If the desired {@code length} is longer than the initial
0202: * length of the {@code array} table, the returned table will contain
0203: * all the elements of {@code array} as well as the elements
0204: * initialised to {@code null} at the end of the table. If, on the
0205: * contrary, the desired {@code length} is shorter than the initial
0206: * length of the {@code array} table, the table will be truncated
0207: * (that is to say the surplus {@code array} elements will be
0208: * forgotten). If the length of {@code array} is equal to
0209: * {@code length}, then {@code array} will be returned as it
0210: * stands.
0211: *
0212: * @param array Table to copy.
0213: * @param length Length of the desired table.
0214: * @return Table of the same type as {@code array}, of length
0215: * {@code length} and containing the data from
0216: * {@code array}.
0217: */
0218: public static short[] resize(final short[] array, final int length) {
0219: return (short[]) doResize(array, length);
0220: }
0221:
0222: /**
0223: * Returns a new table which contains the same elements as
0224: * {@code array} but with the {@code length} specified.
0225: * If the desired {@code length} is longer than the initial
0226: * length of the {@code array} table, the returned table will contain
0227: * all the elements of {@code array} as well as the elements
0228: * initialised to {@code null} at the end of the table. If, on the
0229: * contrary, the desired {@code length} is shorter than the initial
0230: * length of the {@code array} table, the table will be truncated
0231: * (that is to say the surplus {@code array} elements will be
0232: * forgotten). If the length of {@code array} is equal to
0233: * {@code length}, then {@code array} will be returned as it
0234: * stands.
0235: *
0236: * @param array Table to copy.
0237: * @param length Length of the desired table.
0238: * @return Table of the same type as {@code array}, of length
0239: * {@code length} and containing the data from
0240: * {@code array}.
0241: */
0242: public static byte[] resize(final byte[] array, final int length) {
0243: return (byte[]) doResize(array, length);
0244: }
0245:
0246: /**
0247: * Returns a new table which contains the same elements as
0248: * {@code array} but with the {@code length} specified.
0249: * If the desired {@code length} is longer than the initial
0250: * length of the {@code array} table, the returned table will contain
0251: * all the elements of {@code array} as well as the elements
0252: * initialised to {@code null} at the end of the table. If, on the
0253: * contrary, the desired {@code length} is shorter than the initial
0254: * length of the {@code array} table, the table will be truncated
0255: * (that is to say the surplus {@code array} elements will be
0256: * forgotten). If the length of {@code array} is equal to
0257: * {@code length}, then {@code array} will be returned as it
0258: * stands.
0259: *
0260: * @param array Table to copy.
0261: * @param length Length of the desired table.
0262: * @return Table of the same type as {@code array}, of length
0263: * {@code length} and containing the data from
0264: * {@code array}.
0265: */
0266: public static char[] resize(final char[] array, final int length) {
0267: return (char[]) doResize(array, length);
0268: }
0269:
0270: /**
0271: * Returns a new table which contains the same elements as
0272: * {@code array} but with the {@code length} specified.
0273: * If the desired {@code length} is longer than the initial
0274: * length of the {@code array} table, the returned table will contain
0275: * all the elements of {@code array} as well as the elements
0276: * initialised to {@code null} at the end of the table. If, on the
0277: * contrary, the desired {@code length} is shorter than the initial
0278: * length of the {@code array} table, the table will be truncated
0279: * (that is to say the surplus {@code array} elements will be
0280: * forgotten). If the length of {@code array} is equal to
0281: * {@code length}, then {@code array} will be returned as it
0282: * stands.
0283: *
0284: * @param array Table to copy.
0285: * @param length Length of the desired table.
0286: * @return Table of the same type as {@code array}, of length
0287: * {@code length} and containing the data from
0288: * {@code array}.
0289: */
0290: public static boolean[] resize(final boolean[] array,
0291: final int length) {
0292: return (boolean[]) doResize(array, length);
0293: }
0294:
0295: /**
0296: * Grabs elements from the middle of a table.
0297: *
0298: * @param array Table from which to grab elements.
0299: * @param index {@code array} index of the first element to grab.
0300: * All subsequent elements of {@code array}
0301: * can be moved forward.
0302: * @param length Number of elements to grab.
0303: * @return Table which contains the {@code array} data with the
0304: * extracted elements. This method can directly return
0305: * {@code dst}, but most often it returns a newly created
0306: * table.
0307: */
0308: private static Object doRemove(final Object array, final int index,
0309: final int length) {
0310: if (length == 0) {
0311: return array;
0312: }
0313: int arrayLength = Array.getLength(array);
0314: final Object newArray = Array.newInstance(array.getClass()
0315: .getComponentType(), arrayLength -= length);
0316: System.arraycopy(array, 0, newArray, 0, index);
0317: System.arraycopy(array, index + length, newArray, index,
0318: arrayLength - index);
0319: return newArray;
0320: }
0321:
0322: /**
0323: * Grabs elements from the middle of a table.
0324: *
0325: * @param array Table from which to grab the elements.
0326: * @param index {@code array} index of the first element to grab.
0327: * All subsequent {@code array} elements can be moved forward.
0328: * @param length Number of elements to grab.
0329: * @return Table which contains the {@code array} data with the
0330: * extracted elements. This method can directly return
0331: * {@code dst}, but most often it returns a newly created
0332: * table.
0333: */
0334: public static Object[] remove(final Object[] array,
0335: final int index, final int length) {
0336: return (Object[]) doRemove(array, index, length);
0337: }
0338:
0339: /**
0340: * Grabs elements from the middle of a table.
0341: *
0342: * @param array Table from which to grab elements.
0343: * @param index {@code array} index of the first element to grab.
0344: * All subsequent elements of {@code array}
0345: * can be moved forward.
0346: * @param length Number of elements to grab.
0347: * @return Table which contains the {@code array} data with the
0348: * extracted elements. This method can directly return
0349: * {@code dst}, but most often it returns a newly created
0350: * table.
0351: */
0352: public static double[] remove(final double[] array,
0353: final int index, final int length) {
0354: return (double[]) doRemove(array, index, length);
0355: }
0356:
0357: /**
0358: * Grabs elements from the middle of a table.
0359: *
0360: * @param array Table from which to grab elements.
0361: * @param index {@code array} index of the first element to grab.
0362: * All subsequent elements of {@code array}
0363: * can be moved forward.
0364: * @param length Number of elements to grab.
0365: * @return Table which contains the {@code array} data with the
0366: * extracted elements. This method can directly return
0367: * {@code dst}, but most often it returns a newly created
0368: * table.
0369: */
0370: public static float[] remove(final float[] array, final int index,
0371: final int length) {
0372: return (float[]) doRemove(array, index, length);
0373: }
0374:
0375: /**
0376: * Grabs elements from the middle of a table.
0377: *
0378: * @param array Table from which to grab elements.
0379: * @param index {@code array} index of the first element to grab.
0380: * All subsequent elements of {@code array}
0381: * can be moved forward.
0382: * @param length Number of elements to grab.
0383: * @return Table which contains the {@code array} data with the
0384: * extracted elements. This method can directly return
0385: * {@code dst}, but most often it returns a newly created
0386: * table.
0387: */
0388: public static long[] remove(final long[] array, final int index,
0389: final int length) {
0390: return (long[]) doRemove(array, index, length);
0391: }
0392:
0393: /**
0394: * Grabs elements from the middle of a table.
0395: *
0396: * @param array Table from which to grab elements.
0397: * @param index {@code array} index of the first element to grab.
0398: * All subsequent elements of {@code array}
0399: * can be moved forward.
0400: * @param length Number of elements to grab.
0401: * @return Table which contains the {@code array} data with the
0402: * extracted elements. This method can directly return
0403: * {@code dst}, but most often it returns a newly created
0404: * table.
0405: */
0406: public static int[] remove(final int[] array, final int index,
0407: final int length) {
0408: return (int[]) doRemove(array, index, length);
0409: }
0410:
0411: /**
0412: * Grabs elements from the middle of a table.
0413: *
0414: * @param array Table from which to grab elements.
0415: * @param index {@code array} index of the first element to grab.
0416: * All subsequent elements of {@code array}
0417: * can be moved forward.
0418: * @param length Number of elements to grab.
0419: * @return Table which contains the {@code array} data with the
0420: * extracted elements. This method can directly return
0421: * {@code dst}, but most often it returns a newly created
0422: * table.
0423: */
0424: public static short[] remove(final short[] array, final int index,
0425: final int length) {
0426: return (short[]) doRemove(array, index, length);
0427: }
0428:
0429: /**
0430: * Grabs elements from the middle of a table.
0431: *
0432: * @param array Table from which to grab elements.
0433: * @param index {@code array} index of the first element to grab.
0434: * All subsequent elements of {@code array}
0435: * can be moved forward.
0436: * @param length Number of elements to grab.
0437: * @return Table which contains the {@code array} data with the
0438: * extracted elements. This method can directly return
0439: * {@code dst}, but most often it returns a newly created
0440: * table.
0441: */
0442: public static byte[] remove(final byte[] array, final int index,
0443: final int length) {
0444: return (byte[]) doRemove(array, index, length);
0445: }
0446:
0447: /**
0448: * Grabs elements from the middle of a table.
0449: *
0450: * @param array Table from which to grab elements.
0451: * @param index {@code array} index of the first element to grab.
0452: * All subsequent elements of {@code array}
0453: * can be moved forward.
0454: * @param length Number of elements to grab.
0455: * @return Table which contains the {@code array} data with the
0456: * extracted elements. This method can directly return
0457: * {@code dst}, but most often it returns a newly created
0458: * table.
0459: */
0460: public static char[] remove(final char[] array, final int index,
0461: final int length) {
0462: return (char[]) doRemove(array, index, length);
0463: }
0464:
0465: /**
0466: * Grabs elements from the middle of a table.
0467: *
0468: * @param array Table from which to grab elements.
0469: * @param index {@code array} index of the first element to grab.
0470: * All subsequent elements of {@code array}
0471: * can be moved forward.
0472: * @param length Number of elements to grab.
0473: * @return Table which contains the {@code array} data with the
0474: * extracted elements. This method can directly return
0475: * {@code dst}, but most often it returns a newly created
0476: * table.
0477: */
0478: public static boolean[] remove(final boolean[] array,
0479: final int index, final int length) {
0480: return (boolean[]) doRemove(array, index, length);
0481: }
0482:
0483: /**
0484: * Inserts spaces into the middle of a table. These "spaces" will be made
0485: * up of null elements.
0486: *
0487: * @param array Table in which to insert spaces.
0488: * @param index {@code array} index where spaces should be inserted.
0489: * All {@code array} elements which have an index equal
0490: * to or higher than {@code index} will be moved
0491: * forward.
0492: * @param length Number of spaces to insert.
0493: * @return Table which contains the {@code array} data with the
0494: * additional space. This method can directly return
0495: * {@code dst}, but most often it returns a newly
0496: * created table.
0497: */
0498: private static Object doInsert(final Object array, final int index,
0499: final int length) {
0500: if (length == 0) {
0501: return array;
0502: }
0503: final int arrayLength = Array.getLength(array);
0504: final Object newArray = Array.newInstance(array.getClass()
0505: .getComponentType(), arrayLength + length);
0506: System.arraycopy(array, 0, newArray, 0, index);
0507: System.arraycopy(array, index, newArray, index + length,
0508: arrayLength - index);
0509: return newArray;
0510: }
0511:
0512: /**
0513: * Inserts spaces into the middle of a table. These "spaces" will be made
0514: * up of null elements.
0515: *
0516: * @param array Table in which to insert spaces.
0517: * @param index {@code array} index where spaces should be inserted.
0518: * All {@code array} elements which have an index equal
0519: * to or higher than {@code index} will be moved
0520: * forward.
0521: * @param length Number of spaces to insert.
0522: * @return Table which contains the {@code array} data with the
0523: * additional space. This method can directly return
0524: * {@code dst}, but most often it returns a newly
0525: * created table.
0526: */
0527: public static Object[] insert(final Object[] array,
0528: final int index, final int length) {
0529: return (Object[]) doInsert(array, index, length);
0530: }
0531:
0532: /**
0533: * Inserts spaces into the middle of a table. These "spaces" will be made
0534: * up of zeros.
0535: *
0536: * @param array Table in which to insert spaces.
0537: * @param index {@code array} index where spaces should be inserted.
0538: * All {@code array} elements which have an index equal
0539: * to or higher than {@code index} will be moved
0540: * forward.
0541: * @param length Number of spaces to insert.
0542: * @return Table which contains the {@code array} data with the
0543: * additional space. This method can directly return
0544: * {@code dst}, but most often it returns a newly
0545: * created table.
0546: */
0547: public static double[] insert(final double[] array,
0548: final int index, final int length) {
0549: return (double[]) doInsert(array, index, length);
0550: }
0551:
0552: /**
0553: * Inserts spaces into the middle of a table. These "spaces" will be made
0554: * up of zeros.
0555: *
0556: * @param array Table in which to insert spaces.
0557: * @param index {@code array} index where spaces should be inserted.
0558: * All {@code array} elements which have an index equal
0559: * to or higher than {@code index} will be moved
0560: * forward.
0561: * @param length Number of spaces to insert.
0562: * @return Table which contains the {@code array} data with the
0563: * additional space. This method can directly return
0564: * {@code dst}, but most often it returns a newly
0565: * created table.
0566: */
0567: public static float[] insert(final float[] array, final int index,
0568: final int length) {
0569: return (float[]) doInsert(array, index, length);
0570: }
0571:
0572: /**
0573: * Inserts spaces into the middle of a table. These "spaces" will be made
0574: * up of zeros.
0575: *
0576: * @param array Table in which to insert spaces.
0577: * @param index {@code array} index where spaces should be inserted.
0578: * All {@code array} elements which have an index equal
0579: * to or higher than {@code index} will be moved
0580: * forward.
0581: * @param length Number of spaces to insert.
0582: * @return Table which contains the {@code array} data with the
0583: * additional space. This method can directly return
0584: * {@code dst}, but most often it returns a newly
0585: * created table.
0586: */
0587: public static long[] insert(final long[] array, final int index,
0588: final int length) {
0589: return (long[]) doInsert(array, index, length);
0590: }
0591:
0592: /**
0593: * Inserts spaces into the middle of a table. These "spaces" will be made
0594: * up of zeros.
0595: *
0596: * @param array Table in which to insert spaces.
0597: * @param index {@code array} index where spaces should be inserted.
0598: * All {@code array} elements which have an index equal
0599: * to or higher than {@code index} will be moved
0600: * forward.
0601: * @param length Number of spaces to insert.
0602: * @return Table which contains the {@code array} data with the
0603: * additional space. This method can directly return
0604: * {@code dst}, but most often it returns a newly
0605: * created table.
0606: */
0607: public static int[] insert(final int[] array, final int index,
0608: final int length) {
0609: return (int[]) doInsert(array, index, length);
0610: }
0611:
0612: /**
0613: * Inserts spaces into the middle of a table. These "spaces" will be made
0614: * up of zeros.
0615: *
0616: * @param array Table in which to insert spaces.
0617: * @param index {@code array} index where spaces should be inserted.
0618: * All {@code array} elements which have an index equal
0619: * to or higher than {@code index} will be moved
0620: * forward.
0621: * @param length Number of spaces to insert.
0622: * @return Table which contains the {@code array} data with the
0623: * additional space. This method can directly return
0624: * {@code dst}, but most often it returns a newly
0625: * created table.
0626: */
0627: public static short[] insert(final short[] array, final int index,
0628: final int length) {
0629: return (short[]) doInsert(array, index, length);
0630: }
0631:
0632: /**
0633: * Inserts spaces into the middle of a table. These "spaces" will be made
0634: * up of zeros.
0635: *
0636: * @param array Table in which to insert spaces.
0637: * @param index {@code array} index where spaces should be inserted.
0638: * All {@code array} elements which have an index equal
0639: * to or higher than {@code index} will be moved
0640: * forward.
0641: * @param length Number of spaces to insert.
0642: * @return Table which contains the {@code array} data with the
0643: * additional space. This method can directly return
0644: * {@code dst}, but most often it returns a newly
0645: * created table.
0646: */
0647: public static byte[] insert(final byte[] array, final int index,
0648: final int length) {
0649: return (byte[]) doInsert(array, index, length);
0650: }
0651:
0652: /**
0653: * Inserts spaces into the middle of a table. These "spaces" will be made
0654: * up of zeros.
0655: *
0656: * @param array Table in which to insert spaces.
0657: * @param index {@code array} index where spaces should be inserted.
0658: * All {@code array} elements which have an index equal
0659: * to or higher than {@code index} will be moved
0660: * forward.
0661: * @param length Number of spaces to insert.
0662: * @return Table which contains the {@code array} data with the
0663: * additional space. This method can directly return
0664: * {@code dst}, but most often it returns a newly
0665: * created table.
0666: */
0667: public static char[] insert(final char[] array, final int index,
0668: final int length) {
0669: return (char[]) doInsert(array, index, length);
0670: }
0671:
0672: /**
0673: * Inserts spaces into the middle of a table. These "spaces" will be made
0674: * up of {@code false}.
0675: *
0676: * @param array Table in which to insert spaces.
0677: * @param index {@code array} index where spaces should be inserted.
0678: * All {@code array} elements which have an index equal
0679: * to or higher than {@code index} will be moved
0680: * forward.
0681: * @param length Number of spaces to insert.
0682: * @return Table which contains the {@code array} data with the
0683: * additional space. This method can directly return
0684: * {@code dst}, but most often it returns a newly
0685: * created table.
0686: */
0687: public static boolean[] insert(final boolean[] array,
0688: final int index, final int length) {
0689: return (boolean[]) doInsert(array, index, length);
0690: }
0691:
0692: /**
0693: * Inserts a table slice into another table. The {@code src} table
0694: * will be entirely or partially inserted into the {@code dst} table.
0695: *
0696: * @param src Table to insert into {@code dst}.
0697: * @param src_pos Index of the first data item of {@code src} to
0698: * insert into {@code dst}.
0699: * @param dst Table in which to insert {@code src} data.
0700: * @param dst_pos {@code dst} index in which to insert
0701: * {@code src} data. All elements of
0702: * {@code dst} whose index is equal to or greater than
0703: * {@code dst_pos} will be moved forward.
0704: * @param length Number of {@code src} data items to insert.
0705: * @return Table which contains the combination of {@code src}
0706: * and {@code dst}. This method can directly return
0707: * {@code dst}, but never {@code src}. It most
0708: * often returns a newly created table.
0709: */
0710: private static Object doInsert(final Object src, final int src_pos,
0711: final Object dst, final int dst_pos, final int length) {
0712: if (length == 0) {
0713: return dst;
0714: }
0715: final int dstLength = Array.getLength(dst);
0716: final Object newArray = Array.newInstance(dst.getClass()
0717: .getComponentType(), dstLength + length);
0718: System.arraycopy(dst, 0, newArray, 0, dst_pos);
0719: System.arraycopy(src, src_pos, newArray, dst_pos, length);
0720: System.arraycopy(dst, dst_pos, newArray, dst_pos + length,
0721: dstLength - dst_pos);
0722: return newArray;
0723: }
0724:
0725: /**
0726: * Inserts a table slice into another table. The {@code src} table
0727: * will be entirely or partially inserted into the {@code dst} table.
0728: *
0729: * @param src Tablea to insert into {@code dst}.
0730: * @param src_pos Index of the first data item of {@code src} to
0731: * insert into {@code dst}.
0732: * @param dst Table in which to insert {@code src} data.
0733: * @param dst_pos {@code dst} index in which to insert
0734: * {@code src} data. All elements of
0735: * {@code dst} whose index is equal to or greater than
0736: * {@code dst_pos} will be moved forward.
0737: * @param length Number of {@code src} data items to insert.
0738: * @return Table which contains the combination of {@code src}
0739: * and {@code dst}. This method can directly return
0740: * {@code dst}, but never {@code src}. It most
0741: * often returns a newly created table.
0742: */
0743: public static Object[] insert(final Object[] src,
0744: final int src_pos, final Object[] dst, final int dst_pos,
0745: final int length) {
0746: return (Object[]) doInsert(src, src_pos, dst, dst_pos, length);
0747: }
0748:
0749: /**
0750: * Inserts a table slice into another table. The {@code src} table
0751: * will be entirely or partially inserted into the {@code dst} table.
0752: *
0753: * @param src Tablea to insert into {@code dst}.
0754: * @param src_pos Index of the first data item of {@code src} to
0755: * insert into {@code dst}.
0756: * @param dst Table in which to insert {@code src} data.
0757: * @param dst_pos {@code dst} index in which to insert
0758: * {@code src} data. All elements of
0759: * {@code dst} whose index is equal to or greater than
0760: * {@code dst_pos} will be moved forward.
0761: * @param length Number of {@code src} data items to insert.
0762: * @return Table which contains the combination of {@code src}
0763: * and {@code dst}. This method can directly return
0764: * {@code dst}, but never {@code src}. It most
0765: * often returns a newly created table.
0766: */
0767: public static double[] insert(final double[] src,
0768: final int src_pos, final double[] dst, final int dst_pos,
0769: final int length) {
0770: return (double[]) doInsert(src, src_pos, dst, dst_pos, length);
0771: }
0772:
0773: /**
0774: * Inserts a table slice into another table. The {@code src} table
0775: * will be entirely or partially inserted into the {@code dst} table.
0776: *
0777: * @param src Tablea to insert into {@code dst}.
0778: * @param src_pos Index of the first data item of {@code src} to
0779: * insert into {@code dst}.
0780: * @param dst Table in which to insert {@code src} data.
0781: * @param dst_pos {@code dst} index in which to insert
0782: * {@code src} data. All elements of
0783: * {@code dst} whose index is equal to or greater than
0784: * {@code dst_pos} will be moved forward.
0785: * @param length Number of {@code src} data items to insert.
0786: * @return Table which contains the combination of {@code src}
0787: * and {@code dst}. This method can directly return
0788: * {@code dst}, but never {@code src}. It most
0789: * often returns a newly created table.
0790: */
0791: public static float[] insert(final float[] src, final int src_pos,
0792: final float[] dst, final int dst_pos, final int length) {
0793: return (float[]) doInsert(src, src_pos, dst, dst_pos, length);
0794: }
0795:
0796: /**
0797: * Inserts a table slice into another table. The {@code src} table
0798: * will be entirely or partially inserted into the {@code dst} table.
0799: *
0800: * @param src Tablea to insert into {@code dst}.
0801: * @param src_pos Index of the first data item of {@code src} to
0802: * insert into {@code dst}.
0803: * @param dst Table in which to insert {@code src} data.
0804: * @param dst_pos {@code dst} index in which to insert
0805: * {@code src} data. All elements of
0806: * {@code dst} whose index is equal to or greater than
0807: * {@code dst_pos} will be moved forward.
0808: * @param length Number of {@code src} data items to insert.
0809: * @return Table which contains the combination of {@code src}
0810: * and {@code dst}. This method can directly return
0811: * {@code dst}, but never {@code src}. It most
0812: * often returns a newly created table.
0813: */
0814: public static long[] insert(final long[] src, final int src_pos,
0815: final long[] dst, final int dst_pos, final int length) {
0816: return (long[]) doInsert(src, src_pos, dst, dst_pos, length);
0817: }
0818:
0819: /**
0820: * Inserts a table slice into another table. The {@code src} table
0821: * will be entirely or partially inserted into the {@code dst} table.
0822: *
0823: * @param src Tablea to insert into {@code dst}.
0824: * @param src_pos Index of the first data item of {@code src} to
0825: * insert into {@code dst}.
0826: * @param dst Table in which to insert {@code src} data.
0827: * @param dst_pos {@code dst} index in which to insert
0828: * {@code src} data. All elements of
0829: * {@code dst} whose index is equal to or greater than
0830: * {@code dst_pos} will be moved forward.
0831: * @param length Number of {@code src} data items to insert.
0832: * @return Table which contains the combination of {@code src}
0833: * and {@code dst}. This method can directly return
0834: * {@code dst}, but never {@code src}. It most
0835: * often returns a newly created table.
0836: */
0837: public static int[] insert(final int[] src, final int src_pos,
0838: final int[] dst, final int dst_pos, final int length) {
0839: return (int[]) doInsert(src, src_pos, dst, dst_pos, length);
0840: }
0841:
0842: /**
0843: * Inserts a table slice into another table. The {@code src} table
0844: * will be entirely or partially inserted into the {@code dst} table.
0845: *
0846: * @param src Tablea to insert into {@code dst}.
0847: * @param src_pos Index of the first data item of {@code src} to
0848: * insert into {@code dst}.
0849: * @param dst Table in which to insert {@code src} data.
0850: * @param dst_pos {@code dst} index in which to insert
0851: * {@code src} data. All elements of
0852: * {@code dst} whose index is equal to or greater than
0853: * {@code dst_pos} will be moved forward.
0854: * @param length Number of {@code src} data items to insert.
0855: * @return Table which contains the combination of {@code src}
0856: * and {@code dst}. This method can directly return
0857: * {@code dst}, but never {@code src}. It most
0858: * often returns a newly created table.
0859: */
0860: public static short[] insert(final short[] src, final int src_pos,
0861: final short[] dst, final int dst_pos, final int length) {
0862: return (short[]) doInsert(src, src_pos, dst, dst_pos, length);
0863: }
0864:
0865: /**
0866: * Inserts a table slice into another table. The {@code src} table
0867: * will be entirely or partially inserted into the {@code dst} table.
0868: *
0869: * @param src Tablea to insert into {@code dst}.
0870: * @param src_pos Index of the first data item of {@code src} to
0871: * insert into {@code dst}.
0872: * @param dst Table in which to insert {@code src} data.
0873: * @param dst_pos {@code dst} index in which to insert
0874: * {@code src} data. All elements of
0875: * {@code dst} whose index is equal to or greater than
0876: * {@code dst_pos} will be moved forward.
0877: * @param length Number of {@code src} data items to insert.
0878: * @return Table which contains the combination of {@code src}
0879: * and {@code dst}. This method can directly return
0880: * {@code dst}, but never {@code src}. It most
0881: * often returns a newly created table.
0882: */
0883: public static byte[] insert(final byte[] src, final int src_pos,
0884: final byte[] dst, final int dst_pos, final int length) {
0885: return (byte[]) doInsert(src, src_pos, dst, dst_pos, length);
0886: }
0887:
0888: /**
0889: * Inserts a table slice into another table. The {@code src} table
0890: * will be entirely or partially inserted into the {@code dst} table.
0891: *
0892: * @param src Tablea to insert into {@code dst}.
0893: * @param src_pos Index of the first data item of {@code src} to
0894: * insert into {@code dst}.
0895: * @param dst Table in which to insert {@code src} data.
0896: * @param dst_pos {@code dst} index in which to insert
0897: * {@code src} data. All elements of
0898: * {@code dst} whose index is equal to or greater than
0899: * {@code dst_pos} will be moved forward.
0900: * @param length Number of {@code src} data items to insert.
0901: * @return Table which contains the combination of {@code src}
0902: * and {@code dst}. This method can directly return
0903: * {@code dst}, but never {@code src}. It most
0904: * often returns a newly created table.
0905: */
0906: public static char[] insert(final char[] src, final int src_pos,
0907: final char[] dst, final int dst_pos, final int length) {
0908: return (char[]) doInsert(src, src_pos, dst, dst_pos, length);
0909: }
0910:
0911: /**
0912: * Inserts a table slice into another table. The {@code src} table
0913: * will be entirely or partially inserted into the {@code dst} table.
0914: *
0915: * @param src Tablea to insert into {@code dst}.
0916: * @param src_pos Index of the first data item of {@code src} to
0917: * insert into {@code dst}.
0918: * @param dst Table in which to insert {@code src} data.
0919: * @param dst_pos {@code dst} index in which to insert
0920: * {@code src} data. All elements of
0921: * {@code dst} whose index is equal to or greater than
0922: * {@code dst_pos} will be moved forward.
0923: * @param length Number of {@code src} data items to insert.
0924: * @return Table which contains the combination of {@code src}
0925: * and {@code dst}. This method can directly return
0926: * {@code dst}, but never {@code src}. It most
0927: * often returns a newly created table.
0928: */
0929: public static boolean[] insert(final boolean[] src,
0930: final int src_pos, final boolean[] dst, final int dst_pos,
0931: final int length) {
0932: return (boolean[]) doInsert(src, src_pos, dst, dst_pos, length);
0933: }
0934:
0935: /**
0936: * Returns {@code true} if all elements in the specified array are in increasing order.
0937: * This method is usefull in assertions.
0938: */
0939: public static boolean isSorted(final char[] array) {
0940: for (int i = 1; i < array.length; i++)
0941: if (array[i] < array[i - 1])
0942: return false;
0943: return true;
0944: }
0945:
0946: /**
0947: * Returns {@code true} if all elements in the specified array are in increasing order.
0948: * This method is usefull in assertions.
0949: */
0950: public static boolean isSorted(final byte[] array) {
0951: for (int i = 1; i < array.length; i++)
0952: if (array[i] < array[i - 1])
0953: return false;
0954: return true;
0955: }
0956:
0957: /**
0958: * Returns {@code true} if all elements in the specified array are in increasing order.
0959: * This method is usefull in assertions.
0960: */
0961: public static boolean isSorted(final short[] array) {
0962: for (int i = 1; i < array.length; i++)
0963: if (array[i] < array[i - 1])
0964: return false;
0965: return true;
0966: }
0967:
0968: /**
0969: * Returns {@code true} if all elements in the specified array are in increasing order.
0970: * This method is usefull in assertions.
0971: */
0972: public static boolean isSorted(final int[] array) {
0973: for (int i = 1; i < array.length; i++)
0974: if (array[i] < array[i - 1])
0975: return false;
0976: return true;
0977: }
0978:
0979: /**
0980: * Returns {@code true} if all elements in the specified array are in increasing order.
0981: * This method is usefull in assertions.
0982: */
0983: public static boolean isSorted(final long[] array) {
0984: for (int i = 1; i < array.length; i++)
0985: if (array[i] < array[i - 1])
0986: return false;
0987: return true;
0988: }
0989:
0990: /**
0991: * Returns {@code true} if all elements in the specified array are in increasing order.
0992: * Since {@code NaN} values are unordered, they may appears anywhere in the array; they
0993: * will be ignored. This method is usefull in assertions.
0994: */
0995: public static boolean isSorted(final float[] array) {
0996: int previous = 0;
0997: for (int i = 1; i < array.length; i++) {
0998: final float value = array[i];
0999: if (value < array[previous]) {
1000: return false;
1001: }
1002: if (!Float.isNaN(value)) {
1003: previous = i;
1004: }
1005: }
1006: return true;
1007: }
1008:
1009: /**
1010: * Returns {@code true} if all elements in the specified array are in increasing order.
1011: * Since {@code NaN} values are unordered, they may appears anywhere in the array; they
1012: * will be ignored. This method is usefull in assertions.
1013: */
1014: public static boolean isSorted(final double[] array) {
1015: int previous = 0;
1016: for (int i = 1; i < array.length; i++) {
1017: final double value = array[i];
1018: if (value < array[previous]) {
1019: return false;
1020: }
1021: if (!Double.isNaN(value)) {
1022: previous = i;
1023: }
1024: }
1025: return true;
1026: }
1027:
1028: /**
1029: * Returns {@code true} if all elements in the specified array are in strictly increasing order.
1030: * This method is usefull in assertions.
1031: */
1032: public static boolean isStrictlySorted(final int[] array) {
1033: for (int i = 1; i < array.length; i++)
1034: if (array[i] <= array[i - 1])
1035: return false;
1036: return true;
1037: }
1038:
1039: /**
1040: * Returns {@code true} if the specified array contains at least one
1041: * {@link Double#NaN NaN} value.
1042: */
1043: public static boolean hasNaN(final double[] array) {
1044: for (int i = 0; i < array.length; i++) {
1045: if (Double.isNaN(array[i])) {
1046: return true;
1047: }
1048: }
1049: return false;
1050: }
1051:
1052: /**
1053: * Returns {@code true} if the specified array contains at least one
1054: * {@link Float#NaN NaN} value.
1055: */
1056: public static boolean hasNaN(final float[] array) {
1057: for (int i = 0; i < array.length; i++) {
1058: if (Float.isNaN(array[i])) {
1059: return true;
1060: }
1061: }
1062: return false;
1063: }
1064:
1065: /**
1066: * Returns a string representation of an array of numbers. Current implementation
1067: * supports only primitive or subclasses of {@link Number}.
1068: *
1069: * @param array The array to format.
1070: * @param locale The locale for formatting.
1071: * @return The formatted array.
1072: *
1073: * @todo The separator should be local-dependent.
1074: * @todo Should we implements this functionality in {@link org.geotools.io.LineFormat} instead?
1075: */
1076: public static String toString(final Object array,
1077: final Locale locale) {
1078: final StringBuffer buffer = new StringBuffer();
1079: final NumberFormat format = NumberFormat
1080: .getNumberInstance(locale);
1081: final FieldPosition dummy = new FieldPosition(0);
1082: final int length = Array.getLength(array);
1083: for (int i = 0; i < length; i++) {
1084: if (i != 0) {
1085: buffer.append(", "); // TODO: the separator should be local-dependent.
1086: }
1087: format.format(Array.get(array, i), buffer, dummy);
1088: }
1089: return buffer.toString();
1090: }
1091: }
|