Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / util / ArrayUtil.java
bloba4000d29ec8c37c438ba1ffbaf923284b36cc98a
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.util;
20 /**
21 * Utility classes for dealing with arrays.
23 * @author Glen Stampoultzis
24 * @version $Id$
26 public class ArrayUtil
28 /**
29 * This is really a debugging version of <code>System.arraycopy()</code>.
30 * Use it to provide better exception messages when copying arrays around.
31 * For production use it's better to use the original for speed.
33 public static void arraycopy(byte[] src, int src_position, byte[] dst, int dst_position, int length)
35 if (src_position < 0)
36 throw new IllegalArgumentException("src_position was less than 0. Actual value " + src_position);
37 if (src_position >= src.length)
38 throw new IllegalArgumentException( "src_position was greater than src array size. Tried to write starting at position " + src_position + " but the array length is " + src.length );
39 if (src_position + length > src.length)
40 throw new IllegalArgumentException("src_position + length would overrun the src array. Expected end at " + (src_position + length) + " actual end at " + src.length);
41 if (dst_position < 0)
42 throw new IllegalArgumentException("dst_position was less than 0. Actual value " + dst_position);
43 if (dst_position >= dst.length)
44 throw new IllegalArgumentException( "dst_position was greater than dst array size. Tried to write starting at position " + dst_position + " but the array length is " + dst.length );
45 if (dst_position + length > dst.length)
46 throw new IllegalArgumentException("dst_position + length would overrun the dst array. Expected end at " + (dst_position + length) + " actual end at " + dst.length);
48 System.arraycopy( src, src_position, dst, dst_position, length);
51 /**
52 * Moves a number of entries in an array to another point in the array,
53 * shifting those inbetween as required.
54 * @param array The array to alter
55 * @param moveFrom The (0 based) index of the first entry to move
56 * @param moveTo The (0 based) index of the positition to move to
57 * @param numToMove The number of entries to move
59 public static void arrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove) {
60 // If we're not asked to do anything, return now
61 if(numToMove <= 0) { return; }
62 if(moveFrom == moveTo) { return; }
64 // Check that the values supplied are valid
65 if(moveFrom < 0 || moveFrom >= array.length) {
66 throw new IllegalArgumentException("The moveFrom must be a valid array index");
68 if(moveTo < 0 || moveTo >= array.length) {
69 throw new IllegalArgumentException("The moveTo must be a valid array index");
71 if(moveFrom+numToMove > array.length) {
72 throw new IllegalArgumentException("Asked to move more entries than the array has");
74 if(moveTo+numToMove > array.length) {
75 throw new IllegalArgumentException("Asked to move to a position that doesn't have enough space");
78 // Grab the bit to move
79 Object[] toMove = new Object[numToMove];
80 System.arraycopy(array, moveFrom, toMove, 0, numToMove);
82 // Grab the bit to be shifted
83 Object[] toShift;
84 int shiftTo;
85 if(moveFrom > moveTo) {
86 // Moving to an earlier point in the array
87 // Grab everything between the two points
88 toShift = new Object[(moveFrom-moveTo)];
89 System.arraycopy(array, moveTo, toShift, 0, toShift.length);
90 shiftTo = moveTo + numToMove;
91 } else {
92 // Moving to a later point in the array
93 // Grab everything from after the toMove block, to the new point
94 toShift = new Object[(moveTo-moveFrom)];
95 System.arraycopy(array, moveFrom+numToMove, toShift, 0, toShift.length);
96 shiftTo = moveFrom;
99 // Copy the moved block to its new location
100 System.arraycopy(toMove, 0, array, moveTo, toMove.length);
102 // And copy the shifted block to the shifted location
103 System.arraycopy(toShift, 0, array, shiftTo, toShift.length);
106 // We're done - array will now have everything moved as required