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    *      https://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  
19  import java.util.Map;
20  import java.util.Map.Entry;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.KeyValue;
24  
25  /**
26   * Provides a base decorator that allows additional functionality to be
27   * added to a {@link Entry Map.Entry}.
28   *
29   * @param <K> The type of keys
30   * @param <V> The type of mapped values
31   * @since 3.0
32   */
33  public abstract class AbstractMapEntryDecorator<K, V> implements Map.Entry<K, V>, KeyValue<K, V> {
34  
35      /** The {@code Map.Entry} to decorate */
36      private final Map.Entry<K, V> entry;
37  
38      /**
39       * Constructor that wraps (not copies).
40       *
41       * @param entry  The {@code Map.Entry} to decorate, must not be null
42       * @throws NullPointerException if the collection is null
43       */
44      public AbstractMapEntryDecorator(final Map.Entry<K, V> entry) {
45          this.entry = Objects.requireNonNull(entry, "entry");
46      }
47  
48      @Override
49      public boolean equals(final Object object) {
50          if (object == this) {
51              return true;
52          }
53          return entry.equals(object);
54      }
55  
56      @Override
57      public K getKey() {
58          return entry.getKey();
59      }
60  
61      /**
62       * Gets the map being decorated.
63       *
64       * @return The decorated map
65       */
66      protected Map.Entry<K, V> getMapEntry() {
67          return entry;
68      }
69  
70      @Override
71      public V getValue() {
72          return entry.getValue();
73      }
74  
75      @Override
76      public int hashCode() {
77          return entry.hashCode();
78      }
79  
80      @Override
81      public V setValue(final V value) {
82          return entry.setValue(value);
83      }
84  
85      @Override
86      public String toString() {
87          return entry.toString();
88      }
89  
90  }