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