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