001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: /**
021: * Utility classes for dealing with arrays.
022: *
023: * @author Glen Stampoultzis
024: * @version $Id: ArrayUtil.java 489730 2006-12-22 19:18:16Z bayard $
025: */
026: public class ArrayUtil {
027: /**
028: * This is really a debugging version of <code>System.arraycopy()</code>.
029: * Use it to provide better exception messages when copying arrays around.
030: * For production use it's better to use the original for speed.
031: */
032: public static void arraycopy(byte[] src, int src_position,
033: byte[] dst, int dst_position, int length) {
034: if (src_position < 0)
035: throw new IllegalArgumentException(
036: "src_position was less than 0. Actual value "
037: + src_position);
038: if (src_position >= src.length)
039: throw new IllegalArgumentException(
040: "src_position was greater than src array size. Tried to write starting at position "
041: + src_position
042: + " but the array length is " + src.length);
043: if (src_position + length > src.length)
044: throw new IllegalArgumentException(
045: "src_position + length would overrun the src array. Expected end at "
046: + (src_position + length)
047: + " actual end at " + src.length);
048: if (dst_position < 0)
049: throw new IllegalArgumentException(
050: "dst_position was less than 0. Actual value "
051: + dst_position);
052: if (dst_position >= dst.length)
053: throw new IllegalArgumentException(
054: "dst_position was greater than dst array size. Tried to write starting at position "
055: + dst_position
056: + " but the array length is " + dst.length);
057: if (dst_position + length > dst.length)
058: throw new IllegalArgumentException(
059: "dst_position + length would overrun the dst array. Expected end at "
060: + (dst_position + length)
061: + " actual end at " + dst.length);
062:
063: System.arraycopy(src, src_position, dst, dst_position, length);
064: }
065:
066: /**
067: * Moves a number of entries in an array to another point in the array,
068: * shifting those inbetween as required.
069: * @param array The array to alter
070: * @param moveFrom The (0 based) index of the first entry to move
071: * @param moveTo The (0 based) index of the positition to move to
072: * @param numToMove The number of entries to move
073: */
074: public static void arrayMoveWithin(Object[] array, int moveFrom,
075: int moveTo, int numToMove) {
076: // If we're not asked to do anything, return now
077: if (numToMove <= 0) {
078: return;
079: }
080: if (moveFrom == moveTo) {
081: return;
082: }
083:
084: // Check that the values supplied are valid
085: if (moveFrom < 0 || moveFrom >= array.length) {
086: throw new IllegalArgumentException(
087: "The moveFrom must be a valid array index");
088: }
089: if (moveTo < 0 || moveTo >= array.length) {
090: throw new IllegalArgumentException(
091: "The moveTo must be a valid array index");
092: }
093: if (moveFrom + numToMove > array.length) {
094: throw new IllegalArgumentException(
095: "Asked to move more entries than the array has");
096: }
097: if (moveTo + numToMove > array.length) {
098: throw new IllegalArgumentException(
099: "Asked to move to a position that doesn't have enough space");
100: }
101:
102: // Grab the bit to move
103: Object[] toMove = new Object[numToMove];
104: System.arraycopy(array, moveFrom, toMove, 0, numToMove);
105:
106: // Grab the bit to be shifted
107: Object[] toShift;
108: int shiftTo;
109: if (moveFrom > moveTo) {
110: // Moving to an earlier point in the array
111: // Grab everything between the two points
112: toShift = new Object[(moveFrom - moveTo)];
113: System.arraycopy(array, moveTo, toShift, 0, toShift.length);
114: shiftTo = moveTo + numToMove;
115: } else {
116: // Moving to a later point in the array
117: // Grab everything from after the toMove block, to the new point
118: toShift = new Object[(moveTo - moveFrom)];
119: System.arraycopy(array, moveFrom + numToMove, toShift, 0,
120: toShift.length);
121: shiftTo = moveFrom;
122: }
123:
124: // Copy the moved block to its new location
125: System.arraycopy(toMove, 0, array, moveTo, toMove.length);
126:
127: // And copy the shifted block to the shifted location
128: System.arraycopy(toShift, 0, array, shiftTo, toShift.length);
129:
130: // We're done - array will now have everything moved as required
131: }
132: }
|