Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible;
     20 import com.google.common.annotations.GwtIncompatible;
     21 
     22 import java.io.Serializable;
     23 import java.util.Map.Entry;
     24 
     25 import javax.annotation.Nullable;
     26 
     27 /**
     28  * {@code keySet()} implementation for {@link ImmutableMap}.
     29  *
     30  * @author Jesse Wilson
     31  * @author Kevin Bourrillion
     32  */
     33 @GwtCompatible(emulated = true)
     34 final class ImmutableMapKeySet<K, V> extends ImmutableSet<K> {
     35   private final ImmutableMap<K, V> map;
     36 
     37   ImmutableMapKeySet(ImmutableMap<K, V> map) {
     38     this.map = map;
     39   }
     40 
     41   @Override
     42   public int size() {
     43     return map.size();
     44   }
     45 
     46   @Override
     47   public UnmodifiableIterator<K> iterator() {
     48     return asList().iterator();
     49   }
     50 
     51   @Override
     52   public boolean contains(@Nullable Object object) {
     53     return map.containsKey(object);
     54   }
     55 
     56   @Override
     57   ImmutableList<K> createAsList() {
     58     final ImmutableList<Entry<K, V>> entryList = map.entrySet().asList();
     59     return new ImmutableAsList<K>() {
     60 
     61       @Override
     62       public K get(int index) {
     63         return entryList.get(index).getKey();
     64       }
     65 
     66       @Override
     67       ImmutableCollection<K> delegateCollection() {
     68         return ImmutableMapKeySet.this;
     69       }
     70 
     71     };
     72   }
     73 
     74   @Override
     75   boolean isPartialView() {
     76     return true;
     77   }
     78 
     79   @GwtIncompatible("serialization")
     80   @Override Object writeReplace() {
     81     return new KeySetSerializedForm<K>(map);
     82   }
     83 
     84   @GwtIncompatible("serialization")
     85   private static class KeySetSerializedForm<K> implements Serializable {
     86     final ImmutableMap<K, ?> map;
     87     KeySetSerializedForm(ImmutableMap<K, ?> map) {
     88       this.map = map;
     89     }
     90     Object readResolve() {
     91       return map.keySet();
     92     }
     93     private static final long serialVersionUID = 0;
     94   }
     95 }
     96