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.qual.MonotonicNonNull;
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     * 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    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  @Override
364  @Deprecated
365  public void clear() {
366    throw new UnsupportedOperationException();
367  }
368
369  /** Associates the value {@code null} with every pair of allowed row and column keys. */
370  public void eraseAll() {
371    for (V[] row : array) {
372      Arrays.fill(row, null);
373    }
374  }
375
376  /**
377   * Returns {@code true} if the provided keys are among the keys provided when the table was
378   * constructed.
379   */
380  @Override
381  public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) {
382    return containsRow(rowKey) && containsColumn(columnKey);
383  }
384
385  /**
386   * Returns {@code true} if the provided column key is among the column keys provided when the
387   * table was constructed.
388   */
389  @Override
390  public boolean containsColumn(@Nullable Object columnKey) {
391    return columnKeyToIndex.containsKey(columnKey);
392  }
393
394  /**
395   * Returns {@code true} if the provided row key is among the row keys provided when the table was
396   * constructed.
397   */
398  @Override
399  public boolean containsRow(@Nullable Object rowKey) {
400    return rowKeyToIndex.containsKey(rowKey);
401  }
402
403  @Override
404  public boolean containsValue(@Nullable Object value) {
405    for (V[] row : array) {
406      for (V element : row) {
407        if (Objects.equal(value, element)) {
408          return true;
409        }
410      }
411    }
412    return false;
413  }
414
415  @Override
416  public V get(@Nullable Object rowKey, @Nullable Object columnKey) {
417    Integer rowIndex = rowKeyToIndex.get(rowKey);
418    Integer columnIndex = columnKeyToIndex.get(columnKey);
419    return (rowIndex == null || columnIndex == null) ? null : at(rowIndex, columnIndex);
420  }
421
422  /**
423   * Returns {@code true} if {@code rowKeyList().size == 0} or {@code columnKeyList().size() == 0}.
424   */
425  @Override
426  public boolean isEmpty() {
427    return rowList.isEmpty() || columnList.isEmpty();
428  }
429
430  /**
431   * {@inheritDoc}
432   *
433   * @throws IllegalArgumentException if {@code rowKey} is not in {@link #rowKeySet()} or {@code
434   *     columnKey} is not in {@link #columnKeySet()}.
435   */
436  @CanIgnoreReturnValue
437  @Override
438  public V put(R rowKey, C columnKey, @Nullable V value) {
439    checkNotNull(rowKey);
440    checkNotNull(columnKey);
441    Integer rowIndex = rowKeyToIndex.get(rowKey);
442    checkArgument(rowIndex != null, "Row %s not in %s", rowKey, rowList);
443    Integer columnIndex = columnKeyToIndex.get(columnKey);
444    checkArgument(columnIndex != null, "Column %s not in %s", columnKey, columnList);
445    return set(rowIndex, columnIndex, value);
446  }
447
448  /*
449   * TODO(jlevy): Consider creating a merge() method, similar to putAll() but
450   * copying non-null values only.
451   */
452
453  /**
454   * {@inheritDoc}
455   *
456   * <p>If {@code table} is an {@code ArrayTable}, its null values will be stored in this table,
457   * possibly replacing values that were previously non-null.
458   *
459   * @throws NullPointerException if {@code table} has a null key
460   * @throws IllegalArgumentException if any of the provided table's row keys or column keys is not
461   *     in {@link #rowKeySet()} or {@link #columnKeySet()}
462   */
463  @Override
464  public void putAll(Table<? extends R, ? extends C, ? extends V> table) {
465    super.putAll(table);
466  }
467
468  /**
469   * Not supported. Use {@link #erase} instead.
470   *
471   * @throws UnsupportedOperationException always
472   * @deprecated Use {@link #erase}
473   */
474  @CanIgnoreReturnValue
475  @Override
476  @Deprecated
477  public V remove(Object rowKey, Object columnKey) {
478    throw new UnsupportedOperationException();
479  }
480
481  /**
482   * Associates the value {@code null} with the specified keys, assuming both keys are valid. If
483   * either key is null or isn't among the keys provided during construction, this method has no
484   * effect.
485   *
486   * <p>This method is equivalent to {@code put(rowKey, columnKey, null)} when both provided keys
487   * are valid.
488   *
489   * @param rowKey row key of mapping to be erased
490   * @param columnKey column key of mapping to be erased
491   * @return the value previously associated with the keys, or {@code null} if no mapping existed
492   *     for the keys
493   */
494  @CanIgnoreReturnValue
495  public V erase(@Nullable Object rowKey, @Nullable Object columnKey) {
496    Integer rowIndex = rowKeyToIndex.get(rowKey);
497    Integer columnIndex = columnKeyToIndex.get(columnKey);
498    if (rowIndex == null || columnIndex == null) {
499      return null;
500    }
501    return set(rowIndex, columnIndex, null);
502  }
503
504  // TODO(jlevy): Add eraseRow and eraseColumn methods?
505
506  @Override
507  public int size() {
508    return rowList.size() * columnList.size();
509  }
510
511  /**
512   * Returns an unmodifiable set of all row key / column key / value triplets. Changes to the table
513   * will update the returned set.
514   *
515   * <p>The returned set's iterator traverses the mappings with the first row key, the mappings with
516   * the second row key, and so on.
517   *
518   * <p>The value in the returned cells may change if the table subsequently changes.
519   *
520   * @return set of table cells consisting of row key / column key / value triplets
521   */
522  @Override
523  public Set<Cell<R, C, V>> cellSet() {
524    return super.cellSet();
525  }
526
527  @Override
528  Iterator<Cell<R, C, V>> cellIterator() {
529    return new AbstractIndexedListIterator<Cell<R, C, V>>(size()) {
530      @Override
531      protected Cell<R, C, V> get(final int index) {
532        return getCell(index);
533      }
534    };
535  }
536
537  @Override
538  Spliterator<Cell<R, C, V>> cellSpliterator() {
539    return CollectSpliterators.indexed(
540        size(), Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT, this::getCell);
541  }
542
543  private Cell<R, C, V> getCell(final int index) {
544    return new Tables.AbstractCell<R, C, V>() {
545      final int rowIndex = index / columnList.size();
546      final int columnIndex = index % columnList.size();
547
548      @Override
549      public R getRowKey() {
550        return rowList.get(rowIndex);
551      }
552
553      @Override
554      public C getColumnKey() {
555        return columnList.get(columnIndex);
556      }
557
558      @Override
559      public V getValue() {
560        return at(rowIndex, columnIndex);
561      }
562    };
563  }
564
565  private V getValue(int index) {
566    int rowIndex = index / columnList.size();
567    int columnIndex = index % columnList.size();
568    return at(rowIndex, columnIndex);
569  }
570
571  /**
572   * Returns a view of all mappings that have the given column key. If the column key isn't in
573   * {@link #columnKeySet()}, an empty immutable map is returned.
574   *
575   * <p>Otherwise, for each row key in {@link #rowKeySet()}, the returned map associates the row key
576   * with the corresponding value in the table. Changes to the returned map will update the
577   * underlying table, and vice versa.
578   *
579   * @param columnKey key of column to search for in the table
580   * @return the corresponding map from row keys to values
581   */
582  @Override
583  public Map<R, V> column(C columnKey) {
584    checkNotNull(columnKey);
585    Integer columnIndex = columnKeyToIndex.get(columnKey);
586    return (columnIndex == null) ? ImmutableMap.<R, V>of() : new Column(columnIndex);
587  }
588
589  private class Column extends ArrayMap<R, V> {
590    final int columnIndex;
591
592    Column(int columnIndex) {
593      super(rowKeyToIndex);
594      this.columnIndex = columnIndex;
595    }
596
597    @Override
598    String getKeyRole() {
599      return "Row";
600    }
601
602    @Override
603    V getValue(int index) {
604      return at(index, columnIndex);
605    }
606
607    @Override
608    V setValue(int index, V newValue) {
609      return set(index, columnIndex, newValue);
610    }
611  }
612
613  /**
614   * Returns an immutable set of the valid column keys, including those that are associated with
615   * null values only.
616   *
617   * @return immutable set of column keys
618   */
619  @Override
620  public ImmutableSet<C> columnKeySet() {
621    return columnKeyToIndex.keySet();
622  }
623
624  private transient @MonotonicNonNull ColumnMap columnMap;
625
626  @Override
627  public Map<C, Map<R, V>> columnMap() {
628    ColumnMap map = columnMap;
629    return (map == null) ? columnMap = new ColumnMap() : map;
630  }
631
632  @WeakOuter
633  private class ColumnMap extends ArrayMap<C, Map<R, V>> {
634    private ColumnMap() {
635      super(columnKeyToIndex);
636    }
637
638    @Override
639    String getKeyRole() {
640      return "Column";
641    }
642
643    @Override
644    Map<R, V> getValue(int index) {
645      return new Column(index);
646    }
647
648    @Override
649    Map<R, V> setValue(int index, Map<R, V> newValue) {
650      throw new UnsupportedOperationException();
651    }
652
653    @Override
654    public Map<R, V> put(C key, Map<R, V> value) {
655      throw new UnsupportedOperationException();
656    }
657  }
658
659  /**
660   * Returns a view of all mappings that have the given row key. If the row key isn't in {@link
661   * #rowKeySet()}, an empty immutable map is returned.
662   *
663   * <p>Otherwise, for each column key in {@link #columnKeySet()}, the returned map associates the
664   * column key with the corresponding value in the table. Changes to the returned map will update
665   * the underlying table, and vice versa.
666   *
667   * @param rowKey key of row to search for in the table
668   * @return the corresponding map from column keys to values
669   */
670  @Override
671  public Map<C, V> row(R rowKey) {
672    checkNotNull(rowKey);
673    Integer rowIndex = rowKeyToIndex.get(rowKey);
674    return (rowIndex == null) ? ImmutableMap.<C, V>of() : new Row(rowIndex);
675  }
676
677  private class Row extends ArrayMap<C, V> {
678    final int rowIndex;
679
680    Row(int rowIndex) {
681      super(columnKeyToIndex);
682      this.rowIndex = rowIndex;
683    }
684
685    @Override
686    String getKeyRole() {
687      return "Column";
688    }
689
690    @Override
691    V getValue(int index) {
692      return at(rowIndex, index);
693    }
694
695    @Override
696    V setValue(int index, V newValue) {
697      return set(rowIndex, index, newValue);
698    }
699  }
700
701  /**
702   * Returns an immutable set of the valid row keys, including those that are associated with null
703   * values only.
704   *
705   * @return immutable set of row keys
706   */
707  @Override
708  public ImmutableSet<R> rowKeySet() {
709    return rowKeyToIndex.keySet();
710  }
711
712  private transient @MonotonicNonNull RowMap rowMap;
713
714  @Override
715  public Map<R, Map<C, V>> rowMap() {
716    RowMap map = rowMap;
717    return (map == null) ? rowMap = new RowMap() : map;
718  }
719
720  @WeakOuter
721  private class RowMap extends ArrayMap<R, Map<C, V>> {
722    private RowMap() {
723      super(rowKeyToIndex);
724    }
725
726    @Override
727    String getKeyRole() {
728      return "Row";
729    }
730
731    @Override
732    Map<C, V> getValue(int index) {
733      return new Row(index);
734    }
735
736    @Override
737    Map<C, V> setValue(int index, Map<C, V> newValue) {
738      throw new UnsupportedOperationException();
739    }
740
741    @Override
742    public Map<C, V> put(R key, Map<C, V> value) {
743      throw new UnsupportedOperationException();
744    }
745  }
746
747  /**
748   * Returns an unmodifiable collection of all values, which may contain duplicates. Changes to the
749   * table will update the returned collection.
750   *
751   * <p>The returned collection's iterator traverses the values of the first row key, the values of
752   * the second row key, and so on.
753   *
754   * @return collection of values
755   */
756  @Override
757  public Collection<V> values() {
758    return super.values();
759  }
760
761  @Override
762  Iterator<V> valuesIterator() {
763    return new AbstractIndexedListIterator<V>(size()) {
764      @Override
765      protected V get(int index) {
766        return getValue(index);
767      }
768    };
769  }
770
771  @Override
772  Spliterator<V> valuesSpliterator() {
773    return CollectSpliterators.indexed(size(), Spliterator.ORDERED, this::getValue);
774  }
775
776  private static final long serialVersionUID = 0;
777}