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