View Javadoc

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.collections.keyvalue;
18  
19  import org.apache.commons.collections.KeyValue;
20  
21  /**
22   * Abstract pair class to assist with creating <code>KeyValue</code>
23   * and {@link java.util.Map.Entry Map.Entry} implementations.
24   *
25   * @since 3.0
26   * @version $Id: AbstractKeyValue.java 1429905 2013-01-07 17:15:14Z ggregory $
27   */
28  public abstract class AbstractKeyValue<K, V> implements KeyValue<K, V> {
29  
30      /** The key */
31      protected K key;
32      /** The value */
33      protected V value;
34  
35      /**
36       * Constructs a new pair with the specified key and given value.
37       *
38       * @param key  the key for the entry, may be null
39       * @param value  the value for the entry, may be null
40       */
41      protected AbstractKeyValue(final K key, final V value) {
42          super();
43          this.key = key;
44          this.value = value;
45      }
46  
47      /**
48       * Gets the key from the pair.
49       *
50       * @return the key 
51       */
52      public K getKey() {
53          return key;
54      }
55  
56      /**
57       * Gets the value from the pair.
58       *
59       * @return the value
60       */
61      public V getValue() {
62          return value;
63      }
64  
65      /**
66       * Gets a debugging String view of the pair.
67       * 
68       * @return a String view of the entry
69       */
70      @Override
71      public String toString() {
72          return new StringBuilder()
73              .append(getKey())
74              .append('=')
75              .append(getValue())
76              .toString();
77      }
78  
79  }