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.iterators;
18  
19  import java.util.Objects;
20  
21  import org.apache.commons.collections4.OrderedMapIterator;
22  import org.apache.commons.collections4.Unmodifiable;
23  
24  /**
25   * Decorates an ordered map iterator such that it cannot be modified.
26   * <p>
27   * Attempts to modify it will result in an UnsupportedOperationException.
28   * </p>
29   *
30   * @param <K> the type of keys
31   * @param <V> the type of mapped values
32   * @since 3.0
33   */
34  public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>,
35          Unmodifiable {
36  
37      /**
38       * Decorates the specified iterator such that it cannot be modified.
39       *
40       * @param <K>  the key type
41       * @param <V>  the value type
42       * @param iterator  the iterator to decorate
43       * @return a new unmodifiable ordered map iterator
44       * @throws NullPointerException if the iterator is null
45       */
46      public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator(
47              final OrderedMapIterator<K, ? extends V> iterator) {
48          Objects.requireNonNull(iterator, "iterator");
49          if (iterator instanceof Unmodifiable) {
50              @SuppressWarnings("unchecked") // safe to upcast
51              final OrderedMapIterator<K, V> tmpIterator = (OrderedMapIterator<K, V>) iterator;
52              return tmpIterator;
53          }
54          return new UnmodifiableOrderedMapIterator<>(iterator);
55      }
56  
57      /** The iterator being decorated */
58      private final OrderedMapIterator<? extends K, ? extends V> iterator;
59  
60      /**
61       * Constructs a new instance.
62       *
63       * @param iterator  the iterator to decorate
64       */
65      private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, ? extends V> iterator) {
66          this.iterator = iterator;
67      }
68  
69      @Override
70      public K getKey() {
71          return iterator.getKey();
72      }
73  
74      @Override
75      public V getValue() {
76          return iterator.getValue();
77      }
78  
79      @Override
80      public boolean hasNext() {
81          return iterator.hasNext();
82      }
83  
84      @Override
85      public boolean hasPrevious() {
86          return iterator.hasPrevious();
87      }
88  
89      @Override
90      public K next() {
91          return iterator.next();
92      }
93  
94      @Override
95      public K previous() {
96          return iterator.previous();
97      }
98  
99      @Override
100     public void remove() {
101         throw new UnsupportedOperationException("remove() is not supported");
102     }
103 
104     @Override
105     public V setValue(final V value) {
106         throw new UnsupportedOperationException("setValue() is not supported");
107     }
108 
109 }