• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import static com.google.common.collect.CollectPreconditions.checkNonnegative;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.base.Supplier;
23 import java.io.Serializable;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 
27 /**
28  * Implementation of {@link Table} using linked hash tables. This guarantees predictable iteration
29  * order of the various views.
30  *
31  * <p>The views returned by {@link #column}, {@link #columnKeySet()}, and {@link #columnMap()} have
32  * iterators that don't support {@code remove()}. Otherwise, all optional operations are supported.
33  * Null row keys, columns keys, and values are not supported.
34  *
35  * <p>Lookups by row key are often faster than lookups by column key, because the data is stored in
36  * a {@code Map<R, Map<C, V>>}. A method call like {@code column(columnKey).get(rowKey)} still runs
37  * quickly, since the row key is provided. However, {@code column(columnKey).size()} takes longer,
38  * since an iteration across all row keys occurs.
39  *
40  * <p>Note that this implementation is not synchronized. If multiple threads access this table
41  * concurrently and one of the threads modifies the table, it must be synchronized externally.
42  *
43  * <p>See the Guava User Guide article on <a href=
44  * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table"> {@code Table}</a>.
45  *
46  * @author Jared Levy
47  * @since 7.0
48  */
49 @GwtCompatible(serializable = true)
50 @ElementTypesAreNonnullByDefault
51 public class HashBasedTable<R, C, V> extends StandardTable<R, C, V> {
52   private static class Factory<C, V> implements Supplier<Map<C, V>>, Serializable {
53     final int expectedSize;
54 
Factory(int expectedSize)55     Factory(int expectedSize) {
56       this.expectedSize = expectedSize;
57     }
58 
59     @Override
get()60     public Map<C, V> get() {
61       return Maps.newLinkedHashMapWithExpectedSize(expectedSize);
62     }
63 
64     private static final long serialVersionUID = 0;
65   }
66 
67   /** Creates an empty {@code HashBasedTable}. */
create()68   public static <R, C, V> HashBasedTable<R, C, V> create() {
69     return new HashBasedTable<>(new LinkedHashMap<R, Map<C, V>>(), new Factory<C, V>(0));
70   }
71 
72   /**
73    * Creates an empty {@code HashBasedTable} with the specified map sizes.
74    *
75    * @param expectedRows the expected number of distinct row keys
76    * @param expectedCellsPerRow the expected number of column key / value mappings in each row
77    * @throws IllegalArgumentException if {@code expectedRows} or {@code expectedCellsPerRow} is
78    *     negative
79    */
create( int expectedRows, int expectedCellsPerRow)80   public static <R, C, V> HashBasedTable<R, C, V> create(
81       int expectedRows, int expectedCellsPerRow) {
82     checkNonnegative(expectedCellsPerRow, "expectedCellsPerRow");
83     Map<R, Map<C, V>> backingMap = Maps.newLinkedHashMapWithExpectedSize(expectedRows);
84     return new HashBasedTable<>(backingMap, new Factory<C, V>(expectedCellsPerRow));
85   }
86 
87   /**
88    * Creates a {@code HashBasedTable} with the same mappings as the specified table.
89    *
90    * @param table the table to copy
91    * @throws NullPointerException if any of the row keys, column keys, or values in {@code table} is
92    *     null
93    */
create( Table<? extends R, ? extends C, ? extends V> table)94   public static <R, C, V> HashBasedTable<R, C, V> create(
95       Table<? extends R, ? extends C, ? extends V> table) {
96     HashBasedTable<R, C, V> result = create();
97     result.putAll(table);
98     return result;
99   }
100 
HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory)101   HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
102     super(backingMap, factory);
103   }
104 
105   private static final long serialVersionUID = 0;
106 }
107