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