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.base.Objects; 028import com.google.common.collect.Maps.IteratorBasedAbstractMap; 029import com.google.errorprone.annotations.CanIgnoreReturnValue; 030import com.google.errorprone.annotations.DoNotCall; 031import com.google.errorprone.annotations.concurrent.LazyInit; 032import com.google.j2objc.annotations.WeakOuter; 033import java.io.Serializable; 034import java.lang.reflect.Array; 035import java.util.Arrays; 036import java.util.Collection; 037import java.util.Iterator; 038import java.util.Map; 039import java.util.Set; 040import java.util.Spliterator; 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 165 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 183 V[][] copy = (@Nullable V[][]) new Object[rowList.size()][columnList.size()]; 184 array = copy; 185 for (int i = 0; i < rowList.size(); i++) { 186 arraycopy(table.array[i], 0, copy[i], 0, table.array[i].length); 187 } 188 } 189 190 private abstract static class ArrayMap<K, V extends @Nullable Object> 191 extends IteratorBasedAbstractMap<K, V> { 192 private final ImmutableMap<K, Integer> keyIndex; 193 194 private ArrayMap(ImmutableMap<K, Integer> keyIndex) { 195 this.keyIndex = keyIndex; 196 } 197 198 @Override 199 public Set<K> keySet() { 200 return keyIndex.keySet(); 201 } 202 203 K getKey(int index) { 204 return keyIndex.keySet().asList().get(index); 205 } 206 207 abstract String getKeyRole(); 208 209 @ParametricNullness 210 abstract V getValue(int index); 211 212 @ParametricNullness 213 abstract V setValue(int index, @ParametricNullness V newValue); 214 215 @Override 216 public int size() { 217 return keyIndex.size(); 218 } 219 220 @Override 221 public boolean isEmpty() { 222 return keyIndex.isEmpty(); 223 } 224 225 Entry<K, V> getEntry(final int index) { 226 checkElementIndex(index, size()); 227 return new AbstractMapEntry<K, V>() { 228 @Override 229 public K getKey() { 230 return ArrayMap.this.getKey(index); 231 } 232 233 @Override 234 @ParametricNullness 235 public V getValue() { 236 return ArrayMap.this.getValue(index); 237 } 238 239 @Override 240 @ParametricNullness 241 public V setValue(@ParametricNullness V value) { 242 return ArrayMap.this.setValue(index, value); 243 } 244 }; 245 } 246 247 @Override 248 Iterator<Entry<K, V>> entryIterator() { 249 return new AbstractIndexedListIterator<Entry<K, V>>(size()) { 250 @Override 251 protected Entry<K, V> get(final int index) { 252 return getEntry(index); 253 } 254 }; 255 } 256 257 @Override 258 Spliterator<Entry<K, V>> entrySpliterator() { 259 return CollectSpliterators.indexed(size(), Spliterator.ORDERED, this::getEntry); 260 } 261 262 // TODO(lowasser): consider an optimized values() implementation 263 264 @Override 265 public boolean containsKey(@Nullable Object key) { 266 return keyIndex.containsKey(key); 267 } 268 269 @Override 270 public @Nullable V get(@Nullable Object key) { 271 Integer index = keyIndex.get(key); 272 if (index == null) { 273 return null; 274 } else { 275 return getValue(index); 276 } 277 } 278 279 @Override 280 public @Nullable V put(K key, @ParametricNullness V value) { 281 Integer index = keyIndex.get(key); 282 if (index == null) { 283 throw new IllegalArgumentException( 284 getKeyRole() + " " + key + " not in " + keyIndex.keySet()); 285 } 286 return setValue(index, value); 287 } 288 289 @Override 290 public @Nullable V remove(@Nullable Object key) { 291 throw new UnsupportedOperationException(); 292 } 293 294 @Override 295 public void clear() { 296 throw new UnsupportedOperationException(); 297 } 298 } 299 300 /** 301 * Returns, as an immutable list, the row keys provided when the table was constructed, including 302 * those that are mapped to null values only. 303 */ 304 public ImmutableList<R> rowKeyList() { 305 return rowList; 306 } 307 308 /** 309 * Returns, as an immutable list, the column keys provided when the table was constructed, 310 * including those that are mapped to null values only. 311 */ 312 public ImmutableList<C> columnKeyList() { 313 return columnList; 314 } 315 316 /** 317 * Returns the value corresponding to the specified row and column indices. The same value is 318 * returned by {@code get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex))}, but this 319 * method runs more quickly. 320 * 321 * @param rowIndex position of the row key in {@link #rowKeyList()} 322 * @param columnIndex position of the row key in {@link #columnKeyList()} 323 * @return the value with the specified row and column 324 * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than 325 * or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal 326 * to the number of allowed column keys 327 */ 328 public @Nullable V at(int rowIndex, int columnIndex) { 329 // In GWT array access never throws IndexOutOfBoundsException. 330 checkElementIndex(rowIndex, rowList.size()); 331 checkElementIndex(columnIndex, columnList.size()); 332 return array[rowIndex][columnIndex]; 333 } 334 335 /** 336 * Associates {@code value} with the specified row and column indices. The logic {@code 337 * put(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex), value)} has the same 338 * behavior, but this method runs more quickly. 339 * 340 * @param rowIndex position of the row key in {@link #rowKeyList()} 341 * @param columnIndex position of the row key in {@link #columnKeyList()} 342 * @param value value to store in the table 343 * @return the previous value with the specified row and column 344 * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than 345 * or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal 346 * to the number of allowed column keys 347 */ 348 @CanIgnoreReturnValue 349 public @Nullable V set(int rowIndex, int columnIndex, @Nullable V value) { 350 // In GWT array access never throws IndexOutOfBoundsException. 351 checkElementIndex(rowIndex, rowList.size()); 352 checkElementIndex(columnIndex, columnList.size()); 353 V oldValue = array[rowIndex][columnIndex]; 354 array[rowIndex][columnIndex] = value; 355 return oldValue; 356 } 357 358 /** 359 * Returns a two-dimensional array with the table contents. The row and column indices correspond 360 * to the positions of the row and column in the iterables provided during table construction. If 361 * the table lacks a mapping for a given row and column, the corresponding array element is null. 362 * 363 * <p>Subsequent table changes will not modify the array, and vice versa. 364 * 365 * @param valueClass class of values stored in the returned array 366 */ 367 @GwtIncompatible // reflection 368 public @Nullable V[][] toArray(Class<V> valueClass) { 369 @SuppressWarnings("unchecked") // TODO: safe? 370 @Nullable 371 V[][] copy = (@Nullable V[][]) Array.newInstance(valueClass, rowList.size(), columnList.size()); 372 for (int i = 0; i < rowList.size(); i++) { 373 arraycopy(array[i], 0, copy[i], 0, array[i].length); 374 } 375 return copy; 376 } 377 378 /** 379 * Not supported. Use {@link #eraseAll} instead. 380 * 381 * @throws UnsupportedOperationException always 382 * @deprecated Use {@link #eraseAll} 383 */ 384 @DoNotCall("Always throws UnsupportedOperationException") 385 @Override 386 @Deprecated 387 public void clear() { 388 throw new UnsupportedOperationException(); 389 } 390 391 /** Associates the value {@code null} with every pair of allowed row and column keys. */ 392 public void eraseAll() { 393 for (@Nullable V[] row : array) { 394 Arrays.fill(row, null); 395 } 396 } 397 398 /** 399 * Returns {@code true} if the provided keys are among the keys provided when the table was 400 * constructed. 401 */ 402 @Override 403 public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) { 404 return containsRow(rowKey) && containsColumn(columnKey); 405 } 406 407 /** 408 * Returns {@code true} if the provided column key is among the column keys provided when the 409 * table was constructed. 410 */ 411 @Override 412 public boolean containsColumn(@Nullable Object columnKey) { 413 return columnKeyToIndex.containsKey(columnKey); 414 } 415 416 /** 417 * Returns {@code true} if the provided row key is among the row keys provided when the table was 418 * constructed. 419 */ 420 @Override 421 public boolean containsRow(@Nullable Object rowKey) { 422 return rowKeyToIndex.containsKey(rowKey); 423 } 424 425 @Override 426 public boolean containsValue(@Nullable Object value) { 427 for (@Nullable V[] row : array) { 428 for (V element : row) { 429 if (Objects.equal(value, element)) { 430 return true; 431 } 432 } 433 } 434 return false; 435 } 436 437 @Override 438 public @Nullable V get(@Nullable Object rowKey, @Nullable Object columnKey) { 439 Integer rowIndex = rowKeyToIndex.get(rowKey); 440 Integer columnIndex = columnKeyToIndex.get(columnKey); 441 return (rowIndex == null || columnIndex == null) ? null : at(rowIndex, columnIndex); 442 } 443 444 /** 445 * Returns {@code true} if {@code rowKeyList().size == 0} or {@code columnKeyList().size() == 0}. 446 */ 447 @Override 448 public boolean isEmpty() { 449 return rowList.isEmpty() || columnList.isEmpty(); 450 } 451 452 /** 453 * {@inheritDoc} 454 * 455 * @throws IllegalArgumentException if {@code rowKey} is not in {@link #rowKeySet()} or {@code 456 * columnKey} is not in {@link #columnKeySet()}. 457 */ 458 @CanIgnoreReturnValue 459 @Override 460 public @Nullable V put(R rowKey, C columnKey, @Nullable V value) { 461 checkNotNull(rowKey); 462 checkNotNull(columnKey); 463 Integer rowIndex = rowKeyToIndex.get(rowKey); 464 checkArgument(rowIndex != null, "Row %s not in %s", rowKey, rowList); 465 Integer columnIndex = columnKeyToIndex.get(columnKey); 466 checkArgument(columnIndex != null, "Column %s not in %s", columnKey, columnList); 467 return set(rowIndex, columnIndex, value); 468 } 469 470 /* 471 * TODO(jlevy): Consider creating a merge() method, similar to putAll() but 472 * copying non-null values only. 473 */ 474 475 /** 476 * {@inheritDoc} 477 * 478 * <p>If {@code table} is an {@code ArrayTable}, its null values will be stored in this table, 479 * possibly replacing values that were previously non-null. 480 * 481 * @throws NullPointerException if {@code table} has a null key 482 * @throws IllegalArgumentException if any of the provided table's row keys or column keys is not 483 * in {@link #rowKeySet()} or {@link #columnKeySet()} 484 */ 485 @Override 486 public void putAll(Table<? extends R, ? extends C, ? extends @Nullable V> table) { 487 super.putAll(table); 488 } 489 490 /** 491 * Not supported. Use {@link #erase} instead. 492 * 493 * @throws UnsupportedOperationException always 494 * @deprecated Use {@link #erase} 495 */ 496 @DoNotCall("Always throws UnsupportedOperationException") 497 @CanIgnoreReturnValue 498 @Override 499 @Deprecated 500 public @Nullable V remove(@Nullable Object rowKey, @Nullable Object columnKey) { 501 throw new UnsupportedOperationException(); 502 } 503 504 /** 505 * Associates the value {@code null} with the specified keys, assuming both keys are valid. If 506 * either key is null or isn't among the keys provided during construction, this method has no 507 * effect. 508 * 509 * <p>This method is equivalent to {@code put(rowKey, columnKey, null)} when both provided keys 510 * are valid. 511 * 512 * @param rowKey row key of mapping to be erased 513 * @param columnKey column key of mapping to be erased 514 * @return the value previously associated with the keys, or {@code null} if no mapping existed 515 * for the keys 516 */ 517 @CanIgnoreReturnValue 518 public @Nullable V erase(@Nullable Object rowKey, @Nullable Object columnKey) { 519 Integer rowIndex = rowKeyToIndex.get(rowKey); 520 Integer columnIndex = columnKeyToIndex.get(columnKey); 521 if (rowIndex == null || columnIndex == null) { 522 return null; 523 } 524 return set(rowIndex, columnIndex, null); 525 } 526 527 // TODO(jlevy): Add eraseRow and eraseColumn methods? 528 529 @Override 530 public int size() { 531 return rowList.size() * columnList.size(); 532 } 533 534 /** 535 * Returns an unmodifiable set of all row key / column key / value triplets. Changes to the table 536 * will update the returned set. 537 * 538 * <p>The returned set's iterator traverses the mappings with the first row key, the mappings with 539 * the second row key, and so on. 540 * 541 * <p>The value in the returned cells may change if the table subsequently changes. 542 * 543 * @return set of table cells consisting of row key / column key / value triplets 544 */ 545 @Override 546 public Set<Cell<R, C, @Nullable V>> cellSet() { 547 return super.cellSet(); 548 } 549 550 @Override 551 Iterator<Cell<R, C, @Nullable V>> cellIterator() { 552 return new AbstractIndexedListIterator<Cell<R, C, @Nullable V>>(size()) { 553 @Override 554 protected Cell<R, C, @Nullable V> get(final int index) { 555 return getCell(index); 556 } 557 }; 558 } 559 560 @Override 561 Spliterator<Cell<R, C, @Nullable V>> cellSpliterator() { 562 return CollectSpliterators.<Cell<R, C, @Nullable V>>indexed( 563 size(), Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT, this::getCell); 564 } 565 566 private Cell<R, C, @Nullable V> getCell(final int index) { 567 return new Tables.AbstractCell<R, C, @Nullable V>() { 568 final int rowIndex = index / columnList.size(); 569 final int columnIndex = index % columnList.size(); 570 571 @Override 572 public R getRowKey() { 573 return rowList.get(rowIndex); 574 } 575 576 @Override 577 public C getColumnKey() { 578 return columnList.get(columnIndex); 579 } 580 581 @Override 582 public @Nullable V getValue() { 583 return at(rowIndex, columnIndex); 584 } 585 }; 586 } 587 588 private @Nullable V getValue(int index) { 589 int rowIndex = index / columnList.size(); 590 int columnIndex = index % columnList.size(); 591 return at(rowIndex, columnIndex); 592 } 593 594 /** 595 * Returns a view of all mappings that have the given column key. If the column key isn't in 596 * {@link #columnKeySet()}, an empty immutable map is returned. 597 * 598 * <p>Otherwise, for each row key in {@link #rowKeySet()}, the returned map associates the row key 599 * with the corresponding value in the table. Changes to the returned map will update the 600 * underlying table, and vice versa. 601 * 602 * @param columnKey key of column to search for in the table 603 * @return the corresponding map from row keys to values 604 */ 605 @Override 606 public Map<R, @Nullable V> column(C columnKey) { 607 checkNotNull(columnKey); 608 Integer columnIndex = columnKeyToIndex.get(columnKey); 609 if (columnIndex == null) { 610 return emptyMap(); 611 } else { 612 return new Column(columnIndex); 613 } 614 } 615 616 private class Column extends ArrayMap<R, @Nullable V> { 617 final int columnIndex; 618 619 Column(int columnIndex) { 620 super(rowKeyToIndex); 621 this.columnIndex = columnIndex; 622 } 623 624 @Override 625 String getKeyRole() { 626 return "Row"; 627 } 628 629 @Override 630 @Nullable V getValue(int index) { 631 return at(index, columnIndex); 632 } 633 634 @Override 635 @Nullable V setValue(int index, @Nullable V newValue) { 636 return set(index, columnIndex, newValue); 637 } 638 } 639 640 /** 641 * Returns an immutable set of the valid column keys, including those that are associated with 642 * null values only. 643 * 644 * @return immutable set of column keys 645 */ 646 @Override 647 public ImmutableSet<C> columnKeySet() { 648 return columnKeyToIndex.keySet(); 649 } 650 651 @LazyInit private transient @Nullable ColumnMap columnMap; 652 653 @Override 654 public Map<C, Map<R, @Nullable V>> columnMap() { 655 ColumnMap map = columnMap; 656 return (map == null) ? columnMap = new ColumnMap() : map; 657 } 658 659 @WeakOuter 660 private class ColumnMap extends ArrayMap<C, Map<R, @Nullable V>> { 661 private ColumnMap() { 662 super(columnKeyToIndex); 663 } 664 665 @Override 666 String getKeyRole() { 667 return "Column"; 668 } 669 670 @Override 671 Map<R, @Nullable V> getValue(int index) { 672 return new Column(index); 673 } 674 675 @Override 676 Map<R, @Nullable V> setValue(int index, Map<R, @Nullable V> newValue) { 677 throw new UnsupportedOperationException(); 678 } 679 680 @Override 681 public @Nullable Map<R, @Nullable V> put(C key, Map<R, @Nullable V> value) { 682 throw new UnsupportedOperationException(); 683 } 684 } 685 686 /** 687 * Returns a view of all mappings that have the given row key. If the row key isn't in {@link 688 * #rowKeySet()}, an empty immutable map is returned. 689 * 690 * <p>Otherwise, for each column key in {@link #columnKeySet()}, the returned map associates the 691 * column key with the corresponding value in the table. Changes to the returned map will update 692 * the underlying table, and vice versa. 693 * 694 * @param rowKey key of row to search for in the table 695 * @return the corresponding map from column keys to values 696 */ 697 @Override 698 public Map<C, @Nullable V> row(R rowKey) { 699 checkNotNull(rowKey); 700 Integer rowIndex = rowKeyToIndex.get(rowKey); 701 if (rowIndex == null) { 702 return emptyMap(); 703 } else { 704 return new Row(rowIndex); 705 } 706 } 707 708 private class Row extends ArrayMap<C, @Nullable V> { 709 final int rowIndex; 710 711 Row(int rowIndex) { 712 super(columnKeyToIndex); 713 this.rowIndex = rowIndex; 714 } 715 716 @Override 717 String getKeyRole() { 718 return "Column"; 719 } 720 721 @Override 722 @Nullable V getValue(int index) { 723 return at(rowIndex, index); 724 } 725 726 @Override 727 @Nullable V setValue(int index, @Nullable V newValue) { 728 return set(rowIndex, index, newValue); 729 } 730 } 731 732 /** 733 * Returns an immutable set of the valid row keys, including those that are associated with null 734 * values only. 735 * 736 * @return immutable set of row keys 737 */ 738 @Override 739 public ImmutableSet<R> rowKeySet() { 740 return rowKeyToIndex.keySet(); 741 } 742 743 @LazyInit private transient @Nullable RowMap rowMap; 744 745 @Override 746 public Map<R, Map<C, @Nullable V>> rowMap() { 747 RowMap map = rowMap; 748 return (map == null) ? rowMap = new RowMap() : map; 749 } 750 751 @WeakOuter 752 private class RowMap extends ArrayMap<R, Map<C, @Nullable V>> { 753 private RowMap() { 754 super(rowKeyToIndex); 755 } 756 757 @Override 758 String getKeyRole() { 759 return "Row"; 760 } 761 762 @Override 763 Map<C, @Nullable V> getValue(int index) { 764 return new Row(index); 765 } 766 767 @Override 768 Map<C, @Nullable V> setValue(int index, Map<C, @Nullable V> newValue) { 769 throw new UnsupportedOperationException(); 770 } 771 772 @Override 773 public @Nullable Map<C, @Nullable V> put(R key, Map<C, @Nullable V> value) { 774 throw new UnsupportedOperationException(); 775 } 776 } 777 778 /** 779 * Returns an unmodifiable collection of all values, which may contain duplicates. Changes to the 780 * table will update the returned collection. 781 * 782 * <p>The returned collection's iterator traverses the values of the first row key, the values of 783 * the second row key, and so on. 784 * 785 * @return collection of values 786 */ 787 @Override 788 public Collection<@Nullable V> values() { 789 return super.values(); 790 } 791 792 @Override 793 Iterator<@Nullable V> valuesIterator() { 794 return new AbstractIndexedListIterator<@Nullable V>(size()) { 795 @Override 796 protected @Nullable V get(int index) { 797 return getValue(index); 798 } 799 }; 800 } 801 802 @Override 803 Spliterator<@Nullable V> valuesSpliterator() { 804 return CollectSpliterators.<@Nullable V>indexed(size(), Spliterator.ORDERED, this::getValue); 805 } 806 807 private static final long serialVersionUID = 0; 808}