|
| 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 | + */ |
1 | 16 | package com.vaadin.flow.component.spreadsheet;
|
2 | 17 |
|
3 | 18 | import org.apache.poi.ss.util.CellReference;
|
4 | 19 |
|
5 | 20 | import java.util.Collections;
|
| 21 | +import java.util.Objects; |
6 | 22 | import java.util.Set;
|
7 | 23 |
|
| 24 | +/** |
| 25 | + * CellSet is a set of cells that also provides utilities regarding the contents |
| 26 | + * of the set. |
| 27 | + */ |
8 | 28 | public class CellSet {
|
9 | 29 |
|
10 | 30 | private final Set<CellReference> cells;
|
11 | 31 |
|
| 32 | + /** |
| 33 | + * Creates a set with the specified cells |
| 34 | + * |
| 35 | + * @param cells |
| 36 | + * cells to construct the set with, not {@code null} |
| 37 | + */ |
12 | 38 | public CellSet(Set<CellReference> cells) {
|
| 39 | + Objects.requireNonNull(cells, "Cells cannot be null"); |
13 | 40 | this.cells = cells;
|
14 | 41 | }
|
15 | 42 |
|
| 43 | + /** |
| 44 | + * Gets an unmodifiable set of the cells |
| 45 | + * |
| 46 | + * @return an unmodifiable set of the cells |
| 47 | + */ |
16 | 48 | public Set<CellReference> getCells() {
|
17 | 49 | return Collections.unmodifiableSet(cells);
|
18 | 50 | }
|
19 | 51 |
|
| 52 | + /** |
| 53 | + * Gets the number of the cells |
| 54 | + * |
| 55 | + * @return number of cells |
| 56 | + */ |
20 | 57 | public int getCellCount() {
|
21 | 58 | return cells.size();
|
22 | 59 | }
|
23 | 60 |
|
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) { |
25 | 72 | if (cells.isEmpty()) {
|
26 | 73 | return false;
|
27 | 74 | }
|
28 |
| - if (cell.getSheetName() == null) { |
| 75 | + if (cellReference.getSheetName() == null) { |
29 | 76 | 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()); |
32 | 81 | return cells.contains(cellWithSheetName);
|
33 | 82 | }
|
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)); |
35 | 117 | }
|
36 | 118 | }
|
0 commit comments