1 /* 2 * Copyright (C) 2017 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import java.io.Serializable; 20 import java.util.Collection; 21 import java.util.Map; 22 import java.util.Set; 23 import org.checkerframework.checker.nullness.qual.Nullable; 24 25 public class SynchronizedTableTest extends AbstractTableTest { 26 private static final class TestTable<R, C, V> implements Table<R, C, V>, Serializable { 27 final Table<R, C, V> delegate = HashBasedTable.create(); 28 public final Object mutex = new Integer(1); // something Serializable 29 30 @Override toString()31 public String toString() { 32 assertTrue(Thread.holdsLock(mutex)); 33 return delegate.toString(); 34 } 35 36 @Override equals(@ullable Object o)37 public boolean equals(@Nullable Object o) { 38 assertTrue(Thread.holdsLock(mutex)); 39 return delegate.equals(o); 40 } 41 42 @Override hashCode()43 public int hashCode() { 44 assertTrue(Thread.holdsLock(mutex)); 45 return delegate.hashCode(); 46 } 47 48 @Override size()49 public int size() { 50 assertTrue(Thread.holdsLock(mutex)); 51 return delegate.size(); 52 } 53 54 @Override isEmpty()55 public boolean isEmpty() { 56 assertTrue(Thread.holdsLock(mutex)); 57 return delegate.isEmpty(); 58 } 59 60 @Override containsValue(@ullable Object value)61 public boolean containsValue(@Nullable Object value) { 62 assertTrue(Thread.holdsLock(mutex)); 63 return delegate.containsValue(value); 64 } 65 66 @Override clear()67 public void clear() { 68 assertTrue(Thread.holdsLock(mutex)); 69 delegate.clear(); 70 } 71 72 @Override values()73 public Collection<V> values() { 74 assertTrue(Thread.holdsLock(mutex)); 75 /* TODO: verify that the Collection is also synchronized? */ 76 return delegate.values(); 77 } 78 79 @Override cellSet()80 public Set<Cell<R, C, V>> cellSet() { 81 assertTrue(Thread.holdsLock(mutex)); 82 return delegate.cellSet(); 83 } 84 85 @Override column(C columnKey)86 public Map<R, V> column(C columnKey) { 87 assertTrue(Thread.holdsLock(mutex)); 88 return delegate.column(columnKey); 89 } 90 91 @Override columnKeySet()92 public Set<C> columnKeySet() { 93 assertTrue(Thread.holdsLock(mutex)); 94 return delegate.columnKeySet(); 95 } 96 97 @Override columnMap()98 public Map<C, Map<R, V>> columnMap() { 99 assertTrue(Thread.holdsLock(mutex)); 100 return delegate.columnMap(); 101 } 102 103 @Override contains(Object rowKey, Object columnKey)104 public boolean contains(Object rowKey, Object columnKey) { 105 assertTrue(Thread.holdsLock(mutex)); 106 return delegate.contains(rowKey, columnKey); 107 } 108 109 @Override containsColumn(Object columnKey)110 public boolean containsColumn(Object columnKey) { 111 assertTrue(Thread.holdsLock(mutex)); 112 return delegate.containsColumn(columnKey); 113 } 114 115 @Override containsRow(Object rowKey)116 public boolean containsRow(Object rowKey) { 117 assertTrue(Thread.holdsLock(mutex)); 118 return delegate.containsRow(rowKey); 119 } 120 121 @Override get(Object rowKey, Object columnKey)122 public V get(Object rowKey, Object columnKey) { 123 assertTrue(Thread.holdsLock(mutex)); 124 return delegate.get(rowKey, columnKey); 125 } 126 127 @Override put(R rowKey, C columnKey, V value)128 public V put(R rowKey, C columnKey, V value) { 129 assertTrue(Thread.holdsLock(mutex)); 130 return delegate.put(rowKey, columnKey, value); 131 } 132 133 @Override putAll(Table<? extends R, ? extends C, ? extends V> table)134 public void putAll(Table<? extends R, ? extends C, ? extends V> table) { 135 assertTrue(Thread.holdsLock(mutex)); 136 delegate.putAll(table); 137 } 138 139 @Override remove(Object rowKey, Object columnKey)140 public V remove(Object rowKey, Object columnKey) { 141 assertTrue(Thread.holdsLock(mutex)); 142 return delegate.remove(rowKey, columnKey); 143 } 144 145 @Override row(R rowKey)146 public Map<C, V> row(R rowKey) { 147 assertTrue(Thread.holdsLock(mutex)); 148 return delegate.row(rowKey); 149 } 150 151 @Override rowKeySet()152 public Set<R> rowKeySet() { 153 assertTrue(Thread.holdsLock(mutex)); 154 return delegate.rowKeySet(); 155 } 156 157 @Override rowMap()158 public Map<R, Map<C, V>> rowMap() { 159 assertTrue(Thread.holdsLock(mutex)); 160 return delegate.rowMap(); 161 } 162 163 private static final long serialVersionUID = 0; 164 } 165 166 @Override create(Object... data)167 protected Table<String, Integer, Character> create(Object... data) { 168 TestTable<String, Integer, Character> table = new TestTable<>(); 169 Table<String, Integer, Character> synced = Synchronized.table(table, table.mutex); 170 populate(synced, data); 171 return synced; 172 } 173 } 174