bug 61730: remove CellRangeAddressBase which is eager. The lazy iterator is safer, less likely to cause an OOM/DoS.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814461 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
841cc75ee4
commit
163673a7aa
@ -282,26 +282,7 @@ public abstract class CellRangeAddressBase implements Iterable<CellAddress> {
|
|||||||
public int getNumberOfCells() {
|
public int getNumberOfCells() {
|
||||||
return (_lastRow - _firstRow + 1) * (_lastCol - _firstCol + 1);
|
return (_lastRow - _firstRow + 1) * (_lastCol - _firstCol + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CellAddress> getCellAddresses(boolean rowMajorOrder) {
|
|
||||||
List<CellAddress> addresses = new ArrayList<>();
|
|
||||||
if (rowMajorOrder) {
|
|
||||||
for (int r = _firstRow; r <= _lastRow; r++) {
|
|
||||||
for (int c = _firstCol; c <= _lastCol; c++) {
|
|
||||||
addresses.add(new CellAddress(r, c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (int c = _firstCol; c <= _lastCol; c++) {
|
|
||||||
for (int r = _firstRow; r <= _lastRow; r++) {
|
|
||||||
addresses.add(new CellAddress(r, c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableList(addresses);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an iterator over the CellAddresses in this cell range in row-major order.
|
* Returns an iterator over the CellAddresses in this cell range in row-major order.
|
||||||
* @since POI 4.0.0
|
* @since POI 4.0.0
|
||||||
|
@ -21,6 +21,10 @@ import org.junit.Test;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assume.assumeTrue;
|
import static org.junit.Assume.assumeTrue;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import org.apache.commons.collections4.IteratorUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests CellRangeUtil.
|
* Tests CellRangeUtil.
|
||||||
@ -47,23 +51,40 @@ public final class TestCellRangeUtil {
|
|||||||
// A B
|
// A B
|
||||||
// 1 x x A1,A2,B1,B2 --> A1:B2
|
// 1 x x A1,A2,B1,B2 --> A1:B2
|
||||||
// 2 x x
|
// 2 x x
|
||||||
assertArrayEquals(asArray(A1_B2), merge(A1, B1, A2, B2));
|
assertCellRangesEqual(asArray(A1_B2), merge(A1, B1, A2, B2));
|
||||||
assertArrayEquals(asArray(A1_B2), merge(A1, B2, A2, B1));
|
assertCellRangesEqual(asArray(A1_B2), merge(A1, B2, A2, B1));
|
||||||
|
|
||||||
// Partially mergeable: multiple possible mergings
|
// Partially mergeable: multiple possible mergings
|
||||||
// A B
|
// A B
|
||||||
// 1 x x A1,A2,B1 --> A1:B1,A2 or A1:A2,B1
|
// 1 x x A1,A2,B1 --> A1:B1,A2 or A1:A2,B1
|
||||||
// 2 x
|
// 2 x
|
||||||
assertArrayEquals(asArray(A1_B1, A2), merge(A1, B1, A2));
|
assertCellRangesEqual(asArray(A1_B1, A2), merge(A1, B1, A2));
|
||||||
assertArrayEquals(asArray(A1_A2, B1), merge(A2, A1, B1));
|
assertCellRangesEqual(asArray(A1_A2, B1), merge(A2, A1, B1));
|
||||||
assertArrayEquals(asArray(A1_B1, A2), merge(B1, A2, A1));
|
assertCellRangesEqual(asArray(A1_B1, A2), merge(B1, A2, A1));
|
||||||
|
|
||||||
// Not mergeable
|
// Not mergeable
|
||||||
// A B
|
// A B
|
||||||
// 1 x A1,B2 --> A1,B2
|
// 1 x A1,B2 --> A1,B2
|
||||||
// 2 x
|
// 2 x
|
||||||
assertArrayEquals(asArray(A1, B2), merge(A1, B2));
|
assertCellRangesEqual(asArray(A1, B2), merge(A1, B2));
|
||||||
assertArrayEquals(asArray(B2, A1), merge(B2, A1));
|
assertCellRangesEqual(asArray(B2, A1), merge(B2, A1));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertCellRangesEqual(CellRangeAddress[] a, CellRangeAddress[] b) {
|
||||||
|
assertEquals(getCellAddresses(a), getCellAddresses(b));
|
||||||
|
assertArrayEquals(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<CellAddress> getCellAddresses(CellRangeAddress[] ranges) {
|
||||||
|
final Set<CellAddress> set = new HashSet<>();
|
||||||
|
for (final CellRangeAddress range : ranges) {
|
||||||
|
set.addAll(asSet(range.iterator()));
|
||||||
|
}
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> Set<T> asSet(Iterator<T> iterator) {
|
||||||
|
return new HashSet<T>(IteratorUtils.toList(iterator));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> T[] asArray(T...ts) {
|
private static <T> T[] asArray(T...ts) {
|
||||||
|
Loading…
Reference in New Issue
Block a user