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