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