Skip to content

Commit 369e792

Browse files
committed
New class TableCell
1 parent 62f0756 commit 369e792

13 files changed

+373
-108
lines changed

.idea/compiler.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_0.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_6_2.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_6_2.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_6_2.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_6_2.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package io.github.parubok.stream;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.OptionalInt;
7+
import java.util.Set;
8+
9+
/**
10+
* Table cell with row and column indexes.
11+
* <p>
12+
* Immutable object - may be shared by reference.
13+
* <p>
14+
* Implements {@link Comparable}. The comparison is performed by row and then by column.
15+
* <p>
16+
* Also contains some utility methods.
17+
*/
18+
public class TableCell implements Comparable<TableCell> {
19+
20+
public static OptionalInt getMaxRow(Collection<TableCell> cells) {
21+
return cells.stream()
22+
.mapToInt(TableCell::getRow)
23+
.max();
24+
}
25+
26+
public static OptionalInt getMaxCol(Collection<TableCell> cells) {
27+
return cells.stream()
28+
.mapToInt(TableCell::getColumn)
29+
.max();
30+
}
31+
32+
private final int row;
33+
private final int col;
34+
35+
public enum Field {
36+
ROW, COLUMN
37+
}
38+
39+
public static final Field ROW = Field.ROW;
40+
public static final Field COLUMN = Field.COLUMN;
41+
42+
public TableCell(int row, int col) {
43+
this.row = row;
44+
this.col = col;
45+
}
46+
47+
public TableCell(TableCell source, int rowDelta, int columnDelta) {
48+
this(source.getRow() + rowDelta, source.getColumn() + columnDelta);
49+
}
50+
51+
public int getColumn() {
52+
return col;
53+
}
54+
55+
public int getRow() {
56+
return row;
57+
}
58+
59+
/**
60+
* @param field TableCell.ROW or TableCell.COL.
61+
* @return Value of the field for this cell.
62+
*/
63+
public int get(Field field) {
64+
return field.equals(ROW) ? getRow() : getColumn();
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
int result = 17;
70+
result = 37 * result + row;
71+
result = 37 * result + col;
72+
return result;
73+
}
74+
75+
@Override
76+
public boolean equals(Object obj) {
77+
return (obj instanceof TableCell) && equals(((TableCell) obj).row, ((TableCell) obj).col);
78+
}
79+
80+
public boolean equals(int row, int col) {
81+
return this.row == row && this.col == col;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return "TableCell{row=" + row + ",column=" + col + "}";
87+
}
88+
89+
/**
90+
* @return true if the specified cells form a continuous selection rectangle.
91+
* @implNote Returns true for empty set.
92+
*/
93+
public static boolean isContinuousSelection(Set<TableCell> cells) {
94+
int[] d = getDimensions(cells);
95+
return (d[0] * d[1]) == cells.size();
96+
}
97+
98+
/**
99+
* @return int[] { row count, column count}
100+
*/
101+
public static int[] getDimensions(Set<TableCell> cells) {
102+
if (cells.isEmpty()) {
103+
return new int[]{0, 0};
104+
}
105+
TableCell leftUpper = Collections.min(cells);
106+
TableCell rightLower = Collections.max(cells);
107+
int rowCount = rightLower.getRow() - leftUpper.getRow() + 1;
108+
int colCount = rightLower.getColumn() - leftUpper.getColumn() + 1;
109+
return new int[]{rowCount, colCount};
110+
}
111+
112+
/**
113+
* Compare cells by row or by column if rows are equal.<br>
114+
* Call of {@link java.util.Collections#min(Collection)} returns the leftmost TableCell in the topmost row within
115+
* {@link Collection}.<br>
116+
* Call of {@link java.util.Collections#max(Collection)} returns the rightmost TableCell in the bottommost row
117+
* within {@link Collection}.
118+
*/
119+
@Override
120+
public int compareTo(TableCell c) {
121+
int result = (this.getRow() - c.getRow());
122+
if (result == 0) {
123+
result = COLUMNS_COMPARATOR.compare(this, c);
124+
}
125+
return result;
126+
}
127+
128+
public boolean isValid() {
129+
return row > -1 && col > -1;
130+
}
131+
132+
/**
133+
* Compares two {@link TableCell}s by columns ignoring rows.
134+
*/
135+
public static final Comparator<TableCell> COLUMNS_COMPARATOR = new Comparator<TableCell>() {
136+
@Override
137+
public int compare(TableCell c0, TableCell c1) {
138+
return c0.getColumn() - c1.getColumn();
139+
}
140+
};
141+
}

0 commit comments

Comments
 (0)