    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.collections.keyvalue;
    18
    19  import java.util.Map;
    20
    21  /***
    22   * Abstract Pair class to assist with creating correct Map Entry implementations.
    23   *
    24   * @since Commons Collections 3.0
    25   * @version $Revision: 438363 $ $Date: 2006-08-30 05:48:00 +0100 (Wed, 30 Aug 2006) $
    26   *
    27   * @author James Strachan
    28   * @author Michael A. Smith
    29   * @author Neil O'Toole
    30   * @author Stephen Colebourne
    31   */
    32  public abstract class AbstractMapEntry extends AbstractKeyValue implements Map.Entry {
    33
    34      /***
    35       * Constructs a new entry with the given key and given value.
    36       *
    37       * @param key  the key for the entry, may be null
    38       * @param value  the value for the entry, may be null
    39       */
    40      protected AbstractMapEntry(Object key, Object value) {
    41          super(key, value);
    42      }
    43
    44      // Map.Entry interface
    45      //-------------------------------------------------------------------------
    46      /***
    47       * Sets the value stored in this Map Entry.
    48       * <p>
    49       * This Map Entry is not connected to a Map, so only the local data is changed.
    50       *
    51       * @param value  the new value
    52       * @return the previous value
    53       */
    54      public Object setValue(Object value) {
    55          Object answer = this.value;
    56          this.value = value;
    57          return answer;
    58      }
    59
    60      /***
    61       * Compares this Map Entry with another Map Entry.
    62       * <p>
    63       * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
    64       *
    65       * @param obj  the object to compare to
    66       * @return true if equal key and value
    67       */
    68      public boolean equals(Object obj) {
    69          if (obj == this) {
    70              return true;
    71          }
    72          if (obj instanceof Map.Entry == false) {
    73              return false;
    74          }
    75          Map.Entry other = (Map.Entry) obj;
    76          return
    77              (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
    78              (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
    79      }
    80
    81      /***
    82       * Gets a hashCode compatible with the equals method.
    83       * <p>
    84       * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
    85       *
    86       * @return a suitable hash code
    87       */
    88      public int hashCode() {
    89          return (getKey() == null ? 0 : getKey().hashCode()) ^
    90                 (getValue() == null ? 0 : getValue().hashCode());
    91      }
    92
    93  }