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 *      https://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;
020import java.util.Map.Entry;
021import java.util.Objects;
022
023import org.apache.commons.collections4.KeyValue;
024
025/**
026 * Provides a base decorator that allows additional functionality to be
027 * added to a {@link Entry Map.Entry}.
028 *
029 * @param <K> The type of keys
030 * @param <V> The type of mapped values
031 * @since 3.0
032 */
033public abstract class AbstractMapEntryDecorator<K, V> implements Map.Entry<K, V>, KeyValue<K, V> {
034
035    /** The {@code Map.Entry} to decorate */
036    private final Map.Entry<K, V> entry;
037
038    /**
039     * Constructor that wraps (not copies).
040     *
041     * @param entry  The {@code Map.Entry} to decorate, must not be null
042     * @throws NullPointerException if the collection is null
043     */
044    public AbstractMapEntryDecorator(final Map.Entry<K, V> entry) {
045        this.entry = Objects.requireNonNull(entry, "entry");
046    }
047
048    @Override
049    public boolean equals(final Object object) {
050        if (object == this) {
051            return true;
052        }
053        return entry.equals(object);
054    }
055
056    @Override
057    public K getKey() {
058        return entry.getKey();
059    }
060
061    /**
062     * Gets the map being decorated.
063     *
064     * @return The decorated map
065     */
066    protected Map.Entry<K, V> getMapEntry() {
067        return entry;
068    }
069
070    @Override
071    public V getValue() {
072        return entry.getValue();
073    }
074
075    @Override
076    public int hashCode() {
077        return entry.hashCode();
078    }
079
080    @Override
081    public V setValue(final V value) {
082        return entry.setValue(value);
083    }
084
085    @Override
086    public String toString() {
087        return entry.toString();
088    }
089
090}