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