AbstractKeyValue.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.keyvalue;

  18. import org.apache.commons.collections4.KeyValue;

  19. /**
  20.  * Abstract pair class to assist with creating {@code KeyValue}
  21.  * and {@link java.util.Map.Entry Map.Entry} implementations.
  22.  *
  23.  * @param <K> the type of keys
  24.  * @param <V> the type of values
  25.  * @since 3.0
  26.  */
  27. public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {

  28.     /** The key */
  29.     private K key;
  30.     /** The value */
  31.     private V value;

  32.     /**
  33.      * Constructs a new pair with the specified key and given value.
  34.      *
  35.      * @param key  the key for the entry, may be null
  36.      * @param value  the value for the entry, may be null
  37.      */
  38.     protected AbstractKeyValue(final K key, final V value) {
  39.         this.key = key;
  40.         this.value = value;
  41.     }

  42.     /**
  43.      * Gets the key from the pair.
  44.      *
  45.      * @return the key
  46.      */
  47.     @Override
  48.     public K getKey() {
  49.         return key;
  50.     }

  51.     /**
  52.      * Gets the value from the pair.
  53.      *
  54.      * @return the value
  55.      */
  56.     @Override
  57.     public V getValue() {
  58.         return value;
  59.     }

  60.     /**
  61.      * Sets the key.
  62.      *
  63.      * @param key The key.
  64.      * @return The previous key.
  65.      */
  66.     protected K setKey(final K key) {
  67.         final K old = this.key;
  68.         this.key = key;
  69.         return old;
  70.     }

  71.     /**
  72.      * Sets the value.
  73.      *
  74.      * @param value The value.
  75.      * @return The previous value.
  76.      */
  77.     protected V setValue(final V value) {
  78.         final V old = this.value;
  79.         this.value = value;
  80.         return old;
  81.     }

  82.     /**
  83.      * Gets a debugging String view of the pair.
  84.      *
  85.      * @return a String view of the entry
  86.      */
  87.     @Override
  88.     public String toString() {
  89.         return new StringBuilder()
  90.             .append(getKey())
  91.             .append('=')
  92.             .append(getValue())
  93.             .toString();
  94.     }

  95. }