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.MonotonicNonNullDecl;
039import org.checkerframework.checker.nullness.compatqual.NullableDecl;
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     * acknolwedge 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    @NullableDecl
194    abstract V getValue(int index);
195
196    @NullableDecl
197    abstract V setValue(int index, V newValue);
198
199    @Override
200    public int size() {
201      return keyIndex.size();
202    }
203
204    @Override
205    public boolean isEmpty() {
206      return keyIndex.isEmpty();
207    }
208
209    Entry<K, V> getEntry(final int index) {
210      checkElementIndex(index, size());
211      return new AbstractMapEntry<K, V>() {
212        @Override
213        public K getKey() {
214          return ArrayMap.this.getKey(index);
215        }
216
217        @Override
218        public V getValue() {
219          return ArrayMap.this.getValue(index);
220        }
221
222        @Override
223        public V setValue(V value) {
224          return ArrayMap.this.setValue(index, value);
225        }
226      };
227    }
228
229    @Override
230    Iterator<Entry<K, V>> entryIterator() {
231      return new AbstractIndexedListIterator<Entry<K, V>>(size()) {
232        @Override
233        protected Entry<K, V> get(final int index) {
234          return getEntry(index);
235        }
236      };
237    }
238
239    @Override
240    Spliterator<Entry<K, V>> entrySpliterator() {
241      return CollectSpliterators.indexed(size(), Spliterator.ORDERED, this::getEntry);
242    }
243
244    // TODO(lowasser): consider an optimized values() implementation
245
246    @Override
247    public boolean containsKey(@NullableDecl Object key) {
248      return keyIndex.containsKey(key);
249    }
250
251    @Override
252    public V get(@NullableDecl Object key) {
253      Integer index = keyIndex.get(key);
254      if (index == null) {
255        return null;
256      } else {
257        return getValue(index);
258      }
259    }
260
261    @Override
262    public V put(K key, V value) {
263      Integer index = keyIndex.get(key);
264      if (index == null) {
265        throw new IllegalArgumentException(
266            getKeyRole() + " " + key + " not in " + keyIndex.keySet());
267      }
268      return setValue(index, value);
269    }
270
271    @Override
272    public V remove(Object key) {
273      throw new UnsupportedOperationException();
274    }
275
276    @Override
277    public void clear() {
278      throw new UnsupportedOperationException();
279    }
280  }
281
282  /**
283   * Returns, as an immutable list, the row keys provided when the table was constructed, including
284   * those that are mapped to null values only.
285   */
286  public ImmutableList<R> rowKeyList() {
287    return rowList;
288  }
289
290  /**
291   * Returns, as an immutable list, the column keys provided when the table was constructed,
292   * including those that are mapped to null values only.
293   */
294  public ImmutableList<C> columnKeyList() {
295    return columnList;
296  }
297
298  /**
299   * Returns the value corresponding to the specified row and column indices. The same value is
300   * returned by {@code get(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex))}, but this
301   * method runs more quickly.
302   *
303   * @param rowIndex position of the row key in {@link #rowKeyList()}
304   * @param columnIndex position of the row key in {@link #columnKeyList()}
305   * @return the value with the specified row and column
306   * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than
307   *     or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal
308   *     to the number of allowed column keys
309   */
310  public V at(int rowIndex, int columnIndex) {
311    // In GWT array access never throws IndexOutOfBoundsException.
312    checkElementIndex(rowIndex, rowList.size());
313    checkElementIndex(columnIndex, columnList.size());
314    return array[rowIndex][columnIndex];
315  }
316
317  /**
318   * Associates {@code value} with the specified row and column indices. The logic {@code
319   * put(rowKeyList().get(rowIndex), columnKeyList().get(columnIndex), value)} has the same
320   * behavior, but this method runs more quickly.
321   *
322   * @param rowIndex position of the row key in {@link #rowKeyList()}
323   * @param columnIndex position of the row key in {@link #columnKeyList()}
324   * @param value value to store in the table
325   * @return the previous value with the specified row and column
326   * @throws IndexOutOfBoundsException if either index is negative, {@code rowIndex} is greater than
327   *     or equal to the number of allowed row keys, or {@code columnIndex} is greater than or equal
328   *     to the number of allowed column keys
329   */
330  @CanIgnoreReturnValue
331  public V set(int rowIndex, int columnIndex, @NullableDecl V value) {
332    // In GWT array access never throws IndexOutOfBoundsException.
333    checkElementIndex(rowIndex, rowList.size());
334    checkElementIndex(columnIndex, columnList.size());
335    V oldValue = array[rowIndex][columnIndex];
336    array[rowIndex][columnIndex] = value;
337    return oldValue;
338  }
339
340  /**
341   * Returns a two-dimensional array with the table contents. The row and column indices correspond
342   * to the positions of the row and column in the iterables provided during table construction. If
343   * the table lacks a mapping for a given row and column, the corresponding array element is null.
344   *
345   * <p>Subsequent table changes will not modify the array, and vice versa.
346   *
347   * @param valueClass class of values stored in the returned array
348   */
349  @GwtIncompatible // reflection
350  public V[][] toArray(Class<V> valueClass) {
351    @SuppressWarnings("unchecked") // TODO: safe?
352    V[][] copy = (V[][]) Array.newInstance(valueClass, rowList.size(), columnList.size());
353    for (int i = 0; i < rowList.size(); i++) {
354      System.arraycopy(array[i], 0, copy[i], 0, array[i].length);
355    }
356    return copy;
357  }
358
359  /**
360   * Not supported. Use {@link #eraseAll} instead.
361   *
362   * @throws UnsupportedOperationException always
363   * @deprecated Use {@link #eraseAll}
364   */
365  @Override
366  @Deprecated
367  public void clear() {
368    throw new UnsupportedOperationException();
369  }
370
371  /** Associates the value {@code null} with every pair of allowed row and column keys. */
372  public void eraseAll() {
373    for (V[] row : array) {
374      Arrays.fill(row, null);
375    }
376  }
377
378  /**
379   * Returns {@code true} if the provided keys are among the keys provided when the table was
380   * constructed.
381   */
382  @Override
383  public boolean contains(@NullableDecl Object rowKey, @NullableDecl Object columnKey) {
384    return containsRow(rowKey) && containsColumn(columnKey);
385  }
386
387  /**
388   * Returns {@code true} if the provided column key is among the column keys provided when the
389   * table was constructed.
390   */
391  @Override
392  public boolean containsColumn(@NullableDecl Object columnKey) {
393    return columnKeyToIndex.containsKey(columnKey);
394  }
395
396  /**
397   * Returns {@code true} if the provided row key is among the row keys provided when the table was
398   * constructed.
399   */
400  @Override
401  public boolean containsRow(@NullableDecl Object rowKey) {
402    return rowKeyToIndex.containsKey(rowKey);
403  }
404
405  @Override
406  public boolean containsValue(@NullableDecl Object value) {
407    for (V[] row : array) {
408      for (V element : row) {
409        if (Objects.equal(value, element)) {
410          return true;
411        }
412      }
413    }
414    return false;
415  }
416
417  @Override
418  public V get(@NullableDecl Object rowKey, @NullableDecl Object columnKey) {
419    Integer rowIndex = rowKeyToIndex.get(rowKey);
420    Integer columnIndex = columnKeyToIndex.get(columnKey);
421    return (rowIndex == null || columnIndex == null) ? null : at(rowIndex, columnIndex);
422  }
423
424  /**
425   * Returns {@code true} if {@code rowKeyList().size == 0} or {@code columnKeyList().size() == 0}.
426   */
427  @Override
428  public boolean isEmpty() {
429    return rowList.isEmpty() || columnList.isEmpty();
430  }
431
432  /**
433   * {@inheritDoc}
434   *
435   * @throws IllegalArgumentException if {@code rowKey} is not in {@link #rowKeySet()} or {@code
436   *     columnKey} is not in {@link #columnKeySet()}.
437   */
438  @CanIgnoreReturnValue
439  @Override
440  public V put(R rowKey, C columnKey, @NullableDecl V value) {
441    checkNotNull(rowKey);
442    checkNotNull(columnKey);
443    Integer rowIndex = rowKeyToIndex.get(rowKey);
444    checkArgument(rowIndex != null, "Row %s not in %s", rowKey, rowList);
445    Integer columnIndex = columnKeyToIndex.get(columnKey);
446    checkArgument(columnIndex != null, "Column %s not in %s", columnKey, columnList);
447    return set(rowIndex, columnIndex, value);
448  }
449
450  /*
451   * TODO(jlevy): Consider creating a merge() method, similar to putAll() but
452   * copying non-null values only.
453   */
454
455  /**
456   * {@inheritDoc}
457   *
458   * <p>If {@code table} is an {@code ArrayTable}, its null values will be stored in this table,
459   * possibly replacing values that were previously non-null.
460   *
461   * @throws NullPointerException if {@code table} has a null key
462   * @throws IllegalArgumentException if any of the provided table's row keys or column keys is not
463   *     in {@link #rowKeySet()} or {@link #columnKeySet()}
464   */
465  @Override
466  public void putAll(Table<? extends R, ? extends C, ? extends V> table) {
467    super.putAll(table);
468  }
469
470  /**
471   * Not supported. Use {@link #erase} instead.
472   *
473   * @throws UnsupportedOperationException always
474   * @deprecated Use {@link #erase}
475   */
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(@NullableDecl Object rowKey, @NullableDecl 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  @MonotonicNonNullDecl private transient 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  @MonotonicNonNullDecl private transient 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}