bug 59729: update CellRangeUtil

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749243 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-20 00:55:51 +00:00
parent a1156bc7ba
commit bfa21bc5fa
1 changed files with 48 additions and 34 deletions

View File

@ -20,6 +20,11 @@ package org.apache.poi.ss.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Utility class that builds on {@link CellRangeAddress}
*
* Portions of this class may be moved to {@link CellRangeAddressBase}
*/
public final class CellRangeUtil { public final class CellRangeUtil {
private CellRangeUtil() { private CellRangeUtil() {
// no instance of this class // no instance of this class
@ -33,15 +38,18 @@ public final class CellRangeUtil {
public static final int ENCLOSES = 4; public static final int ENCLOSES = 4;
/** /**
* Intersect this range with the specified range. * Get the type of intersection between two cell ranges
* *
* @param crB - the specified range * @param crB - the specified range
* @return code which reflects how the specified range is related to this range.<br/> * @return code which reflects how the specified range is related to this range.<br/>
* Possible return codes are: * Possible return codes are:
* NO_INTERSECTION - the specified range is outside of this range;<br/> * <ul>
* OVERLAP - both ranges partially overlap;<br/> * <li>{@link #NO_INTERSECTION} - the specified range is outside of this range;</li>
* INSIDE - the specified range is inside of this one<br/> * <li>{@link #OVERLAP} - both ranges partially overlap</li>
* ENCLOSES - the specified range encloses (possibly exactly the same as) this range<br/> * <li>{@link #INSIDE} - the specified range is inside of this one</li>
* <li>{@link #ENCLOSES} - the specified range encloses (possibly exactly the same as) this range</li>
* </ul>
* @see CellRangeAddressBase#intersects(CellRangeAddressBase)
*/ */
public static int intersect(CellRangeAddress crA, CellRangeAddress crB ) public static int intersect(CellRangeAddress crA, CellRangeAddress crB )
{ {
@ -73,22 +81,20 @@ public final class CellRangeUtil {
} }
/** /**
* Do all possible cell merges between cells of the list so that:<br> * Do all possible cell merges between cells of the list so that:<br/>
* <li>if a cell range is completely inside of another cell range, it gets removed from the list * <ul>
* <li>if two cells have a shared border, merge them into one bigger cell range * <li>if a cell range is completely inside of another cell range, it gets removed from the list</li>
* @param cellRanges * <li>if two cells have a shared border, merge them into one bigger cell range</li>
* @return updated List of cell ranges * </ul>
* @param cellRanges the ranges to merge
* @return list of merged cell ranges
*/ */
public static CellRangeAddress[] mergeCellRanges(CellRangeAddress[] cellRanges) { public static CellRangeAddress[] mergeCellRanges(CellRangeAddress[] cellRanges) {
if(cellRanges.length < 1) { if(cellRanges.length < 1) {
return cellRanges; return new CellRangeAddress[] {};
} }
List<CellRangeAddress> list = toList(cellRanges);
List<CellRangeAddress> lst = new ArrayList<CellRangeAddress>(); List<CellRangeAddress> temp = mergeCellRanges(list);
for(CellRangeAddress cr : cellRanges) {
lst.add(cr);
}
List<CellRangeAddress> temp = mergeCellRanges(lst);
return toArray(temp); return toArray(temp);
} }
@ -164,21 +170,29 @@ public final class CellRangeUtil {
temp.toArray(result); temp.toArray(result);
return result; return result;
} }
private static List<CellRangeAddress> toList(CellRangeAddress[] temp) {
List<CellRangeAddress> result = new ArrayList<CellRangeAddress>(temp.length);
for (CellRangeAddress range : temp) {
result.add(range);
}
return result;
}
/** /**
* Check if the specified range is located inside of this cell range. * Check if cell range A contains cell range B (B <= A)
* *
* @param crB * TODO: move this into {@link CellRangeAddressBase}
* @return true if this cell range contains the argument range inside if it's area *
* @param crA cell range A
* @param crB cell range B
* @return true if cell range A contains cell range B
*/ */
public static boolean contains(CellRangeAddress crA, CellRangeAddress crB) public static boolean contains(CellRangeAddress crA, CellRangeAddress crB)
{ {
int firstRow = crB.getFirstRow(); return le(crA.getFirstRow(), crB.getFirstRow()) &&
int lastRow = crB.getLastRow(); ge(crA.getLastRow(), crB.getLastRow()) &&
int firstCol = crB.getFirstColumn(); le(crA.getFirstColumn(), crB.getFirstColumn()) &&
int lastCol = crB.getLastColumn(); ge(crA.getLastColumn(), crB.getLastColumn());
return le(crA.getFirstRow(), firstRow) && ge(crA.getLastRow(), lastRow)
&& le(crA.getFirstColumn(), firstCol) && ge(crA.getLastColumn(), lastCol);
} }
/** /**
@ -218,13 +232,13 @@ public final class CellRangeUtil {
if( crB == null) { if( crB == null) {
return crA.copy(); return crA.copy();
} }
int minRow = lt(crB.getFirstRow(), crA.getFirstRow()) ?crB.getFirstRow() :crA.getFirstRow();
int maxRow = gt(crB.getLastRow(), crA.getLastRow()) ?crB.getLastRow() :crA.getLastRow();
int minCol = lt(crB.getFirstColumn(),crA.getFirstColumn())?crB.getFirstColumn():crA.getFirstColumn();
int maxCol = gt(crB.getLastColumn(), crA.getLastColumn()) ?crB.getLastColumn() :crA.getLastColumn();
return new CellRangeAddress( return new CellRangeAddress(minRow, maxRow, minCol, maxCol);
lt(crB.getFirstRow(), crA.getFirstRow()) ?crB.getFirstRow() :crA.getFirstRow(),
gt(crB.getLastRow(), crA.getLastRow()) ?crB.getLastRow() :crA.getLastRow(),
lt(crB.getFirstColumn(),crA.getFirstColumn())?crB.getFirstColumn():crA.getFirstColumn(),
gt(crB.getLastColumn(), crA.getLastColumn()) ?crB.getLastColumn() :crA.getLastColumn()
);
} }
/** /**