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