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