    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.keyvalue;
    018
    019import java.util.Map;
    020
    021import org.apache.commons.collections4.KeyValue;
    022
    023/**
    024 * Provides a base decorator that allows additional functionality to be
    025 * added to a {@link java.util.Map.Entry Map.Entry}.
    026 *
    027 * @since 3.0
    028 */
    029public abstract class AbstractMapEntryDecorator<K, V> implements Map.Entry<K, V>, KeyValue<K, V> {
    030
    031    /** The <code>Map.Entry</code> to decorate */
    032    private final Map.Entry<K, V> entry;
    033
    034    /**
    035     * Constructor that wraps (not copies).
    036     *
    037     * @param entry  the <code>Map.Entry</code> to decorate, must not be null
    038     * @throws NullPointerException if the collection is null
    039     */
    040    public AbstractMapEntryDecorator(final Map.Entry<K, V> entry) {
    041        if (entry == null) {
    042            throw new NullPointerException("Map Entry must not be null.");
    043        }
    044        this.entry = entry;
    045    }
    046
    047    /**
    048     * Gets the map being decorated.
    049     *
    050     * @return the decorated map
    051     */
    052    protected Map.Entry<K, V> getMapEntry() {
    053        return entry;
    054    }
    055
    056    //-----------------------------------------------------------------------
    057
    058    @Override
    059    public K getKey() {
    060        return entry.getKey();
    061    }
    062
    063    @Override
    064    public V getValue() {
    065        return entry.getValue();
    066    }
    067
    068    @Override
    069    public V setValue(final V object) {
    070        return entry.setValue(object);
    071    }
    072
    073    @Override
    074    public boolean equals(final Object object) {
    075        if (object == this) {
    076            return true;
    077        }
    078        return entry.equals(object);
    079    }
    080
    081    @Override
    082    public int hashCode() {
    083        return entry.hashCode();
    084    }
    085
    086    @Override
    087    public String toString() {
    088        return entry.toString();
    089    }
    090
    091}