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)
050@ElementTypesAreNonnullByDefault
051public class HashBasedTable<R, C, V> extends StandardTable<R, C, V> {
052  private static class Factory<C, V> implements Supplier<Map<C, V>>, Serializable {
053    final int expectedSize;
054
055    Factory(int expectedSize) {
056      this.expectedSize = expectedSize;
057    }
058
059    @Override
060    public Map<C, V> get() {
061      return Maps.newLinkedHashMapWithExpectedSize(expectedSize);
062    }
063
064    private static final long serialVersionUID = 0;
065  }
066
067  /** Creates an empty {@code HashBasedTable}. */
068  public static <R, C, V> HashBasedTable<R, C, V> create() {
069    return new HashBasedTable<>(new LinkedHashMap<R, Map<C, V>>(), new Factory<C, V>(0));
070  }
071
072  /**
073   * Creates an empty {@code HashBasedTable} with the specified map sizes.
074   *
075   * @param expectedRows the expected number of distinct row keys
076   * @param expectedCellsPerRow the expected number of column key / value mappings in each row
077   * @throws IllegalArgumentException if {@code expectedRows} or {@code expectedCellsPerRow} is
078   *     negative
079   */
080  public static <R, C, V> HashBasedTable<R, C, V> create(
081      int expectedRows, int expectedCellsPerRow) {
082    checkNonnegative(expectedCellsPerRow, "expectedCellsPerRow");
083    Map<R, Map<C, V>> backingMap = Maps.newLinkedHashMapWithExpectedSize(expectedRows);
084    return new HashBasedTable<>(backingMap, new Factory<C, V>(expectedCellsPerRow));
085  }
086
087  /**
088   * Creates a {@code HashBasedTable} with the same mappings as the specified table.
089   *
090   * @param table the table to copy
091   * @throws NullPointerException if any of the row keys, column keys, or values in {@code table} is
092   *     null
093   */
094  public static <R, C, V> HashBasedTable<R, C, V> create(
095      Table<? extends R, ? extends C, ? extends V> table) {
096    HashBasedTable<R, C, V> result = create();
097    result.putAll(table);
098    return result;
099  }
100
101  HashBasedTable(Map<R, Map<C, V>> backingMap, Factory<C, V> factory) {
102    super(backingMap, factory);
103  }
104
105  private static final long serialVersionUID = 0;
106}