UnmodifiableMapEntry.java

  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. import java.util.Map;

  19. import org.apache.commons.collections4.KeyValue;
  20. import org.apache.commons.collections4.Unmodifiable;

  21. /**
  22.  * A {@link java.util.Map.Entry Map.Entry} that throws
  23.  * UnsupportedOperationException when {@code setValue} is called.
  24.  *
  25.  * @param <K> the type of keys
  26.  * @param <V> the type of mapped values
  27.  * @since 3.0
  28.  */
  29. public final class UnmodifiableMapEntry<K, V> extends AbstractMapEntry<K, V> implements Unmodifiable {

  30.     /**
  31.      * Constructs a new entry with the specified key and given value.
  32.      *
  33.      * @param key  the key for the entry, may be null
  34.      * @param value  the value for the entry, may be null
  35.      */
  36.     public UnmodifiableMapEntry(final K key, final V value) {
  37.         super(key, value);
  38.     }

  39.     /**
  40.      * Constructs a new entry from the specified {@code KeyValue}.
  41.      *
  42.      * @param pair  the pair to copy, must not be null
  43.      * @throws NullPointerException if the entry is null
  44.      */
  45.     public UnmodifiableMapEntry(final KeyValue<? extends K, ? extends V> pair) {
  46.         super(pair.getKey(), pair.getValue());
  47.     }

  48.     /**
  49.      * Constructs a new entry from the specified {@code Map.Entry}.
  50.      *
  51.      * @param entry  the entry to copy, must not be null
  52.      * @throws NullPointerException if the entry is null
  53.      */
  54.     public UnmodifiableMapEntry(final Map.Entry<? extends K, ? extends V> entry) {
  55.         super(entry.getKey(), entry.getValue());
  56.     }

  57.     /**
  58.      * Throws UnsupportedOperationException.
  59.      *
  60.      * @param value  the new value
  61.      * @return the previous value
  62.      * @throws UnsupportedOperationException always
  63.      */
  64.     @Override
  65.     public V setValue(final V value) {
  66.         throw new UnsupportedOperationException("setValue() is not supported");
  67.     }

  68. }