Home | History | Annotate | Download | only in util
      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 
     18 package java.util;
     19 
     20 /**
     21  * MapEntry is an internal class which provides an implementation of Map.Entry.
     22  */
     23 class MapEntry<K, V> implements Map.Entry<K, V>, Cloneable {
     24 
     25     K key;
     26     V value;
     27 
     28     interface Type<RT, KT, VT> {
     29         RT get(MapEntry<KT, VT> entry);
     30     }
     31 
     32     MapEntry(K theKey) {
     33         key = theKey;
     34     }
     35 
     36     MapEntry(K theKey, V theValue) {
     37         key = theKey;
     38         value = theValue;
     39     }
     40 
     41     @Override
     42     public Object clone() {
     43         try {
     44             return super.clone();
     45         } catch (CloneNotSupportedException e) {
     46             throw new AssertionError(e);
     47         }
     48     }
     49 
     50     @Override
     51     public boolean equals(Object object) {
     52         if (this == object) {
     53             return true;
     54         }
     55         if (object instanceof Map.Entry) {
     56             Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object;
     57             return (key == null ? entry.getKey() == null : key.equals(entry
     58                     .getKey()))
     59                     && (value == null ? entry.getValue() == null : value
     60                             .equals(entry.getValue()));
     61         }
     62         return false;
     63     }
     64 
     65     public K getKey() {
     66         return key;
     67     }
     68 
     69     public V getValue() {
     70         return value;
     71     }
     72 
     73     @Override
     74     public int hashCode() {
     75         return (key == null ? 0 : key.hashCode())
     76                 ^ (value == null ? 0 : value.hashCode());
     77     }
     78 
     79     public V setValue(V object) {
     80         V result = value;
     81         value = object;
     82         return result;
     83     }
     84 
     85     @Override
     86     public String toString() {
     87         return key + "=" + value;
     88     }
     89 }
     90