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