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 4.4KB

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