Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2008 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 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 
     21 import java.util.Map;
     22 
     23 import javax.annotation.Nullable;
     24 
     25 /**
     26  * An empty immutable map.
     27  *
     28  * @author Jesse Wilson
     29  * @author Kevin Bourrillion
     30  */
     31 @GwtCompatible(serializable = true)
     32 final class EmptyImmutableMap extends ImmutableMap<Object, Object> {
     33   static final EmptyImmutableMap INSTANCE = new EmptyImmutableMap();
     34 
     35   private EmptyImmutableMap() {}
     36 
     37   @Override public Object get(Object key) {
     38     return null;
     39   }
     40 
     41   public int size() {
     42     return 0;
     43   }
     44 
     45   @Override public boolean isEmpty() {
     46     return true;
     47   }
     48 
     49   @Override public boolean containsKey(Object key) {
     50     return false;
     51   }
     52 
     53   @Override public boolean containsValue(Object value) {
     54     return false;
     55   }
     56 
     57   @Override public ImmutableSet<Entry<Object, Object>> entrySet() {
     58     return ImmutableSet.of();
     59   }
     60 
     61   @Override public ImmutableSet<Object> keySet() {
     62     return ImmutableSet.of();
     63   }
     64 
     65   @Override public ImmutableCollection<Object> values() {
     66     return ImmutableCollection.EMPTY_IMMUTABLE_COLLECTION;
     67   }
     68 
     69   @Override public boolean equals(@Nullable Object object) {
     70     if (object instanceof Map) {
     71       Map<?, ?> that = (Map<?, ?>) object;
     72       return that.isEmpty();
     73     }
     74     return false;
     75   }
     76 
     77   @Override public int hashCode() {
     78     return 0;
     79   }
     80 
     81   @Override public String toString() {
     82     return "{}";
     83   }
     84 
     85   Object readResolve() {
     86     return INSTANCE; // preserve singleton property
     87   }
     88 
     89   private static final long serialVersionUID = 0;
     90 }
     91