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