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