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