001    /*
002     * Copyright (C) 2008 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    
021    import com.google.common.annotations.GwtCompatible;
022    import com.google.common.base.Supplier;
023    
024    import java.io.Serializable;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.annotation.Nullable;
029    
030    /**
031     * Implementation of {@link Table} using hash tables.
032     *
033     * <p>The views returned by {@link #column}, {@link #columnKeySet()}, and {@link
034     * #columnMap()} have iterators that don't support {@code remove()}. Otherwise,
035     * all optional operations are supported. Null row keys, columns keys, and
036     * values are not supported.
037     *
038     * <p>Lookups by row key are often faster than lookups by column key, because
039     * the data is stored in a {@code Map<R, Map<C, V>>}. A method call like {@code
040     * column(columnKey).get(rowKey)} still runs quickly, since the row key is
041     * provided. However, {@code column(columnKey).size()} takes longer, since an
042     * iteration across all row keys occurs.
043     *
044     * <p>Note that this implementation is not synchronized. If multiple threads
045     * access this table concurrently and one of the threads modifies the table, it
046     * must be synchronized externally.
047     * 
048     * <p>See the Guava User Guide article on <a href=
049     * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Table">
050     * {@code Table}</a>.
051     *
052     * @author Jared Levy
053     * @since 7.0
054     */
055    @GwtCompatible(serializable = true)
056    public class HashBasedTable<R, C, V> extends StandardTable<R, C, V> {
057      private static class Factory<C, V>
058          implements Supplier<Map<C, V>>, Serializable {
059        final int expectedSize;
060        Factory(int expectedSize) {
061          this.expectedSize = expectedSize;
062        }
063        public Map<C, V> get() {
064          return Maps.newHashMapWithExpectedSize(expectedSize);
065        }
066        private static final long serialVersionUID = 0;
067      }
068    
069      /**
070       * Creates an empty {@code HashBasedTable}.
071       */
072      public static <R, C, V> HashBasedTable<R, C, V> create() {
073        return new HashBasedTable<R, C, V>(
074            new HashMap<R, Map<C, V>>(), new Factory<C, V>(0));
075      }
076    
077      /**
078       * Creates an empty {@code HashBasedTable} with the specified map sizes.
079       *
080       * @param expectedRows the expected number of distinct row keys
081       * @param expectedCellsPerRow the expected number of column key / value
082       *     mappings in each row
083       * @throws IllegalArgumentException if {@code expectedRows} or {@code
084       *     expectedCellsPerRow} is negative
085       */
086      public static <R, C, V> HashBasedTable<R, C, V> create(
087          int expectedRows, int expectedCellsPerRow) {
088        checkArgument(expectedCellsPerRow >= 0);
089        Map<R, Map<C, V>> backingMap =
090            Maps.newHashMapWithExpectedSize(expectedRows);
091        return new HashBasedTable<R, C, V>(
092            backingMap, new Factory<C, V>(expectedCellsPerRow));
093      }
094    
095      /**
096       * Creates a {@code HashBasedTable} with the same mappings as the specified
097       * table.
098       *
099       * @param table the table to copy
100       * @throws NullPointerException if any of the row keys, column keys, or values
101       *     in {@code table} is null
102       */
103      public static <R, C, V> HashBasedTable<R, C, V> create(
104          Table<? extends R, ? extends C, ? extends V> table) {
105        HashBasedTable<R, C, V> result = create();
106        result.putAll(table);
107        return result;
108      }
109    
110      HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
111        super(backingMap, factory);
112      }
113    
114      // Overriding so NullPointerTester test passes.
115    
116      
117      @Override
118      public boolean contains(
119          @Nullable Object rowKey, @Nullable Object columnKey) {
120        return super.contains(rowKey, columnKey);
121      }
122    
123      
124      @Override
125      public boolean containsColumn(@Nullable Object columnKey) {
126        return super.containsColumn(columnKey);
127      }
128    
129      
130      @Override
131      public boolean containsRow(@Nullable Object rowKey) {
132        return super.containsRow(rowKey);
133      }
134    
135      
136      @Override
137      public boolean containsValue(@Nullable Object value) {
138        return super.containsValue(value);
139      }
140    
141      
142      @Override
143      public V get(@Nullable Object rowKey, @Nullable Object columnKey) {
144        return super.get(rowKey, columnKey);
145      }
146    
147      
148      @Override
149      public boolean equals(@Nullable Object obj) {
150        return super.equals(obj);
151      }
152    
153      
154      @Override
155      public V remove(
156          @Nullable Object rowKey, @Nullable Object columnKey) {
157        return super.remove(rowKey, columnKey);
158      }
159    
160      private static final long serialVersionUID = 0;
161    }