Home » openjdk-7 » java » util » [javadoc | source]

    1   /*
    2    * Copyright 2008 Google Inc.
    3    * 
    4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    5    * use this file except in compliance with the License. You may obtain a copy of
    6    * 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, WITHOUT
   12    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13    * License for the specific language governing permissions and limitations under
   14    * the License.
   15    */
   16   package java.util;
   17   
   18   import static java.util.Utility.equalsWithNullCheck;
   19   
   20   /**
   21    * Basic {@link Map.Entry} implementation that implements hashCode, equals, and
   22    * toString.
   23    */
   24   abstract class AbstractMapEntry<K, V> implements Map.Entry<K, V> {
   25   
   26     @Override
   27     public final boolean equals(Object other) {
   28       if (other instanceof Map.Entry) {
   29         Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other;
   30         if (equalsWithNullCheck(getKey(), entry.getKey())
   31             && equalsWithNullCheck(getValue(), entry.getValue())) {
   32           return true;
   33         }
   34       }
   35       return false;
   36     }
   37   
   38     /**
   39      * Calculate the hash code using Sun's specified algorithm.
   40      */
   41     @Override
   42     public final int hashCode() {
   43       int keyHash = 0;
   44       int valueHash = 0;
   45       if (getKey() != null) {
   46         keyHash = getKey().hashCode();
   47       }
   48       if (getValue() != null) {
   49         valueHash = getValue().hashCode();
   50       }
   51       return keyHash ^ valueHash;
   52     }
   53   
   54     @Override
   55     public final String toString() {
   56       return getKey() + "=" + getValue();
   57     }
   58   }

Home » openjdk-7 » java » util » [javadoc | source]