Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 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 import com.google.common.base.Objects;
     21 
     22 import java.util.Map.Entry;
     23 
     24 import javax.annotation.Nullable;
     25 
     26 /**
     27  * Implementation of the {@code equals}, {@code hashCode}, and {@code toString}
     28  * methods of {@code Entry}.
     29  *
     30  * @author Jared Levy
     31  */
     32 @GwtCompatible
     33 abstract class AbstractMapEntry<K, V> implements Entry<K, V> {
     34 
     35   public abstract K getKey();
     36 
     37   public abstract V getValue();
     38 
     39   public V setValue(V value) {
     40     throw new UnsupportedOperationException();
     41   }
     42 
     43   @Override public boolean equals(@Nullable Object object) {
     44     if (object instanceof Entry) {
     45       Entry<?, ?> that = (Entry<?, ?>) object;
     46       return Objects.equal(this.getKey(), that.getKey())
     47           && Objects.equal(this.getValue(), that.getValue());
     48     }
     49     return false;
     50   }
     51 
     52   @Override public int hashCode() {
     53     K k = getKey();
     54     V v = getValue();
     55     return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
     56   }
     57 
     58   /**
     59    * Returns a string representation of the form <code>{key}={value}</code>.
     60    */
     61   @Override public String toString() {
     62     return getKey() + "=" + getValue();
     63   }
     64 }
     65