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 com.google.common.annotations.GwtCompatible; 020import com.google.common.base.Objects; 021import com.google.errorprone.annotations.CanIgnoreReturnValue; 022import com.google.errorprone.annotations.CompatibleWith; 023import com.google.errorprone.annotations.DoNotMock; 024import java.util.Collection; 025import java.util.Map; 026import java.util.Set; 027import org.checkerframework.checker.nullness.compatqual.NullableDecl; 028 029/** 030 * A collection that associates an ordered pair of keys, called a row key and a column key, with a 031 * single value. A table may be sparse, with only a small fraction of row key / column key pairs 032 * possessing a corresponding value. 033 * 034 * <p>The mappings corresponding to a given row key may be viewed as a {@link Map} whose keys are 035 * the columns. The reverse is also available, associating a column with a row key / value map. Note 036 * that, in some implementations, data access by column key may have fewer supported operations or 037 * worse performance than data access by row key. 038 * 039 * <p>The methods returning collections or maps always return views of the underlying table. 040 * Updating the table can change the contents of those collections, and updating the collections 041 * will change the table. 042 * 043 * <p>All methods that modify the table are optional, and the views returned by the table may or may 044 * not be modifiable. When modification isn't supported, those methods will throw an {@link 045 * UnsupportedOperationException}. 046 * 047 * <p>See the Guava User Guide article on <a href= 048 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#table"> {@code Table}</a>. 049 * 050 * @author Jared Levy 051 * @param <R> the type of the table row keys 052 * @param <C> the type of the table column keys 053 * @param <V> the type of the mapped values 054 * @since 7.0 055 */ 056@DoNotMock("Use ImmutableTable, HashBasedTable, or another implementation") 057@GwtCompatible 058public interface Table<R, C, V> { 059 // TODO(jlevy): Consider adding methods similar to ConcurrentMap methods. 060 061 // Accessors 062 063 /** 064 * Returns {@code true} if the table contains a mapping with the specified row and column keys. 065 * 066 * @param rowKey key of row to search for 067 * @param columnKey key of column to search for 068 */ 069 boolean contains( 070 @NullableDecl @CompatibleWith("R") Object rowKey, 071 @NullableDecl @CompatibleWith("C") Object columnKey); 072 073 /** 074 * Returns {@code true} if the table contains a mapping with the specified row key. 075 * 076 * @param rowKey key of row to search for 077 */ 078 boolean containsRow(@NullableDecl @CompatibleWith("R") Object rowKey); 079 080 /** 081 * Returns {@code true} if the table contains a mapping with the specified column. 082 * 083 * @param columnKey key of column to search for 084 */ 085 boolean containsColumn(@NullableDecl @CompatibleWith("C") Object columnKey); 086 087 /** 088 * Returns {@code true} if the table contains a mapping with the specified value. 089 * 090 * @param value value to search for 091 */ 092 boolean containsValue(@NullableDecl @CompatibleWith("V") Object value); 093 094 /** 095 * Returns the value corresponding to the given row and column keys, or {@code null} if no such 096 * mapping exists. 097 * 098 * @param rowKey key of row to search for 099 * @param columnKey key of column to search for 100 */ 101 V get( 102 @NullableDecl @CompatibleWith("R") Object rowKey, 103 @NullableDecl @CompatibleWith("C") Object columnKey); 104 105 /** Returns {@code true} if the table contains no mappings. */ 106 boolean isEmpty(); 107 108 /** Returns the number of row key / column key / value mappings in the table. */ 109 int size(); 110 111 /** 112 * Compares the specified object with this table for equality. Two tables are equal when their 113 * cell views, as returned by {@link #cellSet}, are equal. 114 */ 115 @Override 116 boolean equals(@NullableDecl Object obj); 117 118 /** 119 * Returns the hash code for this table. The hash code of a table is defined as the hash code of 120 * its cell view, as returned by {@link #cellSet}. 121 */ 122 @Override 123 int hashCode(); 124 125 // Mutators 126 127 /** Removes all mappings from the table. */ 128 void clear(); 129 130 /** 131 * Associates the specified value with the specified keys. If the table already contained a 132 * mapping for those keys, the old value is replaced with the specified value. 133 * 134 * @param rowKey row key that the value should be associated with 135 * @param columnKey column key that the value should be associated with 136 * @param value value to be associated with the specified keys 137 * @return the value previously associated with the keys, or {@code null} if no mapping existed 138 * for the keys 139 */ 140 @CanIgnoreReturnValue 141 @NullableDecl 142 V put(R rowKey, C columnKey, V value); 143 144 /** 145 * Copies all mappings from the specified table to this table. The effect is equivalent to calling 146 * {@link #put} with each row key / column key / value mapping in {@code table}. 147 * 148 * @param table the table to add to this table 149 */ 150 void putAll(Table<? extends R, ? extends C, ? extends V> table); 151 152 /** 153 * Removes the mapping, if any, associated with the given keys. 154 * 155 * @param rowKey row key of mapping to be removed 156 * @param columnKey column key of mapping to be removed 157 * @return the value previously associated with the keys, or {@code null} if no such value existed 158 */ 159 @CanIgnoreReturnValue 160 @NullableDecl 161 V remove( 162 @NullableDecl @CompatibleWith("R") Object rowKey, 163 @NullableDecl @CompatibleWith("C") Object columnKey); 164 165 // Views 166 167 /** 168 * Returns a view of all mappings that have the given row key. For each row key / column key / 169 * value mapping in the table with that row key, the returned map associates the column key with 170 * the value. If no mappings in the table have the provided row key, an empty map is returned. 171 * 172 * <p>Changes to the returned map will update the underlying table, and vice versa. 173 * 174 * @param rowKey key of row to search for in the table 175 * @return the corresponding map from column keys to values 176 */ 177 Map<C, V> row(R rowKey); 178 179 /** 180 * Returns a view of all mappings that have the given column key. For each row key / column key / 181 * value mapping in the table with that column key, the returned map associates the row key with 182 * the value. If no mappings in the table have the provided column key, an empty map is returned. 183 * 184 * <p>Changes to the returned map will update the underlying table, and vice versa. 185 * 186 * @param columnKey key of column to search for in the table 187 * @return the corresponding map from row keys to values 188 */ 189 Map<R, V> column(C columnKey); 190 191 /** 192 * Returns a set of all row key / column key / value triplets. Changes to the returned set will 193 * update the underlying table, and vice versa. The cell set does not support the {@code add} or 194 * {@code addAll} methods. 195 * 196 * @return set of table cells consisting of row key / column key / value triplets 197 */ 198 Set<Cell<R, C, V>> cellSet(); 199 200 /** 201 * Returns a set of row keys that have one or more values in the table. Changes to the set will 202 * update the underlying table, and vice versa. 203 * 204 * @return set of row keys 205 */ 206 Set<R> rowKeySet(); 207 208 /** 209 * Returns a set of column keys that have one or more values in the table. Changes to the set will 210 * update the underlying table, and vice versa. 211 * 212 * @return set of column keys 213 */ 214 Set<C> columnKeySet(); 215 216 /** 217 * Returns a collection of all values, which may contain duplicates. Changes to the returned 218 * collection will update the underlying table, and vice versa. 219 * 220 * @return collection of values 221 */ 222 Collection<V> values(); 223 224 /** 225 * Returns a view that associates each row key with the corresponding map from column keys to 226 * values. Changes to the returned map will update this table. The returned map does not support 227 * {@code put()} or {@code putAll()}, or {@code setValue()} on its entries. 228 * 229 * <p>In contrast, the maps returned by {@code rowMap().get()} have the same behavior as those 230 * returned by {@link #row}. Those maps may support {@code setValue()}, {@code put()}, and {@code 231 * putAll()}. 232 * 233 * @return a map view from each row key to a secondary map from column keys to values 234 */ 235 Map<R, Map<C, V>> rowMap(); 236 237 /** 238 * Returns a view that associates each column key with the corresponding map from row keys to 239 * values. Changes to the returned map will update this table. The returned map does not support 240 * {@code put()} or {@code putAll()}, or {@code setValue()} on its entries. 241 * 242 * <p>In contrast, the maps returned by {@code columnMap().get()} have the same behavior as those 243 * returned by {@link #column}. Those maps may support {@code setValue()}, {@code put()}, and 244 * {@code putAll()}. 245 * 246 * @return a map view from each column key to a secondary map from row keys to values 247 */ 248 Map<C, Map<R, V>> columnMap(); 249 250 /** 251 * Row key / column key / value triplet corresponding to a mapping in a table. 252 * 253 * @since 7.0 254 */ 255 interface Cell<R, C, V> { 256 /** Returns the row key of this cell. */ 257 @NullableDecl 258 R getRowKey(); 259 260 /** Returns the column key of this cell. */ 261 @NullableDecl 262 C getColumnKey(); 263 264 /** Returns the value of this cell. */ 265 @NullableDecl 266 V getValue(); 267 268 /** 269 * Compares the specified object with this cell for equality. Two cells are equal when they have 270 * equal row keys, column keys, and values. 271 */ 272 @Override 273 boolean equals(@NullableDecl Object obj); 274 275 /** 276 * Returns the hash code of this cell. 277 * 278 * <p>The hash code of a table cell is equal to {@link Objects#hashCode}{@code (e.getRowKey(), 279 * e.getColumnKey(), e.getValue())}. 280 */ 281 @Override 282 int hashCode(); 283 } 284}