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