1/* 2 * Copyright (C) 2009 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 17package com.google.common.collect; 18 19import static com.google.common.base.Preconditions.checkNotNull; 20 21import com.google.common.annotations.GwtCompatible; 22 23import java.util.Map; 24 25/** 26 * An implementation of {@link ImmutableTable} that holds a single cell. 27 * 28 * @author Gregory Kick 29 */ 30@GwtCompatible 31class SingletonImmutableTable<R, C, V> extends ImmutableTable<R, C, V> { 32 final R singleRowKey; 33 final C singleColumnKey; 34 final V singleValue; 35 36 SingletonImmutableTable(R rowKey, C columnKey, V value) { 37 this.singleRowKey = checkNotNull(rowKey); 38 this.singleColumnKey = checkNotNull(columnKey); 39 this.singleValue = checkNotNull(value); 40 } 41 42 SingletonImmutableTable(Cell<R, C, V> cell) { 43 this(cell.getRowKey(), cell.getColumnKey(), cell.getValue()); 44 } 45 46 @Override public ImmutableMap<R, V> column(C columnKey) { 47 checkNotNull(columnKey); 48 return containsColumn(columnKey) 49 ? ImmutableMap.of(singleRowKey, singleValue) 50 : ImmutableMap.<R, V>of(); 51 } 52 53 @Override public ImmutableMap<C, Map<R, V>> columnMap() { 54 return ImmutableMap.of(singleColumnKey, 55 (Map<R, V>) ImmutableMap.of(singleRowKey, singleValue)); 56 } 57 58 @Override public ImmutableMap<R, Map<C, V>> rowMap() { 59 return ImmutableMap.of(singleRowKey, 60 (Map<C, V>) ImmutableMap.of(singleColumnKey, singleValue)); 61 } 62 63 @Override public int size() { 64 return 1; 65 } 66 67 @Override 68 ImmutableSet<Cell<R, C, V>> createCellSet() { 69 return ImmutableSet.of( 70 cellOf(singleRowKey, singleColumnKey, singleValue)); 71 } 72 73 @Override ImmutableCollection<V> createValues() { 74 return ImmutableSet.of(singleValue); 75 } 76} 77