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.NullnessCasts.uncheckedCastNullableTToT;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.annotations.J2ktIncompatible;
025import com.google.common.base.Function;
026import com.google.common.base.Objects;
027import com.google.common.base.Supplier;
028import com.google.common.collect.Table.Cell;
029import java.io.Serializable;
030import java.util.Collection;
031import java.util.Collections;
032import java.util.Iterator;
033import java.util.Map;
034import java.util.Set;
035import java.util.SortedMap;
036import java.util.SortedSet;
037import java.util.function.BinaryOperator;
038import java.util.stream.Collector;
039import org.jspecify.annotations.Nullable;
040
041/**
042 * Provides static methods that involve a {@code Table}.
043 *
044 * <p>See the Guava User Guide article on <a href=
045 * "https://github.com/google/guava/wiki/CollectionUtilitiesExplained#tables">{@code Tables}</a>.
046 *
047 * @author Jared Levy
048 * @author Louis Wasserman
049 * @since 7.0
050 */
051@GwtCompatible
052public final class Tables {
053  private Tables() {}
054
055  /**
056   * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the
057   * specified supplier, whose cells are generated by applying the provided mapping functions to the
058   * input elements. Cells are inserted into the generated {@code Table} in encounter order.
059   *
060   * <p>If multiple input elements map to the same row and column, an {@code IllegalStateException}
061   * is thrown when the collection operation is performed.
062   *
063   * <p>To collect to an {@link ImmutableTable}, use {@link ImmutableTable#toImmutableTable}.
064   *
065   * @since 33.2.0 (available since 21.0 in guava-jre)
066   */
067  @SuppressWarnings("Java7ApiChecker")
068  @IgnoreJRERequirement // Users will use this only if they're already using streams.
069  public static <
070          T extends @Nullable Object,
071          R extends @Nullable Object,
072          C extends @Nullable Object,
073          V,
074          I extends Table<R, C, V>>
075      Collector<T, ?, I> toTable(
076          java.util.function.Function<? super T, ? extends R> rowFunction,
077          java.util.function.Function<? super T, ? extends C> columnFunction,
078          java.util.function.Function<? super T, ? extends V> valueFunction,
079          java.util.function.Supplier<I> tableSupplier) {
080    return TableCollectors.<T, R, C, V, I>toTable(
081        rowFunction, columnFunction, valueFunction, tableSupplier);
082  }
083
084  /**
085   * Returns a {@link Collector} that accumulates elements into a {@code Table} created using the
086   * specified supplier, whose cells are generated by applying the provided mapping functions to the
087   * input elements. Cells are inserted into the generated {@code Table} in encounter order.
088   *
089   * <p>If multiple input elements map to the same row and column, the specified merging function is
090   * used to combine the values. Like {@link
091   * java.util.stream.Collectors#toMap(java.util.function.Function, java.util.function.Function,
092   * BinaryOperator, java.util.function.Supplier)}, this Collector throws a {@code
093   * NullPointerException} on null values returned from {@code valueFunction}, and treats nulls
094   * returned from {@code mergeFunction} as removals of that row/column pair.
095   *
096   * @since 33.2.0 (available since 21.0 in guava-jre)
097   */
098  @SuppressWarnings("Java7ApiChecker")
099  @IgnoreJRERequirement // Users will use this only if they're already using streams.
100  public static <
101          T extends @Nullable Object,
102          R extends @Nullable Object,
103          C extends @Nullable Object,
104          V,
105          I extends Table<R, C, V>>
106      Collector<T, ?, I> toTable(
107          java.util.function.Function<? super T, ? extends R> rowFunction,
108          java.util.function.Function<? super T, ? extends C> columnFunction,
109          java.util.function.Function<? super T, ? extends V> valueFunction,
110          BinaryOperator<V> mergeFunction,
111          java.util.function.Supplier<I> tableSupplier) {
112    return TableCollectors.<T, R, C, V, I>toTable(
113        rowFunction, columnFunction, valueFunction, mergeFunction, tableSupplier);
114  }
115
116  /**
117   * Returns an immutable cell with the specified row key, column key, and value.
118   *
119   * <p>The returned cell is serializable.
120   *
121   * @param rowKey the row key to be associated with the returned cell
122   * @param columnKey the column key to be associated with the returned cell
123   * @param value the value to be associated with the returned cell
124   */
125  public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
126      Cell<R, C, V> immutableCell(
127          @ParametricNullness R rowKey,
128          @ParametricNullness C columnKey,
129          @ParametricNullness V value) {
130    return new ImmutableCell<>(rowKey, columnKey, value);
131  }
132
133  static final class ImmutableCell<
134          R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
135      extends AbstractCell<R, C, V> implements Serializable {
136    @ParametricNullness private final R rowKey;
137    @ParametricNullness private final C columnKey;
138    @ParametricNullness private final V value;
139
140    ImmutableCell(
141        @ParametricNullness R rowKey,
142        @ParametricNullness C columnKey,
143        @ParametricNullness V value) {
144      this.rowKey = rowKey;
145      this.columnKey = columnKey;
146      this.value = value;
147    }
148
149    @Override
150    @ParametricNullness
151    public R getRowKey() {
152      return rowKey;
153    }
154
155    @Override
156    @ParametricNullness
157    public C getColumnKey() {
158      return columnKey;
159    }
160
161    @Override
162    @ParametricNullness
163    public V getValue() {
164      return value;
165    }
166
167    private static final long serialVersionUID = 0;
168  }
169
170  abstract static class AbstractCell<
171          R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
172      implements Cell<R, C, V> {
173    // needed for serialization
174    AbstractCell() {}
175
176    @Override
177    public boolean equals(@Nullable Object obj) {
178      if (obj == this) {
179        return true;
180      }
181      if (obj instanceof Cell) {
182        Cell<?, ?, ?> other = (Cell<?, ?, ?>) obj;
183        return Objects.equal(getRowKey(), other.getRowKey())
184            && Objects.equal(getColumnKey(), other.getColumnKey())
185            && Objects.equal(getValue(), other.getValue());
186      }
187      return false;
188    }
189
190    @Override
191    public int hashCode() {
192      return Objects.hashCode(getRowKey(), getColumnKey(), getValue());
193    }
194
195    @Override
196    public String toString() {
197      return "(" + getRowKey() + "," + getColumnKey() + ")=" + getValue();
198    }
199  }
200
201  /**
202   * Creates a transposed view of a given table that flips its row and column keys. In other words,
203   * calling {@code get(columnKey, rowKey)} on the generated table always returns the same value as
204   * calling {@code get(rowKey, columnKey)} on the original table. Updating the original table
205   * changes the contents of the transposed table and vice versa.
206   *
207   * <p>The returned table supports update operations as long as the input table supports the
208   * analogous operation with swapped rows and columns. For example, in a {@link HashBasedTable}
209   * instance, {@code rowKeySet().iterator()} supports {@code remove()} but {@code
210   * columnKeySet().iterator()} doesn't. With a transposed {@link HashBasedTable}, it's the other
211   * way around.
212   */
213  public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
214      Table<C, R, V> transpose(Table<R, C, V> table) {
215    return (table instanceof TransposeTable)
216        ? ((TransposeTable<R, C, V>) table).original
217        : new TransposeTable<C, R, V>(table);
218  }
219
220  private static class TransposeTable<
221          C extends @Nullable Object, R extends @Nullable Object, V extends @Nullable Object>
222      extends AbstractTable<C, R, V> {
223    final Table<R, C, V> original;
224
225    TransposeTable(Table<R, C, V> original) {
226      this.original = checkNotNull(original);
227    }
228
229    @Override
230    public void clear() {
231      original.clear();
232    }
233
234    @Override
235    public Map<C, V> column(@ParametricNullness R columnKey) {
236      return original.row(columnKey);
237    }
238
239    @Override
240    public Set<R> columnKeySet() {
241      return original.rowKeySet();
242    }
243
244    @Override
245    public Map<R, Map<C, V>> columnMap() {
246      return original.rowMap();
247    }
248
249    @Override
250    public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) {
251      return original.contains(columnKey, rowKey);
252    }
253
254    @Override
255    public boolean containsColumn(@Nullable Object columnKey) {
256      return original.containsRow(columnKey);
257    }
258
259    @Override
260    public boolean containsRow(@Nullable Object rowKey) {
261      return original.containsColumn(rowKey);
262    }
263
264    @Override
265    public boolean containsValue(@Nullable Object value) {
266      return original.containsValue(value);
267    }
268
269    @Override
270    public @Nullable V get(@Nullable Object rowKey, @Nullable Object columnKey) {
271      return original.get(columnKey, rowKey);
272    }
273
274    @Override
275    public @Nullable V put(
276        @ParametricNullness C rowKey,
277        @ParametricNullness R columnKey,
278        @ParametricNullness V value) {
279      return original.put(columnKey, rowKey, value);
280    }
281
282    @Override
283    public void putAll(Table<? extends C, ? extends R, ? extends V> table) {
284      original.putAll(transpose(table));
285    }
286
287    @Override
288    public @Nullable V remove(@Nullable Object rowKey, @Nullable Object columnKey) {
289      return original.remove(columnKey, rowKey);
290    }
291
292    @Override
293    public Map<R, V> row(@ParametricNullness C rowKey) {
294      return original.column(rowKey);
295    }
296
297    @Override
298    public Set<C> rowKeySet() {
299      return original.columnKeySet();
300    }
301
302    @Override
303    public Map<C, Map<R, V>> rowMap() {
304      return original.columnMap();
305    }
306
307    @Override
308    public int size() {
309      return original.size();
310    }
311
312    @Override
313    public Collection<V> values() {
314      return original.values();
315    }
316
317    @Override
318    Iterator<Cell<C, R, V>> cellIterator() {
319      return Iterators.transform(original.cellSet().iterator(), Tables::transposeCell);
320    }
321  }
322
323  private static <
324          R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
325      Cell<C, R, V> transposeCell(Cell<R, C, V> cell) {
326    return immutableCell(cell.getColumnKey(), cell.getRowKey(), cell.getValue());
327  }
328
329  /**
330   * Creates a table that uses the specified backing map and factory. It can generate a table based
331   * on arbitrary {@link Map} classes.
332   *
333   * <p>The {@code factory}-generated and {@code backingMap} classes determine the table iteration
334   * order. However, the table's {@code row()} method returns instances of a different class than
335   * {@code factory.get()} does.
336   *
337   * <p>Call this method only when the simpler factory methods in classes like {@link
338   * HashBasedTable} and {@link TreeBasedTable} won't suffice.
339   *
340   * <p>The views returned by the {@code Table} methods {@link Table#column}, {@link
341   * Table#columnKeySet}, and {@link Table#columnMap} have iterators that don't support {@code
342   * remove()}. Otherwise, all optional operations are supported. Null row keys, columns keys, and
343   * values are not supported.
344   *
345   * <p>Lookups by row key are often faster than lookups by column key, because the data is stored
346   * in a {@code Map<R, Map<C, V>>}. A method call like {@code column(columnKey).get(rowKey)} still
347   * runs quickly, since the row key is provided. However, {@code column(columnKey).size()} takes
348   * longer, since an iteration across all row keys occurs.
349   *
350   * <p>Note that this implementation is not synchronized. If multiple threads access this table
351   * concurrently and one of the threads modifies the table, it must be synchronized externally.
352   *
353   * <p>The table is serializable if {@code backingMap}, {@code factory}, the maps generated by
354   * {@code factory}, and the table contents are all serializable.
355   *
356   * <p>Note: the table assumes complete ownership over of {@code backingMap} and the maps returned
357   * by {@code factory}. Those objects should not be manually updated and they should not use soft,
358   * weak, or phantom references.
359   *
360   * @param backingMap place to store the mapping from each row key to its corresponding column key
361   *     / value map
362   * @param factory supplier of new, empty maps that will each hold all column key / value mappings
363   *     for a given row key
364   * @throws IllegalArgumentException if {@code backingMap} is not empty
365   * @since 10.0
366   */
367  public static <R, C, V> Table<R, C, V> newCustomTable(
368      Map<R, Map<C, V>> backingMap, Supplier<? extends Map<C, V>> factory) {
369    checkArgument(backingMap.isEmpty());
370    checkNotNull(factory);
371    // TODO(jlevy): Wrap factory to validate that the supplied maps are empty?
372    return new StandardTable<>(backingMap, factory);
373  }
374
375  /**
376   * Returns a view of a table where each value is transformed by a function. All other properties
377   * of the table, such as iteration order, are left intact.
378   *
379   * <p>Changes in the underlying table are reflected in this view. Conversely, this view supports
380   * removal operations, and these are reflected in the underlying table.
381   *
382   * <p>It's acceptable for the underlying table to contain null keys, and even null values provided
383   * that the function is capable of accepting null input. The transformed table might contain null
384   * values, if the function sometimes gives a null result.
385   *
386   * <p>The returned table is not thread-safe or serializable, even if the underlying table is.
387   *
388   * <p>The function is applied lazily, invoked when needed. This is necessary for the returned
389   * table to be a view, but it means that the function will be applied many times for bulk
390   * operations like {@link Table#containsValue} and {@code Table.toString()}. For this to perform
391   * well, {@code function} should be fast. To avoid lazy evaluation when the returned table doesn't
392   * need to be a view, copy the returned table into a new table of your choosing.
393   *
394   * @since 10.0
395   */
396  public static <
397          R extends @Nullable Object,
398          C extends @Nullable Object,
399          V1 extends @Nullable Object,
400          V2 extends @Nullable Object>
401      Table<R, C, V2> transformValues(
402          Table<R, C, V1> fromTable, Function<? super V1, V2> function) {
403    return new TransformedTable<>(fromTable, function);
404  }
405
406  private static class TransformedTable<
407          R extends @Nullable Object,
408          C extends @Nullable Object,
409          V1 extends @Nullable Object,
410          V2 extends @Nullable Object>
411      extends AbstractTable<R, C, V2> {
412    final Table<R, C, V1> fromTable;
413    final Function<? super V1, V2> function;
414
415    TransformedTable(Table<R, C, V1> fromTable, Function<? super V1, V2> function) {
416      this.fromTable = checkNotNull(fromTable);
417      this.function = checkNotNull(function);
418    }
419
420    @Override
421    public boolean contains(@Nullable Object rowKey, @Nullable Object columnKey) {
422      return fromTable.contains(rowKey, columnKey);
423    }
424
425    @Override
426    public @Nullable V2 get(@Nullable Object rowKey, @Nullable Object columnKey) {
427      // The function is passed a null input only when the table contains a null
428      // value.
429      // The cast is safe because of the contains() check.
430      return contains(rowKey, columnKey)
431          ? function.apply(uncheckedCastNullableTToT(fromTable.get(rowKey, columnKey)))
432          : null;
433    }
434
435    @Override
436    public int size() {
437      return fromTable.size();
438    }
439
440    @Override
441    public void clear() {
442      fromTable.clear();
443    }
444
445    @Override
446    public @Nullable V2 put(
447        @ParametricNullness R rowKey,
448        @ParametricNullness C columnKey,
449        @ParametricNullness V2 value) {
450      throw new UnsupportedOperationException();
451    }
452
453    @Override
454    public void putAll(Table<? extends R, ? extends C, ? extends V2> table) {
455      throw new UnsupportedOperationException();
456    }
457
458    @Override
459    public @Nullable V2 remove(@Nullable Object rowKey, @Nullable Object columnKey) {
460      return contains(rowKey, columnKey)
461          // The cast is safe because of the contains() check.
462          ? function.apply(uncheckedCastNullableTToT(fromTable.remove(rowKey, columnKey)))
463          : null;
464    }
465
466    @Override
467    public Map<C, V2> row(@ParametricNullness R rowKey) {
468      return Maps.transformValues(fromTable.row(rowKey), function);
469    }
470
471    @Override
472    public Map<R, V2> column(@ParametricNullness C columnKey) {
473      return Maps.transformValues(fromTable.column(columnKey), function);
474    }
475
476    Function<Cell<R, C, V1>, Cell<R, C, V2>> cellFunction() {
477      return new Function<Cell<R, C, V1>, Cell<R, C, V2>>() {
478        @Override
479        public Cell<R, C, V2> apply(Cell<R, C, V1> cell) {
480          return immutableCell(
481              cell.getRowKey(), cell.getColumnKey(), function.apply(cell.getValue()));
482        }
483      };
484    }
485
486    @Override
487    Iterator<Cell<R, C, V2>> cellIterator() {
488      return Iterators.transform(fromTable.cellSet().iterator(), cellFunction());
489    }
490
491    @Override
492    public Set<R> rowKeySet() {
493      return fromTable.rowKeySet();
494    }
495
496    @Override
497    public Set<C> columnKeySet() {
498      return fromTable.columnKeySet();
499    }
500
501    @Override
502    Collection<V2> createValues() {
503      return Collections2.transform(fromTable.values(), function);
504    }
505
506    @Override
507    public Map<R, Map<C, V2>> rowMap() {
508      Function<Map<C, V1>, Map<C, V2>> rowFunction =
509          new Function<Map<C, V1>, Map<C, V2>>() {
510            @Override
511            public Map<C, V2> apply(Map<C, V1> row) {
512              return Maps.transformValues(row, function);
513            }
514          };
515      return Maps.transformValues(fromTable.rowMap(), rowFunction);
516    }
517
518    @Override
519    public Map<C, Map<R, V2>> columnMap() {
520      Function<Map<R, V1>, Map<R, V2>> columnFunction =
521          new Function<Map<R, V1>, Map<R, V2>>() {
522            @Override
523            public Map<R, V2> apply(Map<R, V1> column) {
524              return Maps.transformValues(column, function);
525            }
526          };
527      return Maps.transformValues(fromTable.columnMap(), columnFunction);
528    }
529  }
530
531  /**
532   * Returns an unmodifiable view of the specified table. This method allows modules to provide
533   * users with "read-only" access to internal tables. Query operations on the returned table "read
534   * through" to the specified table, and attempts to modify the returned table, whether direct or
535   * via its collection views, result in an {@code UnsupportedOperationException}.
536   *
537   * <p>The returned table will be serializable if the specified table is serializable.
538   *
539   * <p>Consider using an {@link ImmutableTable}, which is guaranteed never to change.
540   *
541   * @since 11.0
542   */
543  public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
544      Table<R, C, V> unmodifiableTable(Table<? extends R, ? extends C, ? extends V> table) {
545    return new UnmodifiableTable<>(table);
546  }
547
548  private static class UnmodifiableTable<
549          R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
550      extends ForwardingTable<R, C, V> implements Serializable {
551    final Table<? extends R, ? extends C, ? extends V> delegate;
552
553    UnmodifiableTable(Table<? extends R, ? extends C, ? extends V> delegate) {
554      this.delegate = checkNotNull(delegate);
555    }
556
557    @SuppressWarnings("unchecked") // safe, covariant cast
558    @Override
559    protected Table<R, C, V> delegate() {
560      return (Table<R, C, V>) delegate;
561    }
562
563    @Override
564    public Set<Cell<R, C, V>> cellSet() {
565      return Collections.unmodifiableSet(super.cellSet());
566    }
567
568    @Override
569    public void clear() {
570      throw new UnsupportedOperationException();
571    }
572
573    @Override
574    public Map<R, V> column(@ParametricNullness C columnKey) {
575      return Collections.unmodifiableMap(super.column(columnKey));
576    }
577
578    @Override
579    public Set<C> columnKeySet() {
580      return Collections.unmodifiableSet(super.columnKeySet());
581    }
582
583    @Override
584    public Map<C, Map<R, V>> columnMap() {
585      Function<Map<R, V>, Map<R, V>> wrapper = unmodifiableWrapper();
586      return Collections.unmodifiableMap(Maps.transformValues(super.columnMap(), wrapper));
587    }
588
589    @Override
590    public @Nullable V put(
591        @ParametricNullness R rowKey,
592        @ParametricNullness C columnKey,
593        @ParametricNullness V value) {
594      throw new UnsupportedOperationException();
595    }
596
597    @Override
598    public void putAll(Table<? extends R, ? extends C, ? extends V> table) {
599      throw new UnsupportedOperationException();
600    }
601
602    @Override
603    public @Nullable V remove(@Nullable Object rowKey, @Nullable Object columnKey) {
604      throw new UnsupportedOperationException();
605    }
606
607    @Override
608    public Map<C, V> row(@ParametricNullness R rowKey) {
609      return Collections.unmodifiableMap(super.row(rowKey));
610    }
611
612    @Override
613    public Set<R> rowKeySet() {
614      return Collections.unmodifiableSet(super.rowKeySet());
615    }
616
617    @Override
618    public Map<R, Map<C, V>> rowMap() {
619      Function<Map<C, V>, Map<C, V>> wrapper = unmodifiableWrapper();
620      return Collections.unmodifiableMap(Maps.transformValues(super.rowMap(), wrapper));
621    }
622
623    @Override
624    public Collection<V> values() {
625      return Collections.unmodifiableCollection(super.values());
626    }
627
628    private static final long serialVersionUID = 0;
629  }
630
631  /**
632   * Returns an unmodifiable view of the specified row-sorted table. This method allows modules to
633   * provide users with "read-only" access to internal tables. Query operations on the returned
634   * table "read through" to the specified table, and attempts to modify the returned table, whether
635   * direct or via its collection views, result in an {@code UnsupportedOperationException}.
636   *
637   * <p>The returned table will be serializable if the specified table is serializable.
638   *
639   * @param table the row-sorted table for which an unmodifiable view is to be returned
640   * @return an unmodifiable view of the specified table
641   * @since 11.0
642   */
643  public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
644      RowSortedTable<R, C, V> unmodifiableRowSortedTable(
645          RowSortedTable<R, ? extends C, ? extends V> table) {
646    /*
647     * It's not ? extends R, because it's technically not covariant in R. Specifically,
648     * table.rowMap().comparator() could return a comparator that only works for the ? extends R.
649     * Collections.unmodifiableSortedMap makes the same distinction.
650     */
651    return new UnmodifiableRowSortedMap<>(table);
652  }
653
654  private static final class UnmodifiableRowSortedMap<
655          R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
656      extends UnmodifiableTable<R, C, V> implements RowSortedTable<R, C, V> {
657
658    public UnmodifiableRowSortedMap(RowSortedTable<R, ? extends C, ? extends V> delegate) {
659      super(delegate);
660    }
661
662    @Override
663    protected RowSortedTable<R, C, V> delegate() {
664      return (RowSortedTable<R, C, V>) super.delegate();
665    }
666
667    @Override
668    public SortedMap<R, Map<C, V>> rowMap() {
669      Function<Map<C, V>, Map<C, V>> wrapper = unmodifiableWrapper();
670      return Collections.unmodifiableSortedMap(Maps.transformValues(delegate().rowMap(), wrapper));
671    }
672
673    @Override
674    public SortedSet<R> rowKeySet() {
675      return Collections.unmodifiableSortedSet(delegate().rowKeySet());
676    }
677
678    private static final long serialVersionUID = 0;
679  }
680
681  @SuppressWarnings("unchecked")
682  private static <K extends @Nullable Object, V extends @Nullable Object>
683      Function<Map<K, V>, Map<K, V>> unmodifiableWrapper() {
684    return (Function) UNMODIFIABLE_WRAPPER;
685  }
686
687  private static final Function<? extends Map<?, ?>, ? extends Map<?, ?>> UNMODIFIABLE_WRAPPER =
688      new Function<Map<Object, Object>, Map<Object, Object>>() {
689        @Override
690        public Map<Object, Object> apply(Map<Object, Object> input) {
691          return Collections.unmodifiableMap(input);
692        }
693      };
694
695  /**
696   * Returns a synchronized (thread-safe) table backed by the specified table. In order to guarantee
697   * serial access, it is critical that <b>all</b> access to the backing table is accomplished
698   * through the returned table.
699   *
700   * <p>It is imperative that the user manually synchronize on the returned table when accessing any
701   * of its collection views:
702   *
703   * <pre>{@code
704   * Table<R, C, V> table = Tables.synchronizedTable(HashBasedTable.<R, C, V>create());
705   * ...
706   * Map<C, V> row = table.row(rowKey);  // Needn't be in synchronized block
707   * ...
708   * synchronized (table) {  // Synchronizing on table, not row!
709   *   Iterator<Entry<C, V>> i = row.entrySet().iterator(); // Must be in synchronized block
710   *   while (i.hasNext()) {
711   *     foo(i.next());
712   *   }
713   * }
714   * }</pre>
715   *
716   * <p>Failure to follow this advice may result in non-deterministic behavior.
717   *
718   * <p>The returned table will be serializable if the specified table is serializable.
719   *
720   * @param table the table to be wrapped in a synchronized view
721   * @return a synchronized view of the specified table
722   * @since 22.0
723   */
724  @J2ktIncompatible // Synchronized
725  public static <R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
726      Table<R, C, V> synchronizedTable(Table<R, C, V> table) {
727    return Synchronized.table(table, null);
728  }
729
730  static boolean equalsImpl(Table<?, ?, ?> table, @Nullable Object obj) {
731    if (obj == table) {
732      return true;
733    } else if (obj instanceof Table) {
734      Table<?, ?, ?> that = (Table<?, ?, ?>) obj;
735      return table.cellSet().equals(that.cellSet());
736    } else {
737      return false;
738    }
739  }
740}