|
| 1 | +package com.ultreon.libs.collections.v0.tables; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * An abstract implementation of {@link Table}. |
| 10 | + * |
| 11 | + * @param <R> the row type. |
| 12 | + * @param <C> the column type. |
| 13 | + * @param <V> the value type. |
| 14 | + * @since 0.2.0 |
| 15 | + * @author <a href="https://github.com/XyperCodee">XyperCode</a> |
| 16 | + */ |
| 17 | +public abstract class AbstractTable<R, C, V> implements Table<R, C, V> { |
| 18 | + public static <R, C> Index<R, C> index(R row, C column) { |
| 19 | + return new SimpleIndex<>(row, column); |
| 20 | + } |
| 21 | + |
| 22 | + public static <R, C, V> Cell<R, C, V> cell(R row, C column, V value) { |
| 23 | + return new SimpleCell<>(row, column, value); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public int columnSize() { |
| 28 | + return this.columnSet().size(); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public int rowSize() { |
| 33 | + return this.rowSet().size(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public V put(R row, C column, V value) { |
| 38 | + throw new UnsupportedOperationException(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public V getOrDefault(R row, C column, V defaultValue) { |
| 43 | + V value = this.get(row, column); |
| 44 | + return value != null ? value : defaultValue; |
| 45 | + } |
| 46 | + |
| 47 | + @Nullable |
| 48 | + @Override |
| 49 | + public V remove(R row, C column) { |
| 50 | + throw new UnsupportedOperationException(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void clear() { |
| 55 | + throw new UnsupportedOperationException(); |
| 56 | + } |
| 57 | + |
| 58 | + @Nullable |
| 59 | + @Override |
| 60 | + public V get(Index<R, C> index) { |
| 61 | + return this.get(index.getRow(), index.getColumn()); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public V get(R row, C column) { |
| 66 | + return this.row(row).get(column); |
| 67 | + } |
| 68 | + |
| 69 | + @NotNull |
| 70 | + @Override |
| 71 | + public Map<C, V> row(R row) { |
| 72 | + Map<C, V> map = new HashMap<>(); |
| 73 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 74 | + if (cell.getRow().equals(row)) { |
| 75 | + map.put(cell.getColumn(), cell.getValue()); |
| 76 | + } |
| 77 | + } |
| 78 | + return Collections.unmodifiableMap(map); |
| 79 | + } |
| 80 | + |
| 81 | + @NotNull |
| 82 | + @Override |
| 83 | + public Map<R, V> column(C column) { |
| 84 | + Map<R, V> map = new HashMap<>(); |
| 85 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 86 | + if (cell.getColumn().equals(column)) { |
| 87 | + map.put(cell.getRow(), cell.getValue()); |
| 88 | + } |
| 89 | + } |
| 90 | + return Collections.unmodifiableMap(map); |
| 91 | + } |
| 92 | + |
| 93 | + @NotNull |
| 94 | + @Override |
| 95 | + public Map<R, Map<C, V>> toRowMap() { |
| 96 | + Map<R, Map<C, V>> map = new HashMap<>(); |
| 97 | + for (R row : this.rowSet()) { |
| 98 | + map.put(row, this.row(row)); |
| 99 | + } |
| 100 | + return Collections.unmodifiableMap(map); |
| 101 | + } |
| 102 | + |
| 103 | + @NotNull |
| 104 | + @Override |
| 105 | + public Map<C, Map<R, V>> toColumnMap() { |
| 106 | + Map<C, Map<R, V>> map = new HashMap<>(); |
| 107 | + for (C column : this.columnSet()) { |
| 108 | + map.put(column, this.column(column)); |
| 109 | + } |
| 110 | + return Collections.unmodifiableMap(map); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Set<R> rowSet() { |
| 115 | + Set<R> set = new HashSet<>(); |
| 116 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 117 | + set.add(cell.getRow()); |
| 118 | + } |
| 119 | + return Collections.unmodifiableSet(set); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Set<R> rowSet(C column) { |
| 124 | + Set<R> set = new HashSet<>(); |
| 125 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 126 | + if (cell.getColumn().equals(column)) { |
| 127 | + set.add(cell.getRow()); |
| 128 | + } |
| 129 | + } |
| 130 | + return Collections.unmodifiableSet(set); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Set<C> columnSet(R row) { |
| 135 | + Set<C> set = new HashSet<>(); |
| 136 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 137 | + if (cell.getRow().equals(row)) { |
| 138 | + set.add(cell.getColumn()); |
| 139 | + } |
| 140 | + } |
| 141 | + return Collections.unmodifiableSet(set); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Set<C> columnSet() { |
| 146 | + Set<C> set = new HashSet<>(); |
| 147 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 148 | + set.add(cell.getColumn()); |
| 149 | + } |
| 150 | + return Collections.unmodifiableSet(set); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Set<Index<R, C>> indexSet() { |
| 155 | + Set<Index<R, C>> set = new HashSet<>(); |
| 156 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 157 | + set.add(new SimpleIndex<>(cell.getRow(), cell.getColumn())); |
| 158 | + } |
| 159 | + return Collections.unmodifiableSet(set); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Collection<V> values() { |
| 164 | + Set<V> set = new HashSet<>(); |
| 165 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 166 | + set.add(cell.getValue()); |
| 167 | + } |
| 168 | + return Collections.unmodifiableSet(set); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean contains(R row, C column) { |
| 173 | + return this.contains(index(row, column)); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean contains(Index<R, C> index) { |
| 178 | + return this.indexSet().contains(index); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public boolean isEmpty() { |
| 183 | + return this.indexSet().isEmpty(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean containsRow(R row) { |
| 188 | + return this.rowSet().contains(row); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public boolean containsColumn(C column) { |
| 193 | + return this.columnSet().contains(column); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public boolean containsAll(Table<R, C, V> table) { |
| 198 | + return table.cellSet().stream().allMatch(this::contains); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public Map<Index<R, C>, V> toMap() { |
| 203 | + Map<Index<R, C>, V> map = new HashMap<>(); |
| 204 | + for (Cell<R, C, V> cell : this.cellSet()) { |
| 205 | + map.put(new SimpleIndex<>(cell.getRow(), cell.getColumn()), cell.getValue()); |
| 206 | + } |
| 207 | + return Collections.unmodifiableMap(map); |
| 208 | + } |
| 209 | + |
| 210 | + public static class SimpleIndex<R, C> implements Index<R, C> { |
| 211 | + private final R row; |
| 212 | + private final C column; |
| 213 | + |
| 214 | + public SimpleIndex(R row, C column) { |
| 215 | + this.row = row; |
| 216 | + this.column = column; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public int hashCode() { |
| 221 | + return this.row.hashCode() ^ this.column.hashCode(); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public boolean equals(Object obj) { |
| 226 | + if (obj == null) |
| 227 | + return false; |
| 228 | + if (this.getClass() != obj.getClass()) |
| 229 | + return false; |
| 230 | + |
| 231 | + final SimpleIndex<?, ?> other = (SimpleIndex<?, ?>) obj; |
| 232 | + return Objects.equals(this.row, other.row) && Objects.equals(this.column, other.column); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public String toString() { |
| 237 | + return "(" + this.row + ", " + this.column + ")"; |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public R getRow() { |
| 242 | + return this.row; |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public C getColumn() { |
| 247 | + return this.column; |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + public static class SimpleCell<R, C, V> implements Cell<R, C, V> { |
| 252 | + private final R row; |
| 253 | + private final C column; |
| 254 | + private final V value; |
| 255 | + |
| 256 | + public SimpleCell(R row, C column, V value) { |
| 257 | + this.row = row; |
| 258 | + this.column = column; |
| 259 | + this.value = value; |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public int hashCode() { |
| 264 | + return this.row.hashCode() ^ this.column.hashCode() ^ this.value.hashCode(); |
| 265 | + } |
| 266 | + |
| 267 | + @Override |
| 268 | + public boolean equals(Object obj) { |
| 269 | + if (obj == null) |
| 270 | + return false; |
| 271 | + |
| 272 | + if (this.getClass() != obj.getClass()) |
| 273 | + return false; |
| 274 | + |
| 275 | + final SimpleCell<?, ?, ?> other = (SimpleCell<?, ?, ?>) obj; |
| 276 | + return Objects.equals(this.row, other.row) |
| 277 | + && Objects.equals(this.column, other.column) |
| 278 | + && Objects.equals(this.value, other.value); |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + public String toString() { |
| 283 | + return "(" + this.row + ", " + this.column + ")=" + this.value; |
| 284 | + } |
| 285 | + |
| 286 | + @Override |
| 287 | + public R getRow() { |
| 288 | + return this.row; |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public C getColumn() { |
| 293 | + return this.column; |
| 294 | + } |
| 295 | + |
| 296 | + @Override |
| 297 | + public V getValue() { |
| 298 | + return this.value; |
| 299 | + } |
| 300 | + } |
| 301 | +} |
0 commit comments