Skip to content

Commit b236546

Browse files
committed
refactor: add contains checks for indexes and add javadocs
1 parent 98e7b94 commit b236546

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed
Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,118 @@
1+
/*
2+
* Copyright 2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
116
package com.vaadin.flow.component.spreadsheet;
217

318
import org.apache.poi.ss.util.CellReference;
419

520
import java.util.Collections;
21+
import java.util.Objects;
622
import java.util.Set;
723

24+
/**
25+
* CellSet is a set of cells that also provides utilities regarding the contents
26+
* of the set.
27+
*/
828
public class CellSet {
929

1030
private final Set<CellReference> cells;
1131

32+
/**
33+
* Creates a set with the specified cells
34+
*
35+
* @param cells
36+
* cells to construct the set with, not {@code null}
37+
*/
1238
public CellSet(Set<CellReference> cells) {
39+
Objects.requireNonNull(cells, "Cells cannot be null");
1340
this.cells = cells;
1441
}
1542

43+
/**
44+
* Gets an unmodifiable set of the cells
45+
*
46+
* @return an unmodifiable set of the cells
47+
*/
1648
public Set<CellReference> getCells() {
1749
return Collections.unmodifiableSet(cells);
1850
}
1951

52+
/**
53+
* Gets the number of the cells
54+
*
55+
* @return number of cells
56+
*/
2057
public int getCellCount() {
2158
return cells.size();
2259
}
2360

24-
public boolean containsCell(CellReference cell) {
61+
/**
62+
* Whether the set contains the specified cell. Does not take the sheet
63+
* names of the cells in set into account if the sheet name of the cell
64+
* reference is {@code null}.
65+
*
66+
* @param cellReference
67+
* cell to be checked whether it exists in the set
68+
* @return {@code true} if set contains the specified cell, {@code false}
69+
* otherwise
70+
*/
71+
public boolean containsCell(CellReference cellReference) {
2572
if (cells.isEmpty()) {
2673
return false;
2774
}
28-
if (cell.getSheetName() == null) {
75+
if (cellReference.getSheetName() == null) {
2976
CellReference cellWithSheetName = new CellReference(
30-
cells.iterator().next().getSheetName(), cell.getRow(),
31-
cell.getCol(), cell.isRowAbsolute(), cell.isColAbsolute());
77+
cells.iterator().next().getSheetName(),
78+
cellReference.getRow(), cellReference.getCol(),
79+
cellReference.isRowAbsolute(),
80+
cellReference.isColAbsolute());
3281
return cells.contains(cellWithSheetName);
3382
}
34-
return cells.contains(cell);
83+
return cells.contains(cellReference);
84+
}
85+
86+
/**
87+
* Whether the set contains the specified cell. Does not take the sheet
88+
* names of the cells in set into account.
89+
*
90+
* @param row
91+
* row index of the cell, 0-based
92+
* @param col
93+
* col index of the cell, 0-based
94+
* @return {@code true} if set contains the specified cell, {@code false}
95+
* otherwise
96+
*/
97+
public boolean containsCell(int row, int col) {
98+
return containsCell(new CellReference(row, col));
99+
}
100+
101+
/**
102+
* Whether the set contains the specified cell
103+
*
104+
* @param row
105+
* row index of the cell, 0-based
106+
* @param col
107+
* col index of the cell, 0-based
108+
* @param sheetName
109+
* sheet name of the cell, not {@code null}
110+
* @return {@code true} if set contains the specified cell, {@code false}
111+
* otherwise
112+
*/
113+
public boolean containsCell(int row, int col, String sheetName) {
114+
Objects.requireNonNull(sheetName, "The sheet name cannot be null");
115+
return containsCell(
116+
new CellReference(sheetName, row, col, false, false));
35117
}
36118
}

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package com.vaadin.flow.component.spreadsheet.tests;
1010

1111
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertFalse;
1213
import static org.junit.Assert.assertTrue;
1314

1415
import java.util.concurrent.atomic.AtomicReference;
@@ -65,8 +66,22 @@ public void formulaChangeResultingInSameValue() {
6566
assertTrue("The changed cells should include C1 with sheet name",
6667
changedCells.get()
6768
.containsCell(new CellReference("Sheet0!C1")));
69+
assertFalse(
70+
"The changed cells should not include C1 with a wrong sheet name",
71+
changedCells.get()
72+
.containsCell(new CellReference("Sheet1!C1")));
6873
assertTrue("The changed cells should include C1 without sheet name",
6974
changedCells.get().containsCell(new CellReference("C1")));
75+
assertTrue(
76+
"The changed cells should include a cell with correct indexes without a sheet name",
77+
changedCells.get().containsCell(0, 2));
78+
assertTrue(
79+
"The changed cells should include a cell with correct indexes and sheet name",
80+
changedCells.get().containsCell(0, 2, "Sheet0"));
81+
assertFalse(
82+
"The changed cells should not include a cell with correct indexes and a wrong sheet name",
83+
changedCells.get().containsCell(0, 2, "Sheet1"));
84+
7085
}
7186

7287
}

0 commit comments

Comments
 (0)