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