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