UnmodifiableOrderedMapIterator.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.OrderedMapIterator;
  20. import org.apache.commons.collections4.Unmodifiable;

  21. /**
  22.  * Decorates an ordered 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 UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>,
  32.         Unmodifiable {

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

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

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

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

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

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

  74.     @Override
  75.     public boolean hasPrevious() {
  76.         return iterator.hasPrevious();
  77.     }

  78.     @Override
  79.     public K next() {
  80.         return iterator.next();
  81.     }

  82.     @Override
  83.     public K previous() {
  84.         return iterator.previous();
  85.     }

  86.     @Override
  87.     public void remove() {
  88.         throw new UnsupportedOperationException("remove() is not supported");
  89.     }

  90.     @Override
  91.     public V setValue(final V value) {
  92.         throw new UnsupportedOperationException("setValue() is not supported");
  93.     }

  94. }