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.Entry;
020
021import org.apache.commons.collections4.KeyValue;
022/**
023 * Abstract pair class to assist with creating {@code KeyValue}
024 * and {@link Entry Map.Entry} implementations.
025 *
026 * @param <K> The type of keys
027 * @param <V> The type of values
028 * @since 3.0
029 */
030public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
031
032    /** The key */
033    private K key;
034
035    /** The value */
036    private V value;
037
038    /**
039     * Constructs a new pair with the specified key and given value.
040     *
041     * @param key  The key for the entry, may be null
042     * @param value  The value for the entry, may be null
043     */
044    protected AbstractKeyValue(final K key, final V value) {
045        this.key = key;
046        this.value = value;
047    }
048
049    /**
050     * Gets the key from the pair.
051     *
052     * @return The key
053     */
054    @Override
055    public K getKey() {
056        return key;
057    }
058
059    /**
060     * Gets the value from the pair.
061     *
062     * @return The value
063     */
064    @Override
065    public V getValue() {
066        return value;
067    }
068
069    /**
070     * Sets the key.
071     *
072     * @param key The key.
073     * @return The previous key.
074     */
075    protected K setKey(final K key) {
076        final K old = this.key;
077        this.key = key;
078        return old;
079    }
080
081    /**
082     * Sets the value.
083     *
084     * @param value The value.
085     * @return The previous value.
086     */
087    protected V setValue(final V value) {
088        final V old = this.value;
089        this.value = value;
090        return old;
091    }
092
093    /**
094     * Gets a debugging String view of the pair.
095     *
096     * @return A String view of the entry
097     */
098    @Override
099    public String toString() {
100        return new StringBuilder()
101            .append(getKey())
102            .append('=')
103            .append(getValue())
104            .toString();
105    }
106
107}