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 * @version $Id: AbstractKeyValue.html 972421 2015-11-14 20:00:04Z tn $ 027 */ 028public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> { 029 030 /** The key */ 031 private K key; 032 /** The value */ 033 private V value; 034 035 /** 036 * Constructs a new pair with the specified key and given value. 037 * 038 * @param key the key for the entry, may be null 039 * @param value the value for the entry, may be null 040 */ 041 protected AbstractKeyValue(final K key, final V value) { 042 super(); 043 this.key = key; 044 this.value = value; 045 } 046 047 /** 048 * Gets the key from the pair. 049 * 050 * @return the key 051 */ 052 public K getKey() { 053 return key; 054 } 055 056 protected K setKey(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 public V getValue() { 068 return value; 069 } 070 071 protected V setValue(V value) { 072 final V old = this.value; 073 this.value = value; 074 return old; 075 } 076 077 /** 078 * Gets a debugging String view of the pair. 079 * 080 * @return a String view of the entry 081 */ 082 @Override 083 public String toString() { 084 return new StringBuilder() 085 .append(getKey()) 086 .append('=') 087 .append(getValue()) 088 .toString(); 089 } 090 091}