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.

CellRangeUtil.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.hssf.record.cf;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. /**
  20. * TODO Should this move to org.apache.poi.ss.util ?
  21. */
  22. public final class CellRangeUtil {
  23. private CellRangeUtil() {
  24. // no instance of this class
  25. }
  26. public static final int NO_INTERSECTION = 1;
  27. public static final int OVERLAP = 2;
  28. /** first range is within the second range */
  29. public static final int INSIDE = 3;
  30. /** first range encloses or is equal to the second */
  31. public static final int ENCLOSES = 4;
  32. /**
  33. * Intersect this range with the specified range.
  34. *
  35. * @param crB - the specified range
  36. * @return code which reflects how the specified range is related to this range.<br/>
  37. * Possible return codes are:
  38. * NO_INTERSECTION - the specified range is outside of this range;<br/>
  39. * OVERLAP - both ranges partially overlap;<br/>
  40. * INSIDE - the specified range is inside of this one<br/>
  41. * ENCLOSES - the specified range encloses (possibly exactly the same as) this range<br/>
  42. */
  43. public static int intersect(CellRangeAddress crA, CellRangeAddress crB )
  44. {
  45. int firstRow = crB.getFirstRow();
  46. int lastRow = crB.getLastRow();
  47. int firstCol = crB.getFirstColumn();
  48. int lastCol = crB.getLastColumn();
  49. if
  50. (
  51. gt(crA.getFirstRow(),lastRow) ||
  52. lt(crA.getLastRow(),firstRow) ||
  53. gt(crA.getFirstColumn(),lastCol) ||
  54. lt(crA.getLastColumn(),firstCol)
  55. )
  56. {
  57. return NO_INTERSECTION;
  58. }
  59. else if( contains(crA, crB) )
  60. {
  61. return INSIDE;
  62. }
  63. else if( contains(crB, crA))
  64. {
  65. return ENCLOSES;
  66. }
  67. else
  68. {
  69. return OVERLAP;
  70. }
  71. }
  72. /**
  73. * Do all possible cell merges between cells of the list so that:<br>
  74. * <li>if a cell range is completely inside of another cell range, it gets removed from the list
  75. * <li>if two cells have a shared border, merge them into one bigger cell range
  76. * @param cellRanges
  77. * @return updated List of cell ranges
  78. */
  79. public static CellRangeAddress[] mergeCellRanges(CellRangeAddress[] cellRanges) {
  80. if(cellRanges.length < 1) {
  81. return cellRanges;
  82. }
  83. List<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();
  84. for(CellRangeAddress cr : cellRanges) {
  85. lst.add(cr);
  86. }
  87. List<CellRangeAddress> temp = mergeCellRanges(lst);
  88. return toArray(temp);
  89. }
  90. private static List<CellRangeAddress> mergeCellRanges(List<CellRangeAddress> cellRangeList)
  91. {
  92. // loop until either only one item is left or we did not merge anything any more
  93. while (cellRangeList.size() > 1) {
  94. boolean somethingGotMerged = false;
  95. // look at all cell-ranges
  96. for (int i = 0; i < cellRangeList.size(); i++) {
  97. CellRangeAddress range1 = cellRangeList.get(i);
  98. // compare each cell range to all other cell-ranges
  99. for (int j = i + 1; j < cellRangeList.size(); j++) {
  100. CellRangeAddress range2 = cellRangeList.get(j);
  101. CellRangeAddress[] mergeResult = mergeRanges(range1, range2);
  102. if (mergeResult == null) {
  103. continue;
  104. }
  105. somethingGotMerged = true;
  106. // overwrite range1 with first result
  107. cellRangeList.set(i, mergeResult[0]);
  108. // remove range2
  109. cellRangeList.remove(j--);
  110. // add any extra results beyond the first
  111. for (int k = 1; k < mergeResult.length; k++) {
  112. j++;
  113. cellRangeList.add(j, mergeResult[k]);
  114. }
  115. }
  116. }
  117. if (!somethingGotMerged) {
  118. break;
  119. }
  120. }
  121. return cellRangeList;
  122. }
  123. /**
  124. * @return the new range(s) to replace the supplied ones. <code>null</code> if no merge is possible
  125. */
  126. private static CellRangeAddress[] mergeRanges(CellRangeAddress range1, CellRangeAddress range2) {
  127. int x = intersect(range1, range2);
  128. switch(x)
  129. {
  130. case CellRangeUtil.NO_INTERSECTION:
  131. // nothing in common: at most they could be adjacent to each other and thus form a single bigger area
  132. if(hasExactSharedBorder(range1, range2)) {
  133. return new CellRangeAddress[] { createEnclosingCellRange(range1, range2), };
  134. }
  135. // else - No intersection and no shared border: do nothing
  136. return null;
  137. case CellRangeUtil.OVERLAP:
  138. // commented out the cells overlap implementation, it caused endless loops, see Bug 55380
  139. // disabled for now, the algorithm will not detect some border cases this way currently!
  140. //return resolveRangeOverlap(range1, range2);
  141. return null;
  142. case CellRangeUtil.INSIDE:
  143. // Remove range2, since it is completely inside of range1
  144. return new CellRangeAddress[] { range1 };
  145. case CellRangeUtil.ENCLOSES:
  146. // range2 encloses range1, so replace it with the enclosing one
  147. return new CellRangeAddress[] { range2 };
  148. }
  149. throw new RuntimeException("unexpected intersection result (" + x + ")");
  150. }
  151. private static CellRangeAddress[] toArray(List<CellRangeAddress> temp) {
  152. CellRangeAddress[] result = new CellRangeAddress[temp.size()];
  153. temp.toArray(result);
  154. return result;
  155. }
  156. /**
  157. * Check if the specified range is located inside of this cell range.
  158. *
  159. * @param crB
  160. * @return true if this cell range contains the argument range inside if it's area
  161. */
  162. public static boolean contains(CellRangeAddress crA, CellRangeAddress crB)
  163. {
  164. int firstRow = crB.getFirstRow();
  165. int lastRow = crB.getLastRow();
  166. int firstCol = crB.getFirstColumn();
  167. int lastCol = crB.getLastColumn();
  168. return le(crA.getFirstRow(), firstRow) && ge(crA.getLastRow(), lastRow)
  169. && le(crA.getFirstColumn(), firstCol) && ge(crA.getLastColumn(), lastCol);
  170. }
  171. /**
  172. * Check if the two cell ranges have a shared border.
  173. *
  174. * @return <code>true</code> if the ranges have a complete shared border (i.e.
  175. * the two ranges together make a simple rectangular region.
  176. */
  177. public static boolean hasExactSharedBorder(CellRangeAddress crA, CellRangeAddress crB) {
  178. int oFirstRow = crB.getFirstRow();
  179. int oLastRow = crB.getLastRow();
  180. int oFirstCol = crB.getFirstColumn();
  181. int oLastCol = crB.getLastColumn();
  182. if (crA.getFirstRow() > 0 && crA.getFirstRow()-1 == oLastRow ||
  183. oFirstRow > 0 && oFirstRow-1 == crA.getLastRow()) {
  184. // ranges have a horizontal border in common
  185. // make sure columns are identical:
  186. return crA.getFirstColumn() == oFirstCol && crA.getLastColumn() == oLastCol;
  187. }
  188. if (crA.getFirstColumn()>0 && crA.getFirstColumn() - 1 == oLastCol ||
  189. oFirstCol>0 && crA.getLastColumn() == oFirstCol -1) {
  190. // ranges have a vertical border in common
  191. // make sure rows are identical:
  192. return crA.getFirstRow() == oFirstRow && crA.getLastRow() == oLastRow;
  193. }
  194. return false;
  195. }
  196. /**
  197. * Create an enclosing CellRange for the two cell ranges.
  198. *
  199. * @return enclosing CellRange
  200. */
  201. public static CellRangeAddress createEnclosingCellRange(CellRangeAddress crA, CellRangeAddress crB) {
  202. if( crB == null) {
  203. return crA.copy();
  204. }
  205. return new CellRangeAddress(
  206. lt(crB.getFirstRow(), crA.getFirstRow()) ?crB.getFirstRow() :crA.getFirstRow(),
  207. gt(crB.getLastRow(), crA.getLastRow()) ?crB.getLastRow() :crA.getLastRow(),
  208. lt(crB.getFirstColumn(),crA.getFirstColumn())?crB.getFirstColumn():crA.getFirstColumn(),
  209. gt(crB.getLastColumn(), crA.getLastColumn()) ?crB.getLastColumn() :crA.getLastColumn()
  210. );
  211. }
  212. /**
  213. * @return true if a < b
  214. */
  215. private static boolean lt(int a, int b)
  216. {
  217. return a == -1 ? false : (b == -1 ? true : a < b);
  218. }
  219. /**
  220. * @return true if a <= b
  221. */
  222. private static boolean le(int a, int b)
  223. {
  224. return a == b || lt(a,b);
  225. }
  226. /**
  227. * @return true if a > b
  228. */
  229. private static boolean gt(int a, int b)
  230. {
  231. return lt(b,a);
  232. }
  233. /**
  234. * @return true if a >= b
  235. */
  236. private static boolean ge(int a, int b)
  237. {
  238. return !lt(a,b);
  239. }
  240. }