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