001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020import static com.google.common.base.Preconditions.checkElementIndex; 021import static com.google.common.base.Preconditions.checkNotNull; 022import static java.lang.System.arraycopy; 023import static java.util.Collections.emptyMap; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.annotations.J2ktIncompatible; 028import com.google.common.base.Objects; 029import com.google.common.collect.Maps.IteratorBasedAbstractMap; 030import com.google.errorprone.annotations.CanIgnoreReturnValue; 031import com.google.errorprone.annotations.DoNotCall; 032import com.google.errorprone.annotations.concurrent.LazyInit; 033import com.google.j2objc.annotations.WeakOuter; 034import java.io.Serializable; 035import java.lang.reflect.Array; 036import java.util.Arrays; 037import java.util.Collection; 038import java.util.Iterator; 039import java.util.Map; 040import java.util.Set; 041import java.util.Spliterator; 042import org.jspecify.annotations.Nullable; 043 044/** 045 * Fixed-size {@link Table} implementation backed by a two-dimensional array. 046 * 047 * <p><b>Warning:</b> {@code ArrayTable} is rarely the {@link Table} implementation you want. First, 048 * it requires that the complete universe of rows and columns be specified at construction time. 049 * Second, it is always backed by an array large enough to hold a value for every possible 050 * combination of row and column keys. (This is rarely optimal unless the table is extremely dense.) 051 * Finally, every possible combination of row and column keys is always considered to have a value 052 * associated with it: It is not possible to "remove" a value, only to replace it with {@code null}, 053 * which will still appear when iterating over the table's contents in a foreach loop or a call to a 054 * null-hostile method like {@link ImmutableTable#copyOf}. For alternatives, please see <a 055 * href="https://github.com/google/guava/wiki/NewCollectionTypesExplained#table">the wiki</a>. 056 * 057 * <p>The allowed row and column keys must be supplied when the table is created. The table always 058 * contains a mapping for every row key / column pair. The value corresponding to a given row and 059 * column is null unless another value is provided. 060 * 061 * <p>The table's size is constant: the product of the number of supplied row keys and the number of 062 * supplied column keys. The {@code remove} and {@code clear} methods are not supported by the table 063 * or its views. The {@link #erase} and {@link #eraseAll} methods may be used instead. 064 * 065 * <p>The ordering of the row and column keys provided when the table is constructed determines the 066 * iteration ordering across rows and columns in the table's views. None of the view iterators 067 * support {@link Iterator#remove}. If the table is modified after an iterator is created, the 068 * iterator remains valid. 069 * 070 * <p>This class requires less memory than the {@link HashBasedTable} and {@link TreeBasedTable} 071 * implementations, except when the table is sparse. 072 * 073 * <p>Null row keys or column keys are not permitted. 074 * 075 * <p>This class provides methods involving the underlying array structure, where the array indices 076 * correspond to the position of a row or column in the lists of allowed keys and values. See the 077 * {@link #at}, {@link #set}, {@link #toArray}, {@link #rowKeyList}, and {@link #columnKeyList} 078 * methods for more details. 079 * 080 * <p>Note that this implementation is not synchronized. If multiple threads access the same cell of 081 * an {@code ArrayTable} concurrently and one of the threads modifies its value, there is no 082 * guarantee that the new value will be fully visible to the other threads. To guarantee that 083 * modifications are visible, synchronize access to the table. Unlike other {@code Table} 084 * implementations, synchronization is unnecessary between a thread that writes to one cell and a 085 * thread that reads from another. 086 * 087 * <p>See the Guava User Guide article on <a href= 088 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table">{@code Table}</a>. 089 * 090 * @author Jared Levy 091 * @since 10.0 092 */ 093@GwtCompatible(emulated = true) 094public final class ArrayTable<R, C, V> extends AbstractTable<R, C, @Nullable V> 095 implements Serializable { 096 097 /** 098 * Creates an {@code ArrayTable} filled with {@code null}. 099 * 100 * @param rowKeys row keys that may be stored in the generated table 101 * @param columnKeys column keys that may be stored in the generated table 102 * @throws NullPointerException if any of the provided keys is null 103 * @throws IllegalArgumentException if {@code rowKeys} or {@code columnKeys} contains duplicates 104 * or if exactly one of {@code rowKeys} or {@code columnKeys} is empty. 105 */ 106 public static <R, C, V> ArrayTable<R, C, V> create( 107 Iterable<? extends R> rowKeys, Iterable<? extends C> columnKeys) { 108 return new ArrayTable<>(rowKeys, columnKeys); 109 } 110 111 /* 112 * TODO(jlevy): Add factory methods taking an Enum class, instead of an 113 * iterable, to specify the allowed row keys and/or column keys. Note that 114 * custom serialization logic is needed to support different enum sizes during 115 * serialization and deserialization. 116 */ 117 118 /** 119 * Creates an {@code ArrayTable} with the mappings in the provided table. 120 * 121 * <p>If {@code table} includes a mapping with row key {@code r} and a separate mapping with 122 * column key {@code c}, the returned table contains a mapping with row key {@code r} and column 123 * key {@code c}. If that row key / column key pair in not in {@code table}, the pair maps to 124 * {@code null} in the generated table. 125 * 126 * <p>The returned table allows subsequent {@code put} calls with the row keys in {@code 127 * table.rowKeySet()} and the column keys in {@code table.columnKeySet()}. Calling {@link #put} 128 * with other keys leads to an {@code IllegalArgumentException}. 129 * 130 * <p>The ordering of {@code table.rowKeySet()} and {@code table.columnKeySet()} determines the 131 * row and column iteration ordering of the returned table. 132 * 133 * @throws NullPointerException if {@code table} has a null key 134 */ 135 @SuppressWarnings("unchecked") // TODO(cpovirk): Make constructor accept wildcard types? 136 public static <R, C, V> ArrayTable<R, C, V> create(Table<R, C, ? extends @Nullable V> table) { 137 return (table instanceof ArrayTable) 138 ? new ArrayTable<R, C, V>((ArrayTable<R, C, V>) table) 139 : new ArrayTable<R, C, V>(table); 140 } 141 142 private final ImmutableList<R> rowList; 143 private final ImmutableList<C> columnList; 144 145 // TODO(jlevy): Add getters returning rowKeyToIndex and columnKeyToIndex? 146 private final ImmutableMap<R, Integer> rowKeyToIndex; 147 private final ImmutableMap<C, Integer> columnKeyToIndex; 148 private final @Nullable V[][] array; 149 150 private ArrayTable(Iterable<? extends R> rowKeys, Iterable<? extends C> columnKeys) { 151 this.rowList = ImmutableList.copyOf(rowKeys); 152 this.columnList = ImmutableList.copyOf(columnKeys); 153 checkArgument(rowList.isEmpty() == columnList.isEmpty()); 154 155 /* 156 * TODO(jlevy): Support only one of rowKey / columnKey being empty? If we 157 * do, when columnKeys is empty but rowKeys isn't, rowKeyList() can contain 158 * elements but rowKeySet() will be empty and containsRow() won't 159 * acknowledge them. 160 */ 161 rowKeyToIndex = Maps.indexMap(rowList); 162 columnKeyToIndex = Maps.indexMap(columnList); 163 164 @SuppressWarnings("unchecked") 165 @Nullable V[][] tmpArray = (@Nullable V[][]) new Object[rowList.size()][columnList.size()]; 166 array = tmpArray; 167 // Necessary because in GWT the arrays are initialized with "undefined" instead of null. 168 eraseAll(); 169 } 170 171 private ArrayTable(Table<R, C, ? extends @Nullable V> table) { 172 this(table.rowKeySet(), table.columnKeySet()); 173 putAll(table); 174 } 175 176 private ArrayTable(ArrayTable<R, C, V> table) { 177 rowList = table.rowList; 178 columnList = table.columnList; 179 rowKeyToIndex = table.rowKeyToIndex; 180 columnKeyToIndex = table.columnKeyToIndex; 181 @SuppressWarnings("unchecked") 182 @Nullable V[][] copy = (@Nullable V[][]) new Object[rowList.size()][columnList.size()]; 183 array = copy; 184 for (int i = 0; i < rowList.size(); i++) { 185 arraycopy(table.array[i], 0, copy[i], 0, table.array[i].length); 186 } 187 } 188 189 private abstract static class ArrayMap<K, V extends @Nullable Object> 190 extends IteratorBasedAbstractMap<K, V> { 191 private final ImmutableMap<K, Integer> keyIndex; 192 193 private ArrayMap(ImmutableMap<K, Integer> keyIndex) { 194 this.keyIndex = keyIndex; 195 } 196 197 @Override 198 public Set<K> keySet() { 199 return keyIndex.keySet(); 200 } 201 202 K getKey(int index) { 203 return keyIndex.keySet().asList().get(index); 204 } 205 206 abstract String getKeyRole(); 207 208 @ParametricNullness 209 abstract V getValue(int index); 210 211 @ParametricNullness 212 abstract V setValue(int index, @ParametricNullness V newValue); 213 214 @Override 215 public int size() { 216 return keyIndex.size(); 217 } 218 219 @Override 220 public boolean isEmpty() { 221 return keyIndex.isEmpty(); 222 } 223 224 Entry<K, V> getEntry(int index) { 225 checkElementIndex(index, size()); 226 return new AbstractMapEntry<K, V>() { 227 @Override 228 public K getKey() { 229 return ArrayMap.this.getKey(index); 230 } 231 232 @Override 233 @ParametricNullness 234 public V getValue() { 235 return ArrayMap.this.getValue(index); 236 } 237 238 @Override 239 @ParametricNullness 240 public V setValue(@ParametricNullness V value) { 241 return ArrayMap.this.setValue(index, value); 242 } 243 }; 244 } 245 246 @Override 247 Iterator<Entry<K, V>> entryIterator() { 248 return new AbstractIndexedListIterator<Entry<K, V>>(size()) { 249 @Override 250 protected Entry<K, V> get(int index) { 251 return getEntry(index); 252 } 253 }; 254 } 255 256 @Override 257 Spliterator<Entry<K, V>> entrySpliterator() { 258 return CollectSpliterators.indexed(size(), Spliterator.ORDERED, this::getEntry); 259 } 260 261 // TODO(lowasser): consider an optimized values() implementation 262 263 @Override 264 public boolean containsKey(@Nullable Object key) { 265 return keyIndex.containsKey(key); 266 } 267 268 @Override 269 public @Nullable V get(@Nullable Object key) { 270 Integer index = keyIndex.get(key); 271 if (index == null) { 272 return null; 273 } else { 274 return getValue(index); 275 } 276 } 277 278 @Override 279 public @Nullable V put(K key, @ParametricNullness V value) { 280 Integer index = keyIndex.get(key); 281 if (index == null) { 282 throw new IllegalArgumentException( 283 getKeyRole() + " " + key + " not in " + keyIndex.keySet()); 284 } 285 return setValue(index, value); 286 } 287 288 @Override 289 public @Nullable V remove(@Nullable Object key) { 290 throw new UnsupportedOperationException(); 291 } 292 293 @Override 294 public void clear() { 295 throw new UnsupportedOperationException(); 296 } 297 } 298 299 /** 300 * Returns, as an immutable list, the row keys provided when the table was constructed, including 301 * those that are mapped to null values only. 302 */ 303 public ImmutableList<R> rowKeyList() { 304 return rowList; 305 } 306 307 /** 308 * Returns, as an immutable list, the column keys provided when the table was constructed, 309 * including those that are mapped to null values only. 310 */ 311 public ImmutableList<C> columnKeyList() { 312 return columnList; 313 } 314 315 /** 316 * Returns the value corresponding to the specified row and column indices. The same value is 317 * returned by {@code get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex))}, but this 318 * method runs more quickly. 319 * 320 * @param rowIndex position of the row key in {@link #rowKeyList()} 321 * @param columnIndex position of the row key in {@link #columnKeyList()} 322 * @return the value with the specified row and column 323 * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than 324 * or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal 325 * to the number of allowed column keys 326 */ 327 public @Nullable V at(int rowIndex, int columnIndex) { 328 // In GWT array access never throws IndexOutOfBoundsException. 329 checkElementIndex(rowIndex, rowList.size()); 330 checkElementIndex(columnIndex, columnList.size()); 331 return array[rowIndex][columnIndex]; 332 } 333 334 /** 335 * Associates {@code value} with the specified row and column indices. The logic {@code 336 * put(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex), value)} has the same 337 * behavior, but this method runs more quickly. 338 * 339 * @param rowIndex position of the row key in {@link #rowKeyList()} 340 * @param columnIndex position of the row key in {@link #columnKeyList()} 341 * @param value value to store in the table 342 * @return the previous value with the specified row and column 343 * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than 344 * or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal 345 * to the number of allowed column keys 346 */ 347 @CanIgnoreReturnValue 348 public @Nullable V set(int rowIndex, int columnIndex, @Nullable V value) { 349 // In GWT array access never throws IndexOutOfBoundsException. 350 checkElementIndex(rowIndex, rowList.size()); 351 checkElementIndex(columnIndex, columnList.size()); 352 V oldValue = array[rowIndex][columnIndex]; 353 array[rowIndex][columnIndex] = value; 354 return oldValue; 355 } 356 357 /** 358 * Returns a two-dimensional array with the table contents. The row and column indices correspond 359 * to the positions of the row and column in the iterables provided during table construction. If 360 * the table lacks a mapping for a given row and column, the corresponding array element is null. 361 * 362 * <p>Subsequent table changes will not modify the array, and vice versa. 363 * 364 * @param valueClass class of values stored in the returned array 365 */ 366 @GwtIncompatible // reflection 367 public @Nullable V[][] toArray(Class<V> valueClass) { 368 @SuppressWarnings("unchecked") // TODO: safe? 369 @Nullable V[][] copy = 370 (@Nullable V[][]) Array.newInstance(valueClass, rowList.size(), columnList.size()); 371 for (int i = 0; i < rowList.size(); i++) { 372 arraycopy(array[i], 0, copy[i], 0, array[i].length); 373 } 374 return copy; 375 } 376 377 /** 378 * Not supported. Use {@link #eraseAll} instead. 379 * 380 * @throws UnsupportedOperationException always 381 * @deprecated Use {@link #eraseAll} 382 */ 383 @DoNotCall("Always throws UnsupportedOperationException") 384 @Override 385 @Deprecated 386 public void clear() { 387 throw new UnsupportedOperationException(); 388 } 389 390 /** Associates the value {@code null} with every pair of allowed row and column keys. */ 391 public void eraseAll() { 392 for (@Nullable V[] row : array) { 393 Arrays.fill(row, null); 394 } 395 } 396 397 /** 398 * Returns {@code true} if the provided keys are among the keys provided when the table was 399 * constructed. 400 */ 401 @Override 402 public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) { 403 return containsRow(rowKey) && containsColumn(columnKey); 404 } 405 406 /** 407 * Returns {@code true} if the provided column key is among the column keys provided when the 408 * table was constructed. 409 */ 410 @Override 411 public boolean containsColumn(@Nullable Object columnKey) { 412 return columnKeyToIndex.containsKey(columnKey); 413 } 414 415 /** 416 * Returns {@code true} if the provided row key is among the row keys provided when the table was 417 * constructed. 418 */ 419 @Override 420 public boolean containsRow(@Nullable Object rowKey) { 421 return rowKeyToIndex.containsKey(rowKey); 422 } 423 424 @Override 425 public boolean containsValue(@Nullable Object value) { 426 for (@Nullable V[] row : array) { 427 for (V element : row) { 428 if (Objects.equal(value, element)) { 429 return true; 430 } 431 } 432 } 433 return false; 434 } 435 436 @Override 437 public @Nullable V get(@Nullable Object rowKey, @Nullable Object columnKey) { 438 Integer rowIndex = rowKeyToIndex.get(rowKey); 439 Integer columnIndex = columnKeyToIndex.get(columnKey); 440 return (rowIndex == null || columnIndex == null) ? null : at(rowIndex, columnIndex); 441 } 442 443 /** 444 * Returns {@code true} if {@code rowKeyList().size == 0} or {@code columnKeyList().size() == 0}. 445 */ 446 @Override 447 public boolean isEmpty() { 448 return rowList.isEmpty() || columnList.isEmpty(); 449 } 450 451 /** 452 * {@inheritDoc} 453 * 454 * @throws IllegalArgumentException if {@code rowKey} is not in {@link #rowKeySet()} or {@code 455 * columnKey} is not in {@link #columnKeySet()}. 456 */ 457 @CanIgnoreReturnValue 458 @Override 459 public @Nullable V put(R rowKey, C columnKey, @Nullable V value) { 460 checkNotNull(rowKey); 461 checkNotNull(columnKey); 462 Integer rowIndex = rowKeyToIndex.get(rowKey); 463 checkArgument(rowIndex != null, "Row %s not in %s", rowKey, rowList); 464 Integer columnIndex = columnKeyToIndex.get(columnKey); 465 checkArgument(columnIndex != null, "Column %s not in %s", columnKey, columnList); 466 return set(rowIndex, columnIndex, value); 467 } 468 469 /* 470 * TODO(jlevy): Consider creating a merge() method, similar to putAll() but 471 * copying non-null values only. 472 */ 473 474 /** 475 * {@inheritDoc} 476 * 477 * <p>If {@code table} is an {@code ArrayTable}, its null values will be stored in this table, 478 * possibly replacing values that were previously non-null. 479 * 480 * @throws NullPointerException if {@code table} has a null key 481 * @throws IllegalArgumentException if any of the provided table's row keys or column keys is not 482 * in {@link #rowKeySet()} or {@link #columnKeySet()} 483 */ 484 @Override 485 public void putAll(Table<? extends R, ? extends C, ? extends @Nullable V> table) { 486 super.putAll(table); 487 } 488 489 /** 490 * Not supported. Use {@link #erase} instead. 491 * 492 * @throws UnsupportedOperationException always 493 * @deprecated Use {@link #erase} 494 */ 495 @DoNotCall("Always throws UnsupportedOperationException") 496 @CanIgnoreReturnValue 497 @Override 498 @Deprecated 499 public @Nullable V remove(@Nullable Object rowKey, @Nullable Object columnKey) { 500 throw new UnsupportedOperationException(); 501 } 502 503 /** 504 * Associates the value {@code null} with the specified keys, assuming both keys are valid. If 505 * either key is null or isn't among the keys provided during construction, this method has no 506 * effect. 507 * 508 * <p>This method is equivalent to {@code put(rowKey, columnKey, null)} when both provided keys 509 * are valid. 510 * 511 * @param rowKey row key of mapping to be erased 512 * @param columnKey column key of mapping to be erased 513 * @return the value previously associated with the keys, or {@code null} if no mapping existed 514 * for the keys 515 */ 516 @CanIgnoreReturnValue 517 public @Nullable V erase(@Nullable Object rowKey, @Nullable Object columnKey) { 518 Integer rowIndex = rowKeyToIndex.get(rowKey); 519 Integer columnIndex = columnKeyToIndex.get(columnKey); 520 if (rowIndex == null || columnIndex == null) { 521 return null; 522 } 523 return set(rowIndex, columnIndex, null); 524 } 525 526 // TODO(jlevy): Add eraseRow and eraseColumn methods? 527 528 @Override 529 public int size() { 530 return rowList.size() * columnList.size(); 531 } 532 533 /** 534 * Returns an unmodifiable set of all row key / column key / value triplets. Changes to the table 535 * will update the returned set. 536 * 537 * <p>The returned set's iterator traverses the mappings with the first row key, the mappings with 538 * the second row key, and so on. 539 * 540 * <p>The value in the returned cells may change if the table subsequently changes. 541 * 542 * @return set of table cells consisting of row key / column key / value triplets 543 */ 544 @Override 545 public Set<Cell<R, C, @Nullable V>> cellSet() { 546 return super.cellSet(); 547 } 548 549 @Override 550 Iterator<Cell<R, C, @Nullable V>> cellIterator() { 551 return new AbstractIndexedListIterator<Cell<R, C, @Nullable V>>(size()) { 552 @Override 553 protected Cell<R, C, @Nullable V> get(int index) { 554 return getCell(index); 555 } 556 }; 557 } 558 559 @Override 560 Spliterator<Cell<R, C, @Nullable V>> cellSpliterator() { 561 return CollectSpliterators.<Cell<R, C, @Nullable V>>indexed( 562 size(), Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT, this::getCell); 563 } 564 565 private Cell<R, C, @Nullable V> getCell(int index) { 566 return new Tables.AbstractCell<R, C, @Nullable V>() { 567 final int rowIndex = index / columnList.size(); 568 final int columnIndex = index % columnList.size(); 569 570 @Override 571 public R getRowKey() { 572 return rowList.get(rowIndex); 573 } 574 575 @Override 576 public C getColumnKey() { 577 return columnList.get(columnIndex); 578 } 579 580 @Override 581 public @Nullable V getValue() { 582 return at(rowIndex, columnIndex); 583 } 584 }; 585 } 586 587 private @Nullable V getValue(int index) { 588 int rowIndex = index / columnList.size(); 589 int columnIndex = index % columnList.size(); 590 return at(rowIndex, columnIndex); 591 } 592 593 /** 594 * Returns a view of all mappings that have the given column key. If the column key isn't in 595 * {@link #columnKeySet()}, an empty immutable map is returned. 596 * 597 * <p>Otherwise, for each row key in {@link #rowKeySet()}, the returned map associates the row key 598 * with the corresponding value in the table. Changes to the returned map will update the 599 * underlying table, and vice versa. 600 * 601 * @param columnKey key of column to search for in the table 602 * @return the corresponding map from row keys to values 603 */ 604 @Override 605 public Map<R, @Nullable V> column(C columnKey) { 606 checkNotNull(columnKey); 607 Integer columnIndex = columnKeyToIndex.get(columnKey); 608 if (columnIndex == null) { 609 return emptyMap(); 610 } else { 611 return new Column(columnIndex); 612 } 613 } 614 615 private class Column extends ArrayMap<R, @Nullable V> { 616 final int columnIndex; 617 618 Column(int columnIndex) { 619 super(rowKeyToIndex); 620 this.columnIndex = columnIndex; 621 } 622 623 @Override 624 String getKeyRole() { 625 return "Row"; 626 } 627 628 @Override 629 @Nullable V getValue(int index) { 630 return at(index, columnIndex); 631 } 632 633 @Override 634 @Nullable V setValue(int index, @Nullable V newValue) { 635 return set(index, columnIndex, newValue); 636 } 637 } 638 639 /** 640 * Returns an immutable set of the valid column keys, including those that are associated with 641 * null values only. 642 * 643 * @return immutable set of column keys 644 */ 645 @Override 646 public ImmutableSet<C> columnKeySet() { 647 return columnKeyToIndex.keySet(); 648 } 649 650 @LazyInit private transient @Nullable ColumnMap columnMap; 651 652 @Override 653 public Map<C, Map<R, @Nullable V>> columnMap() { 654 ColumnMap map = columnMap; 655 return (map == null) ? columnMap = new ColumnMap() : map; 656 } 657 658 @WeakOuter 659 private class ColumnMap extends ArrayMap<C, Map<R, @Nullable V>> { 660 private ColumnMap() { 661 super(columnKeyToIndex); 662 } 663 664 @Override 665 String getKeyRole() { 666 return "Column"; 667 } 668 669 @Override 670 Map<R, @Nullable V> getValue(int index) { 671 return new Column(index); 672 } 673 674 @Override 675 Map<R, @Nullable V> setValue(int index, Map<R, @Nullable V> newValue) { 676 throw new UnsupportedOperationException(); 677 } 678 679 @Override 680 public @Nullable Map<R, @Nullable V> put(C key, Map<R, @Nullable V> value) { 681 throw new UnsupportedOperationException(); 682 } 683 } 684 685 /** 686 * Returns a view of all mappings that have the given row key. If the row key isn't in {@link 687 * #rowKeySet()}, an empty immutable map is returned. 688 * 689 * <p>Otherwise, for each column key in {@link #columnKeySet()}, the returned map associates the 690 * column key with the corresponding value in the table. Changes to the returned map will update 691 * the underlying table, and vice versa. 692 * 693 * @param rowKey key of row to search for in the table 694 * @return the corresponding map from column keys to values 695 */ 696 @Override 697 public Map<C, @Nullable V> row(R rowKey) { 698 checkNotNull(rowKey); 699 Integer rowIndex = rowKeyToIndex.get(rowKey); 700 if (rowIndex == null) { 701 return emptyMap(); 702 } else { 703 return new Row(rowIndex); 704 } 705 } 706 707 private class Row extends ArrayMap<C, @Nullable V> { 708 final int rowIndex; 709 710 Row(int rowIndex) { 711 super(columnKeyToIndex); 712 this.rowIndex = rowIndex; 713 } 714 715 @Override 716 String getKeyRole() { 717 return "Column"; 718 } 719 720 @Override 721 @Nullable V getValue(int index) { 722 return at(rowIndex, index); 723 } 724 725 @Override 726 @Nullable V setValue(int index, @Nullable V newValue) { 727 return set(rowIndex, index, newValue); 728 } 729 } 730 731 /** 732 * Returns an immutable set of the valid row keys, including those that are associated with null 733 * values only. 734 * 735 * @return immutable set of row keys 736 */ 737 @Override 738 public ImmutableSet<R> rowKeySet() { 739 return rowKeyToIndex.keySet(); 740 } 741 742 @LazyInit private transient @Nullable RowMap rowMap; 743 744 @Override 745 public Map<R, Map<C, @Nullable V>> rowMap() { 746 RowMap map = rowMap; 747 return (map == null) ? rowMap = new RowMap() : map; 748 } 749 750 @WeakOuter 751 private class RowMap extends ArrayMap<R, Map<C, @Nullable V>> { 752 private RowMap() { 753 super(rowKeyToIndex); 754 } 755 756 @Override 757 String getKeyRole() { 758 return "Row"; 759 } 760 761 @Override 762 Map<C, @Nullable V> getValue(int index) { 763 return new Row(index); 764 } 765 766 @Override 767 Map<C, @Nullable V> setValue(int index, Map<C, @Nullable V> newValue) { 768 throw new UnsupportedOperationException(); 769 } 770 771 @Override 772 public @Nullable Map<C, @Nullable V> put(R key, Map<C, @Nullable V> value) { 773 throw new UnsupportedOperationException(); 774 } 775 } 776 777 /** 778 * Returns an unmodifiable collection of all values, which may contain duplicates. Changes to the 779 * table will update the returned collection. 780 * 781 * <p>The returned collection's iterator traverses the values of the first row key, the values of 782 * the second row key, and so on. 783 * 784 * @return collection of values 785 */ 786 @Override 787 public Collection<@Nullable V> values() { 788 return super.values(); 789 } 790 791 @Override 792 Iterator<@Nullable V> valuesIterator() { 793 return new AbstractIndexedListIterator<@Nullable V>(size()) { 794 @Override 795 protected @Nullable V get(int index) { 796 return getValue(index); 797 } 798 }; 799 } 800 801 @Override 802 Spliterator<@Nullable V> valuesSpliterator() { 803 return CollectSpliterators.<@Nullable V>indexed(size(), Spliterator.ORDERED, this::getValue); 804 } 805 806 @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0; 807}