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