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;
020import static com.google.common.collect.Maps.newLinkedHashMapWithExpectedSize;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.annotations.J2ktIncompatible;
025import com.google.common.base.Supplier;
026import java.io.Serializable;
027import java.util.LinkedHashMap;
028import java.util.Map;
029
030/**
031 * Implementation of {@link Table} using linked hash tables. This guarantees predictable iteration
032 * order of the various views.
033 *
034 * <p>The views returned by {@link #column}, {@link #columnKeySet()}, and {@link #columnMap()} have
035 * iterators that don't support {@code remove()}. Otherwise, all optional operations are supported.
036 * Null row keys, columns keys, and values are not supported.
037 *
038 * <p>Lookups by row key are often faster than lookups by column key, because the data is stored in
039 * a {@code Map<R, Map<C, V>>}. A method call like {@code column(columnKey).get(rowKey)} still runs
040 * quickly, since the row key is provided. However, {@code column(columnKey).size()} takes longer,
041 * since an iteration across all row keys occurs.
042 *
043 * <p>Note that this implementation is not synchronized. If multiple threads access this table
044 * concurrently and one of the threads modifies the table, it must be synchronized externally.
045 *
046 * <p>See the Guava User Guide article on <a href=
047 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table">{@code Table}</a>.
048 *
049 * @author Jared Levy
050 * @since 7.0
051 */
052@GwtCompatible
053public final class HashBasedTable<R, C, V> extends StandardTable<R, C, V> {
054  private static final class Factory<C, V> implements Supplier<Map<C, V>>, Serializable {
055    final int expectedSize;
056
057    Factory(int expectedSize) {
058      this.expectedSize = expectedSize;
059    }
060
061    @Override
062    public Map<C, V> get() {
063      return newLinkedHashMapWithExpectedSize(expectedSize);
064    }
065
066    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
067  }
068
069  /** Creates an empty {@code HashBasedTable}. */
070  public static <R, C, V> HashBasedTable<R, C, V> create() {
071    return new HashBasedTable<>(new LinkedHashMap<R, Map<C, V>>(), new Factory<C, V>(0));
072  }
073
074  /**
075   * Creates an empty {@code HashBasedTable} with the specified map sizes.
076   *
077   * @param expectedRows the expected number of distinct row keys
078   * @param expectedCellsPerRow the expected number of column key / value mappings in each row
079   * @throws IllegalArgumentException if {@code expectedRows} or {@code expectedCellsPerRow} is
080   *     negative
081   */
082  public static <R, C, V> HashBasedTable<R, C, V> create(
083      int expectedRows, int expectedCellsPerRow) {
084    checkNonnegative(expectedCellsPerRow, "expectedCellsPerRow");
085    Map<R, Map<C, V>> backingMap = newLinkedHashMapWithExpectedSize(expectedRows);
086    return new HashBasedTable<>(backingMap, new Factory<>(expectedCellsPerRow));
087  }
088
089  /**
090   * Creates a {@code HashBasedTable} with the same mappings as the specified table.
091   *
092   * @param table the table to copy
093   * @throws NullPointerException if any of the row keys, column keys, or values in {@code table} is
094   *     null
095   */
096  public static <R, C, V> HashBasedTable<R, C, V> create(
097      Table<? extends R, ? extends C, ? extends V> table) {
098    HashBasedTable<R, C, V> result = create();
099    result.putAll(table);
100    return result;
101  }
102
103  private HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
104    super(backingMap, factory);
105  }
106
107  @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
108}