1/*
2 * Copyright (C) 2009 Google Inc.
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
25import javax.annotation.Nullable;
26import javax.annotation.concurrent.Immutable;
27
28/**
29 * An empty implementation of {@link ImmutableTable}.
30 *
31 * @author gak@google.com (Gregory Kick)
32 */
33@GwtCompatible
34@Immutable
35final class EmptyImmutableTable extends ImmutableTable<Object, Object, Object> {
36  static final EmptyImmutableTable INSTANCE = new EmptyImmutableTable();
37
38  private EmptyImmutableTable() {}
39
40  @Override public int size() {
41    return 0;
42  }
43
44  @Override public Object get(@Nullable Object rowKey,
45      @Nullable Object columnKey) {
46    return null;
47  }
48
49  @Override public boolean isEmpty() {
50    return true;
51  }
52
53  @Override public boolean equals(@Nullable Object obj) {
54    if (obj == this) {
55      return true;
56    } else if (obj instanceof Table<?, ?, ?>) {
57      Table<?, ?, ?> that = (Table<?, ?, ?>) obj;
58      return that.isEmpty();
59    } else {
60      return false;
61    }
62  }
63
64  @Override public int hashCode() {
65    return 0;
66  }
67
68  @Override public ImmutableSet<Cell<Object, Object, Object>> cellSet() {
69    return ImmutableSet.of();
70  }
71
72  @Override public ImmutableMap<Object, Object> column(Object columnKey) {
73    checkNotNull(columnKey);
74    return ImmutableMap.of();
75  }
76
77  @Override public ImmutableSet<Object> columnKeySet() {
78    return ImmutableSet.of();
79  }
80
81  @Override public ImmutableMap<Object, Map<Object, Object>> columnMap() {
82    return ImmutableMap.of();
83  }
84
85  @Override public boolean contains(@Nullable Object rowKey,
86      @Nullable Object columnKey) {
87    return false;
88  }
89
90  @Override public boolean containsColumn(@Nullable Object columnKey) {
91    return false;
92  }
93
94  @Override public boolean containsRow(@Nullable Object rowKey) {
95    return false;
96  }
97
98  @Override public boolean containsValue(@Nullable Object value) {
99    return false;
100  }
101
102  @Override public ImmutableMap<Object, Object> row(Object rowKey) {
103    checkNotNull(rowKey);
104    return ImmutableMap.of();
105  }
106
107  @Override public ImmutableSet<Object> rowKeySet() {
108    return ImmutableSet.of();
109  }
110
111  @Override public ImmutableMap<Object, Map<Object, Object>> rowMap() {
112    return ImmutableMap.of();
113  }
114
115  @Override public String toString() {
116    return "{}";
117  }
118
119  @Override public ImmutableCollection<Object> values() {
120    return ImmutableSet.of();
121  }
122
123  Object readResolve() {
124    return INSTANCE; // preserve singleton property
125  }
126
127  private static final long serialVersionUID = 0;
128}
129