UnmodifiableMapIterator.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.iterators;

  18. import java.util.Objects;

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

  21. /**
  22.  * Decorates a map iterator such that it cannot be modified.
  23.  * <p>
  24.  * Attempts to modify it will result in an UnsupportedOperationException.
  25.  * </p>
  26.  *
  27.  * @param <K> the type of keys
  28.  * @param <V> the type of mapped values
  29.  * @since 3.0
  30.  */
  31. public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable {

  32.     /**
  33.      * Decorates the specified iterator such that it cannot be modified.
  34.      *
  35.      * @param <K>  the key type
  36.      * @param <V>  the value type
  37.      * @param iterator  the iterator to decorate
  38.      * @return a new unmodifiable map iterator
  39.      * @throws NullPointerException if the iterator is null
  40.      */
  41.     public static <K, V> MapIterator<K, V> unmodifiableMapIterator(
  42.             final MapIterator<? extends K, ? extends V> iterator) {
  43.         Objects.requireNonNull(iterator, "iterator");
  44.         if (iterator instanceof Unmodifiable) {
  45.             @SuppressWarnings("unchecked") // safe to upcast
  46.             final MapIterator<K, V> tmpIterator = (MapIterator<K, V>) iterator;
  47.             return tmpIterator;
  48.         }
  49.         return new UnmodifiableMapIterator<>(iterator);
  50.     }

  51.     /** The iterator being decorated */
  52.     private final MapIterator<? extends K, ? extends V> iterator;

  53.     /**
  54.      * Constructs a new instance.
  55.      *
  56.      * @param iterator  the iterator to decorate
  57.      */
  58.     private UnmodifiableMapIterator(final MapIterator<? extends K, ? extends V> iterator) {
  59.         this.iterator = iterator;
  60.     }

  61.     @Override
  62.     public K getKey() {
  63.         return iterator.getKey();
  64.     }

  65.     @Override
  66.     public V getValue() {
  67.         return iterator.getValue();
  68.     }

  69.     @Override
  70.     public boolean hasNext() {
  71.         return iterator.hasNext();
  72.     }

  73.     @Override
  74.     public K next() {
  75.         return iterator.next();
  76.     }

  77.     @Override
  78.     public void remove() {
  79.         throw new UnsupportedOperationException("remove() is not supported");
  80.     }

  81.     @Override
  82.     public V setValue(final V value) {
  83.         throw new UnsupportedOperationException("setValue() is not supported");
  84.     }

  85. }