You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayUtil.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.util.Arrays;
  17. /**
  18. * Utility classes for dealing with arrays.
  19. */
  20. @Internal
  21. public final class ArrayUtil {
  22. private ArrayUtil() {}
  23. /**
  24. * Moves a number of entries in an array to another point in the array,
  25. * shifting those inbetween as required.
  26. * @param array The array to alter
  27. * @param moveFrom The (0 based) index of the first entry to move
  28. * @param moveTo The (0 based) index of the positition to move to
  29. * @param numToMove The number of entries to move
  30. */
  31. public static void arrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove) {
  32. // If we're not asked to do anything, return now
  33. if(numToMove <= 0) { return; }
  34. if(moveFrom == moveTo) { return; }
  35. // Check that the values supplied are valid
  36. if(moveFrom < 0 || moveFrom >= array.length) {
  37. throw new IllegalArgumentException("The moveFrom must be a valid array index");
  38. }
  39. if(moveTo < 0 || moveTo >= array.length) {
  40. throw new IllegalArgumentException("The moveTo must be a valid array index");
  41. }
  42. if(moveFrom+numToMove > array.length) {
  43. throw new IllegalArgumentException("Asked to move more entries than the array has");
  44. }
  45. if(moveTo+numToMove > array.length) {
  46. throw new IllegalArgumentException("Asked to move to a position that doesn't have enough space");
  47. }
  48. // Grab the bit to move
  49. Object[] toMove = Arrays.copyOfRange(array, moveFrom, moveFrom+numToMove);
  50. // Grab the bit to be shifted
  51. Object[] toShift;
  52. int shiftTo;
  53. if(moveFrom > moveTo) {
  54. // Moving to an earlier point in the array
  55. // Grab everything between the two points
  56. toShift = Arrays.copyOfRange(array, moveTo, moveFrom);
  57. shiftTo = moveTo + numToMove;
  58. } else {
  59. // Moving to a later point in the array
  60. // Grab everything from after the toMove block, to the new point
  61. toShift = Arrays.copyOfRange(array, moveFrom+numToMove, moveTo+numToMove);
  62. shiftTo = moveFrom;
  63. }
  64. // Copy the moved block to its new location
  65. System.arraycopy(toMove, 0, array, moveTo, toMove.length);
  66. // And copy the shifted block to the shifted location
  67. System.arraycopy(toShift, 0, array, shiftTo, toShift.length);
  68. // We're done - array will now have everything moved as required
  69. }
  70. }