1 /* 2 * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible; 20 import com.google.common.base.Objects; 21 import com.google.errorprone.annotations.CanIgnoreReturnValue; 22 import com.google.errorprone.annotations.CompatibleWith; 23 import com.google.errorprone.annotations.DoNotMock; 24 import java.util.Collection; 25 import java.util.Map; 26 import java.util.Set; 27 import javax.annotation.CheckForNull; 28 import org.checkerframework.checker.nullness.qual.Nullable; 29 30 /** 31 * A collection that associates an ordered pair of keys, called a row key and a column key, with a 32 * single value. A table may be sparse, with only a small fraction of row key / column key pairs 33 * possessing a corresponding value. 34 * 35 * <p>The mappings corresponding to a given row key may be viewed as a {@link Map} whose keys are 36 * the columns. The reverse is also available, associating a column with a row key / value map. Note 37 * that, in some implementations, data access by column key may have fewer supported operations or 38 * worse performance than data access by row key. 39 * 40 * <p>The methods returning collections or maps always return views of the underlying table. 41 * Updating the table can change the contents of those collections, and updating the collections 42 * will change the table. 43 * 44 * <p>All methods that modify the table are optional, and the views returned by the table may or may 45 * not be modifiable. When modification isn't supported, those methods will throw an {@link 46 * UnsupportedOperationException}. 47 * 48 * <h3>Implementations</h3> 49 * 50 * <ul> 51 * <li>{@link ImmutableTable} 52 * <li>{@link HashBasedTable} 53 * <li>{@link TreeBasedTable} 54 * <li>{@link ArrayTable} 55 * <li>{@link Tables#newCustomTable Tables.newCustomTable} 56 * </ul> 57 * 58 * <p>See the Guava User Guide article on <a href= 59 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table">{@code Table}</a>. 60 * 61 * @author Jared Levy 62 * @param <R> the type of the table row keys 63 * @param <C> the type of the table column keys 64 * @param <V> the type of the mapped values 65 * @since 7.0 66 */ 67 @DoNotMock("Use ImmutableTable, HashBasedTable, or another implementation") 68 @GwtCompatible 69 @ElementTypesAreNonnullByDefault 70 public interface Table< 71 R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object> { 72 // TODO(jlevy): Consider adding methods similar to ConcurrentMap methods. 73 74 // Accessors 75 76 /** 77 * Returns {@code true} if the table contains a mapping with the specified row and column keys. 78 * 79 * @param rowKey key of row to search for 80 * @param columnKey key of column to search for 81 */ contains( @ompatibleWith"R") @heckForNull Object rowKey, @CompatibleWith("C") @CheckForNull Object columnKey)82 boolean contains( 83 @CompatibleWith("R") @CheckForNull Object rowKey, 84 @CompatibleWith("C") @CheckForNull Object columnKey); 85 86 /** 87 * Returns {@code true} if the table contains a mapping with the specified row key. 88 * 89 * @param rowKey key of row to search for 90 */ containsRow(@ompatibleWith"R") @heckForNull Object rowKey)91 boolean containsRow(@CompatibleWith("R") @CheckForNull Object rowKey); 92 93 /** 94 * Returns {@code true} if the table contains a mapping with the specified column. 95 * 96 * @param columnKey key of column to search for 97 */ containsColumn(@ompatibleWith"C") @heckForNull Object columnKey)98 boolean containsColumn(@CompatibleWith("C") @CheckForNull Object columnKey); 99 100 /** 101 * Returns {@code true} if the table contains a mapping with the specified value. 102 * 103 * @param value value to search for 104 */ containsValue(@ompatibleWith"V") @heckForNull Object value)105 boolean containsValue(@CompatibleWith("V") @CheckForNull Object value); 106 107 /** 108 * Returns the value corresponding to the given row and column keys, or {@code null} if no such 109 * mapping exists. 110 * 111 * @param rowKey key of row to search for 112 * @param columnKey key of column to search for 113 */ 114 @CheckForNull get( @ompatibleWith"R") @heckForNull Object rowKey, @CompatibleWith("C") @CheckForNull Object columnKey)115 V get( 116 @CompatibleWith("R") @CheckForNull Object rowKey, 117 @CompatibleWith("C") @CheckForNull Object columnKey); 118 119 /** Returns {@code true} if the table contains no mappings. */ isEmpty()120 boolean isEmpty(); 121 122 /** Returns the number of row key / column key / value mappings in the table. */ size()123 int size(); 124 125 /** 126 * Compares the specified object with this table for equality. Two tables are equal when their 127 * cell views, as returned by {@link #cellSet}, are equal. 128 */ 129 @Override equals(@heckForNull Object obj)130 boolean equals(@CheckForNull Object obj); 131 132 /** 133 * Returns the hash code for this table. The hash code of a table is defined as the hash code of 134 * its cell view, as returned by {@link #cellSet}. 135 */ 136 @Override hashCode()137 int hashCode(); 138 139 // Mutators 140 141 /** Removes all mappings from the table. */ clear()142 void clear(); 143 144 /** 145 * Associates the specified value with the specified keys. If the table already contained a 146 * mapping for those keys, the old value is replaced with the specified value. 147 * 148 * @param rowKey row key that the value should be associated with 149 * @param columnKey column key that the value should be associated with 150 * @param value value to be associated with the specified keys 151 * @return the value previously associated with the keys, or {@code null} if no mapping existed 152 * for the keys 153 */ 154 @CanIgnoreReturnValue 155 @CheckForNull put(@arametricNullness R rowKey, @ParametricNullness C columnKey, @ParametricNullness V value)156 V put(@ParametricNullness R rowKey, @ParametricNullness C columnKey, @ParametricNullness V value); 157 158 /** 159 * Copies all mappings from the specified table to this table. The effect is equivalent to calling 160 * {@link #put} with each row key / column key / value mapping in {@code table}. 161 * 162 * @param table the table to add to this table 163 */ putAll(Table<? extends R, ? extends C, ? extends V> table)164 void putAll(Table<? extends R, ? extends C, ? extends V> table); 165 166 /** 167 * Removes the mapping, if any, associated with the given keys. 168 * 169 * @param rowKey row key of mapping to be removed 170 * @param columnKey column key of mapping to be removed 171 * @return the value previously associated with the keys, or {@code null} if no such value existed 172 */ 173 @CanIgnoreReturnValue 174 @CheckForNull remove( @ompatibleWith"R") @heckForNull Object rowKey, @CompatibleWith("C") @CheckForNull Object columnKey)175 V remove( 176 @CompatibleWith("R") @CheckForNull Object rowKey, 177 @CompatibleWith("C") @CheckForNull Object columnKey); 178 179 // Views 180 181 /** 182 * Returns a view of all mappings that have the given row key. For each row key / column key / 183 * value mapping in the table with that row key, the returned map associates the column key with 184 * the value. If no mappings in the table have the provided row key, an empty map is returned. 185 * 186 * <p>Changes to the returned map will update the underlying table, and vice versa. 187 * 188 * @param rowKey key of row to search for in the table 189 * @return the corresponding map from column keys to values 190 */ row(@arametricNullness R rowKey)191 Map<C, V> row(@ParametricNullness R rowKey); 192 193 /** 194 * Returns a view of all mappings that have the given column key. For each row key / column key / 195 * value mapping in the table with that column key, the returned map associates the row key with 196 * the value. If no mappings in the table have the provided column key, an empty map is returned. 197 * 198 * <p>Changes to the returned map will update the underlying table, and vice versa. 199 * 200 * @param columnKey key of column to search for in the table 201 * @return the corresponding map from row keys to values 202 */ column(@arametricNullness C columnKey)203 Map<R, V> column(@ParametricNullness C columnKey); 204 205 /** 206 * Returns a set of all row key / column key / value triplets. Changes to the returned set will 207 * update the underlying table, and vice versa. The cell set does not support the {@code add} or 208 * {@code addAll} methods. 209 * 210 * @return set of table cells consisting of row key / column key / value triplets 211 */ cellSet()212 Set<Cell<R, C, V>> cellSet(); 213 214 /** 215 * Returns a set of row keys that have one or more values in the table. Changes to the set will 216 * update the underlying table, and vice versa. 217 * 218 * @return set of row keys 219 */ rowKeySet()220 Set<R> rowKeySet(); 221 222 /** 223 * Returns a set of column keys that have one or more values in the table. Changes to the set will 224 * update the underlying table, and vice versa. 225 * 226 * @return set of column keys 227 */ columnKeySet()228 Set<C> columnKeySet(); 229 230 /** 231 * Returns a collection of all values, which may contain duplicates. Changes to the returned 232 * collection will update the underlying table, and vice versa. 233 * 234 * @return collection of values 235 */ values()236 Collection<V> values(); 237 238 /** 239 * Returns a view that associates each row key with the corresponding map from column keys to 240 * values. Changes to the returned map will update this table. The returned map does not support 241 * {@code put()} or {@code putAll()}, or {@code setValue()} on its entries. 242 * 243 * <p>In contrast, the maps returned by {@code rowMap().get()} have the same behavior as those 244 * returned by {@link #row}. Those maps may support {@code setValue()}, {@code put()}, and {@code 245 * putAll()}. 246 * 247 * @return a map view from each row key to a secondary map from column keys to values 248 */ rowMap()249 Map<R, Map<C, V>> rowMap(); 250 251 /** 252 * Returns a view that associates each column key with the corresponding map from row keys to 253 * values. Changes to the returned map will update this table. The returned map does not support 254 * {@code put()} or {@code putAll()}, or {@code setValue()} on its entries. 255 * 256 * <p>In contrast, the maps returned by {@code columnMap().get()} have the same behavior as those 257 * returned by {@link #column}. Those maps may support {@code setValue()}, {@code put()}, and 258 * {@code putAll()}. 259 * 260 * @return a map view from each column key to a secondary map from row keys to values 261 */ columnMap()262 Map<C, Map<R, V>> columnMap(); 263 264 /** 265 * Row key / column key / value triplet corresponding to a mapping in a table. 266 * 267 * @since 7.0 268 */ 269 interface Cell< 270 R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object> { 271 /** Returns the row key of this cell. */ 272 @ParametricNullness getRowKey()273 R getRowKey(); 274 275 /** Returns the column key of this cell. */ 276 @ParametricNullness getColumnKey()277 C getColumnKey(); 278 279 /** Returns the value of this cell. */ 280 @ParametricNullness getValue()281 V getValue(); 282 283 /** 284 * Compares the specified object with this cell for equality. Two cells are equal when they have 285 * equal row keys, column keys, and values. 286 */ 287 @Override equals(@heckForNull Object obj)288 boolean equals(@CheckForNull Object obj); 289 290 /** 291 * Returns the hash code of this cell. 292 * 293 * <p>The hash code of a table cell is equal to {@link Objects#hashCode}{@code (e.getRowKey(), 294 * e.getColumnKey(), e.getValue())}. 295 */ 296 @Override hashCode()297 int hashCode(); 298 } 299 } 300