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
017package com.google.common.collect;
018
019import static com.google.common.collect.CollectPreconditions.checkNonnegative;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.base.Supplier;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import 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)
056public 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    @Override
064    public Map<C, V> get() {
065      return Maps.newHashMapWithExpectedSize(expectedSize);
066    }
067    private static final long serialVersionUID = 0;
068  }
069
070  /**
071   * Creates an empty {@code HashBasedTable}.
072   */
073  public static <R, C, V> HashBasedTable<R, C, V> create() {
074    return new HashBasedTable<R, C, V>(
075        new HashMap<R, Map<C, V>>(), new Factory<C, V>(0));
076  }
077
078  /**
079   * Creates an empty {@code HashBasedTable} with the specified map sizes.
080   *
081   * @param expectedRows the expected number of distinct row keys
082   * @param expectedCellsPerRow the expected number of column key / value
083   *     mappings in each row
084   * @throws IllegalArgumentException if {@code expectedRows} or {@code
085   *     expectedCellsPerRow} is negative
086   */
087  public static <R, C, V> HashBasedTable<R, C, V> create(
088      int expectedRows, int expectedCellsPerRow) {
089    checkNonnegative(expectedCellsPerRow, "expectedCellsPerRow");
090    Map<R, Map<C, V>> backingMap =
091        Maps.newHashMapWithExpectedSize(expectedRows);
092    return new HashBasedTable<R, C, V>(
093        backingMap, new Factory<C, V>(expectedCellsPerRow));
094  }
095
096  /**
097   * Creates a {@code HashBasedTable} with the same mappings as the specified
098   * table.
099   *
100   * @param table the table to copy
101   * @throws NullPointerException if any of the row keys, column keys, or values
102   *     in {@code table} is null
103   */
104  public static <R, C, V> HashBasedTable<R, C, V> create(
105      Table<? extends R, ? extends C, ? extends V> table) {
106    HashBasedTable<R, C, V> result = create();
107    result.putAll(table);
108    return result;
109  }
110
111  HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
112    super(backingMap, factory);
113  }
114
115  // Overriding so NullPointerTester test passes.
116
117  @Override public boolean contains(
118      @Nullable Object rowKey, @Nullable Object columnKey) {
119    return super.contains(rowKey, columnKey);
120  }
121
122  @Override public boolean containsColumn(@Nullable Object columnKey) {
123    return super.containsColumn(columnKey);
124  }
125
126  @Override public boolean containsRow(@Nullable Object rowKey) {
127    return super.containsRow(rowKey);
128  }
129
130  @Override public boolean containsValue(@Nullable Object value) {
131    return super.containsValue(value);
132  }
133
134  @Override public V get(@Nullable Object rowKey, @Nullable Object columnKey) {
135    return super.get(rowKey, columnKey);
136  }
137
138  @Override public boolean equals(@Nullable Object obj) {
139    return super.equals(obj);
140  }
141
142  @Override public V remove(
143      @Nullable Object rowKey, @Nullable Object columnKey) {
144    return super.remove(rowKey, columnKey);
145  }
146
147  private static final long serialVersionUID = 0;
148}