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  
22  import org.apache.commons.collections4.KeyValue;
23  import org.apache.commons.collections4.Unmodifiable;
24  
25  /**
26   * A {@link Entry Map.Entry} that throws
27   * UnsupportedOperationException when {@code setValue} is called.
28   *
29   * @param <K> The type of keys
30   * @param <V> The type of mapped values
31   * @since 3.0
32   */
33  public final class UnmodifiableMapEntry<K, V> extends AbstractMapEntry<K, V> implements Unmodifiable {
34  
35      /**
36       * Constructs a new entry 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      public UnmodifiableMapEntry(final K key, final V value) {
42          super(key, value);
43      }
44  
45      /**
46       * Constructs a new entry from the specified {@code KeyValue}.
47       *
48       * @param pair  The pair to copy, must not be null
49       * @throws NullPointerException if the entry is null
50       */
51      public UnmodifiableMapEntry(final KeyValue<? extends K, ? extends V> pair) {
52          super(pair.getKey(), pair.getValue());
53      }
54  
55      /**
56       * Constructs a new entry from the specified {@code Map.Entry}.
57       *
58       * @param entry  The entry to copy, must not be null
59       * @throws NullPointerException if the entry is null
60       */
61      public UnmodifiableMapEntry(final Map.Entry<? extends K, ? extends V> entry) {
62          super(entry.getKey(), entry.getValue());
63      }
64  
65      /**
66       * Throws UnsupportedOperationException.
67       *
68       * @param value  The new value
69       * @return The previous value
70       * @throws UnsupportedOperationException always
71       */
72      @Override
73      public V setValue(final V value) {
74          throw new UnsupportedOperationException("setValue() is not supported");
75      }
76  
77  }