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