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.

SSCellRange.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.ss.util;
  16. import java.lang.reflect.Array;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.stream.Stream;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellRange;
  22. import org.apache.poi.util.Internal;
  23. /**
  24. * For POI internal use only
  25. */
  26. @Internal
  27. public final class SSCellRange<K extends Cell> implements CellRange<K> {
  28. private final int _height;
  29. private final int _width;
  30. private final K[] _flattenedArray;
  31. private final int _firstRow;
  32. private final int _firstColumn;
  33. private SSCellRange(int firstRow, int firstColumn, int height, int width, K[] flattenedArray) {
  34. _firstRow = firstRow;
  35. _firstColumn = firstColumn;
  36. _height = height;
  37. _width = width;
  38. _flattenedArray = flattenedArray.clone();
  39. }
  40. public static <B extends Cell> SSCellRange<B> create(int firstRow, int firstColumn, int height, int width, List<B> flattenedList, Class<B> cellClass) {
  41. int nItems = flattenedList.size();
  42. if (height * width != nItems) {
  43. throw new IllegalArgumentException("Array size mismatch.");
  44. }
  45. @SuppressWarnings("unchecked")
  46. B[] flattenedArray = (B[]) Array.newInstance(cellClass, nItems);
  47. flattenedList.toArray(flattenedArray);
  48. return new SSCellRange<>(firstRow, firstColumn, height, width, flattenedArray);
  49. }
  50. public int getHeight() {
  51. return _height;
  52. }
  53. public int getWidth() {
  54. return _width;
  55. }
  56. public int size() {
  57. return _height*_width;
  58. }
  59. public String getReferenceText() {
  60. CellRangeAddress cra = new CellRangeAddress(_firstRow, _firstRow+_height-1, _firstColumn, _firstColumn+_width-1);
  61. return cra.formatAsString();
  62. }
  63. public K getTopLeftCell() {
  64. return _flattenedArray[0];
  65. }
  66. public K getCell(int relativeRowIndex, int relativeColumnIndex) {
  67. if (relativeRowIndex < 0 || relativeRowIndex >= _height) {
  68. throw new ArrayIndexOutOfBoundsException("Specified row " + relativeRowIndex
  69. + " is outside the allowable range (0.." + (_height-1) + ").");
  70. }
  71. if (relativeColumnIndex < 0 || relativeColumnIndex >= _width) {
  72. throw new ArrayIndexOutOfBoundsException("Specified colummn " + relativeColumnIndex
  73. + " is outside the allowable range (0.." + (_width-1) + ").");
  74. }
  75. int flatIndex = _width * relativeRowIndex + relativeColumnIndex;
  76. return _flattenedArray[flatIndex];
  77. }
  78. public K[] getFlattenedCells() {
  79. return _flattenedArray.clone();
  80. }
  81. public K[][] getCells() {
  82. Class<?> itemCls = _flattenedArray.getClass();
  83. @SuppressWarnings("unchecked")
  84. K[][] result = (K[][]) Array.newInstance(itemCls, _height);
  85. itemCls = itemCls.getComponentType();
  86. for (int r=_height-1; r>=0; r--) {
  87. @SuppressWarnings("unchecked")
  88. K[] row = (K[]) Array.newInstance(itemCls, _width);
  89. int flatIndex = _width * r;
  90. System.arraycopy(_flattenedArray, flatIndex, row, 0, _width);
  91. }
  92. return result;
  93. }
  94. public Iterator<K> iterator() {
  95. return Stream.of(_flattenedArray).iterator();
  96. }
  97. }