/* * Copyright (C) 2017 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.collect; import java.io.Serializable; import java.util.Collection; import java.util.Map; import java.util.Set; import org.checkerframework.checker.nullness.qual.Nullable; public class SynchronizedTableTest extends AbstractTableTest { private static final class TestTable implements Table, Serializable { final Table delegate = HashBasedTable.create(); public final Object mutex = new Integer(1); // something Serializable @Override public String toString() { assertTrue(Thread.holdsLock(mutex)); return delegate.toString(); } @Override public boolean equals(@Nullable Object o) { assertTrue(Thread.holdsLock(mutex)); return delegate.equals(o); } @Override public int hashCode() { assertTrue(Thread.holdsLock(mutex)); return delegate.hashCode(); } @Override public int size() { assertTrue(Thread.holdsLock(mutex)); return delegate.size(); } @Override public boolean isEmpty() { assertTrue(Thread.holdsLock(mutex)); return delegate.isEmpty(); } @Override public boolean containsValue(@Nullable Object value) { assertTrue(Thread.holdsLock(mutex)); return delegate.containsValue(value); } @Override public void clear() { assertTrue(Thread.holdsLock(mutex)); delegate.clear(); } @Override public Collection values() { assertTrue(Thread.holdsLock(mutex)); /* TODO: verify that the Collection is also synchronized? */ return delegate.values(); } @Override public Set> cellSet() { assertTrue(Thread.holdsLock(mutex)); return delegate.cellSet(); } @Override public Map column(C columnKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.column(columnKey); } @Override public Set columnKeySet() { assertTrue(Thread.holdsLock(mutex)); return delegate.columnKeySet(); } @Override public Map> columnMap() { assertTrue(Thread.holdsLock(mutex)); return delegate.columnMap(); } @Override public boolean contains(Object rowKey, Object columnKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.contains(rowKey, columnKey); } @Override public boolean containsColumn(Object columnKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.containsColumn(columnKey); } @Override public boolean containsRow(Object rowKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.containsRow(rowKey); } @Override public V get(Object rowKey, Object columnKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.get(rowKey, columnKey); } @Override public V put(R rowKey, C columnKey, V value) { assertTrue(Thread.holdsLock(mutex)); return delegate.put(rowKey, columnKey, value); } @Override public void putAll(Table table) { assertTrue(Thread.holdsLock(mutex)); delegate.putAll(table); } @Override public V remove(Object rowKey, Object columnKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.remove(rowKey, columnKey); } @Override public Map row(R rowKey) { assertTrue(Thread.holdsLock(mutex)); return delegate.row(rowKey); } @Override public Set rowKeySet() { assertTrue(Thread.holdsLock(mutex)); return delegate.rowKeySet(); } @Override public Map> rowMap() { assertTrue(Thread.holdsLock(mutex)); return delegate.rowMap(); } private static final long serialVersionUID = 0; } @Override protected Table create(Object... data) { TestTable table = new TestTable<>(); Table synced = Synchronized.table(table, table.mutex); populate(synced, data); return synced; } }