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.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static java.util.Objects.requireNonNull;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.base.Supplier;
025import java.io.Serializable;
026import java.util.Comparator;
027import java.util.Iterator;
028import java.util.Map;
029import java.util.NoSuchElementException;
030import java.util.Set;
031import java.util.SortedMap;
032import java.util.SortedSet;
033import java.util.TreeMap;
034import javax.annotation.CheckForNull;
035
036/**
037 * Implementation of {@code Table} whose row keys and column keys are ordered by their natural
038 * ordering or by supplied comparators. When constructing a {@code TreeBasedTable}, you may provide
039 * comparators for the row keys and the column keys, or you may use natural ordering for both.
040 *
041 * <p>The {@link #rowKeySet} method returns a {@link SortedSet} and the {@link #rowMap} method
042 * returns a {@link SortedMap}, instead of the {@link Set} and {@link Map} specified by the {@link
043 * Table} interface.
044 *
045 * <p>The views returned by {@link #column}, {@link #columnKeySet()}, and {@link #columnMap()} have
046 * iterators that don't support {@code remove()}. Otherwise, all optional operations are supported.
047 * Null row keys, columns keys, and values are not supported.
048 *
049 * <p>Lookups by row key are often faster than lookups by column key, because the data is stored in
050 * a {@code Map<R, Map<C, V>>}. A method call like {@code column(columnKey).get(rowKey)} still runs
051 * quickly, since the row key is provided. However, {@code column(columnKey).size()} takes longer,
052 * since an iteration across all row keys occurs.
053 *
054 * <p>Because a {@code TreeBasedTable} has unique sorted values for a given row, both {@code
055 * row(rowKey)} and {@code rowMap().get(rowKey)} are {@link SortedMap} instances, instead of the
056 * {@link Map} specified in the {@link Table} interface.
057 *
058 * <p>Note that this implementation is not synchronized. If multiple threads access this table
059 * concurrently and one of the threads modifies the table, it must be synchronized externally.
060 *
061 * <p>See the Guava User Guide article on <a href=
062 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table">{@code Table}</a>.
063 *
064 * @author Jared Levy
065 * @author Louis Wasserman
066 * @since 7.0
067 */
068@GwtCompatible(serializable = true)
069@ElementTypesAreNonnullByDefault
070public class TreeBasedTable<R, C, V> extends StandardRowSortedTable<R, C, V> {
071  private final Comparator<? super C> columnComparator;
072
073  private static class Factory<C, V> implements Supplier<TreeMap<C, V>>, Serializable {
074    final Comparator<? super C> comparator;
075
076    Factory(Comparator<? super C> comparator) {
077      this.comparator = comparator;
078    }
079
080    @Override
081    public TreeMap<C, V> get() {
082      return new TreeMap<>(comparator);
083    }
084
085    private static final long serialVersionUID = 0;
086  }
087
088  /**
089   * Creates an empty {@code TreeBasedTable} that uses the natural orderings of both row and column
090   * keys.
091   *
092   * <p>The method signature specifies {@code R extends Comparable} with a raw {@link Comparable},
093   * instead of {@code R extends Comparable<? super R>}, and the same for {@code C}. That's
094   * necessary to support classes defined without generics.
095   */
096  public static <R extends Comparable, C extends Comparable, V> TreeBasedTable<R, C, V> create() {
097    return new TreeBasedTable<>(Ordering.natural(), Ordering.natural());
098  }
099
100  /**
101   * Creates an empty {@code TreeBasedTable} that is ordered by the specified comparators.
102   *
103   * @param rowComparator the comparator that orders the row keys
104   * @param columnComparator the comparator that orders the column keys
105   */
106  public static <R, C, V> TreeBasedTable<R, C, V> create(
107      Comparator<? super R> rowComparator, Comparator<? super C> columnComparator) {
108    checkNotNull(rowComparator);
109    checkNotNull(columnComparator);
110    return new TreeBasedTable<>(rowComparator, columnComparator);
111  }
112
113  /**
114   * Creates a {@code TreeBasedTable} with the same mappings and sort order as the specified {@code
115   * TreeBasedTable}.
116   */
117  public static <R, C, V> TreeBasedTable<R, C, V> create(TreeBasedTable<R, C, ? extends V> table) {
118    TreeBasedTable<R, C, V> result =
119        new TreeBasedTable<>(table.rowComparator(), table.columnComparator());
120    result.putAll(table);
121    return result;
122  }
123
124  TreeBasedTable(Comparator<? super R> rowComparator, Comparator<? super C> columnComparator) {
125    super(new TreeMap<R, Map<C, V>>(rowComparator), new Factory<C, V>(columnComparator));
126    this.columnComparator = columnComparator;
127  }
128
129  // TODO(jlevy): Move to StandardRowSortedTable?
130
131  /**
132   * Returns the comparator that orders the rows. With natural ordering, {@link Ordering#natural()}
133   * is returned.
134   *
135   * @deprecated Use {@code table.rowKeySet().comparator()} instead.
136   */
137  @Deprecated
138  public Comparator<? super R> rowComparator() {
139    /*
140     * requireNonNull is safe because the factories require non-null Comparators, which they pass on
141     * to the backing collections.
142     */
143    return requireNonNull(rowKeySet().comparator());
144  }
145
146  /**
147   * Returns the comparator that orders the columns. With natural ordering, {@link
148   * Ordering#natural()} is returned.
149   *
150   * @deprecated Store the {@link Comparator} alongside the {@link Table}. Or, if you know that the
151   *     {@link Table} contains at least one value, you can retrieve the {@link Comparator} with:
152   *     {@code ((SortedMap<C, V>) table.rowMap().values().iterator().next()).comparator();}.
153   */
154  @Deprecated
155  public Comparator<? super C> columnComparator() {
156    return columnComparator;
157  }
158
159  // TODO(lowasser): make column return a SortedMap
160
161  /**
162   * {@inheritDoc}
163   *
164   * <p>Because a {@code TreeBasedTable} has unique sorted values for a given row, this method
165   * returns a {@link SortedMap}, instead of the {@link Map} specified in the {@link Table}
166   * interface.
167   *
168   * @since 10.0 (<a href="https://github.com/google/guava/wiki/Compatibility" >mostly
169   *     source-compatible</a> since 7.0)
170   */
171  @Override
172  public SortedMap<C, V> row(R rowKey) {
173    return new TreeRow(rowKey);
174  }
175
176  private class TreeRow extends Row implements SortedMap<C, V> {
177    @CheckForNull final C lowerBound;
178    @CheckForNull final C upperBound;
179
180    TreeRow(R rowKey) {
181      this(rowKey, null, null);
182    }
183
184    TreeRow(R rowKey, @CheckForNull C lowerBound, @CheckForNull C upperBound) {
185      super(rowKey);
186      this.lowerBound = lowerBound;
187      this.upperBound = upperBound;
188      checkArgument(
189          lowerBound == null || upperBound == null || compare(lowerBound, upperBound) <= 0);
190    }
191
192    @Override
193    public SortedSet<C> keySet() {
194      return new Maps.SortedKeySet<>(this);
195    }
196
197    @Override
198    public Comparator<? super C> comparator() {
199      return columnComparator();
200    }
201
202    int compare(Object a, Object b) {
203      // pretend we can compare anything
204      @SuppressWarnings("unchecked")
205      Comparator<Object> cmp = (Comparator<Object>) comparator();
206      return cmp.compare(a, b);
207    }
208
209    boolean rangeContains(@CheckForNull Object o) {
210      return o != null
211          && (lowerBound == null || compare(lowerBound, o) <= 0)
212          && (upperBound == null || compare(upperBound, o) > 0);
213    }
214
215    @Override
216    public SortedMap<C, V> subMap(C fromKey, C toKey) {
217      checkArgument(rangeContains(checkNotNull(fromKey)) && rangeContains(checkNotNull(toKey)));
218      return new TreeRow(rowKey, fromKey, toKey);
219    }
220
221    @Override
222    public SortedMap<C, V> headMap(C toKey) {
223      checkArgument(rangeContains(checkNotNull(toKey)));
224      return new TreeRow(rowKey, lowerBound, toKey);
225    }
226
227    @Override
228    public SortedMap<C, V> tailMap(C fromKey) {
229      checkArgument(rangeContains(checkNotNull(fromKey)));
230      return new TreeRow(rowKey, fromKey, upperBound);
231    }
232
233    @Override
234    public C firstKey() {
235      updateBackingRowMapField();
236      if (backingRowMap == null) {
237        throw new NoSuchElementException();
238      }
239      return ((SortedMap<C, V>) backingRowMap).firstKey();
240    }
241
242    @Override
243    public C lastKey() {
244      updateBackingRowMapField();
245      if (backingRowMap == null) {
246        throw new NoSuchElementException();
247      }
248      return ((SortedMap<C, V>) backingRowMap).lastKey();
249    }
250
251    @CheckForNull transient SortedMap<C, V> wholeRow;
252
253    // If the row was previously empty, we check if there's a new row here every time we're queried.
254    void updateWholeRowField() {
255      if (wholeRow == null || (wholeRow.isEmpty() && backingMap.containsKey(rowKey))) {
256        wholeRow = (SortedMap<C, V>) backingMap.get(rowKey);
257      }
258    }
259
260    @Override
261    @CheckForNull
262    SortedMap<C, V> computeBackingRowMap() {
263      updateWholeRowField();
264      SortedMap<C, V> map = wholeRow;
265      if (map != null) {
266        if (lowerBound != null) {
267          map = map.tailMap(lowerBound);
268        }
269        if (upperBound != null) {
270          map = map.headMap(upperBound);
271        }
272        return map;
273      }
274      return null;
275    }
276
277    @Override
278    void maintainEmptyInvariant() {
279      updateWholeRowField();
280      if (wholeRow != null && wholeRow.isEmpty()) {
281        backingMap.remove(rowKey);
282        wholeRow = null;
283        backingRowMap = null;
284      }
285    }
286
287    @Override
288    public boolean containsKey(@CheckForNull Object key) {
289      return rangeContains(key) && super.containsKey(key);
290    }
291
292    @Override
293    @CheckForNull
294    public V put(C key, V value) {
295      checkArgument(rangeContains(checkNotNull(key)));
296      return super.put(key, value);
297    }
298  }
299
300  // rowKeySet() and rowMap() are defined here so they appear in the Javadoc.
301
302  @Override
303  public SortedSet<R> rowKeySet() {
304    return super.rowKeySet();
305  }
306
307  @Override
308  public SortedMap<R, Map<C, V>> rowMap() {
309    return super.rowMap();
310  }
311
312  /** Overridden column iterator to return columns values in globally sorted order. */
313  @Override
314  Iterator<C> createColumnKeyIterator() {
315    Comparator<? super C> comparator = columnComparator();
316
317    Iterator<C> merged =
318        Iterators.mergeSorted(
319            Iterables.transform(
320                backingMap.values(), (Map<C, V> input) -> input.keySet().iterator()),
321            comparator);
322
323    return new AbstractIterator<C>() {
324      @CheckForNull C lastValue;
325
326      @Override
327      @CheckForNull
328      protected C computeNext() {
329        while (merged.hasNext()) {
330          C next = merged.next();
331          boolean duplicate = lastValue != null && comparator.compare(next, lastValue) == 0;
332
333          // Keep looping till we find a non-duplicate value.
334          if (!duplicate) {
335            lastValue = next;
336            return lastValue;
337          }
338        }
339
340        lastValue = null; // clear reference to unused data
341        return endOfData();
342      }
343    };
344  }
345
346  private static final long serialVersionUID = 0;
347}