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 * https://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.MapIterator;
22 import org.apache.commons.collections4.Unmodifiable;
23
24 /**
25 * Decorates a 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 UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable {
35
36 /**
37 * Decorates the specified iterator such that it cannot be modified.
38 *
39 * @param <K> The key type.
40 * @param <V> The value type.
41 * @param iterator The iterator to decorate.
42 * @return A new unmodifiable map iterator.
43 * @throws NullPointerException If the iterator is null.
44 */
45 public static <K, V> MapIterator<K, V> unmodifiableMapIterator(
46 final MapIterator<? extends K, ? extends V> iterator) {
47 Objects.requireNonNull(iterator, "iterator");
48 if (iterator instanceof Unmodifiable) {
49 @SuppressWarnings("unchecked") // safe to upcast
50 final MapIterator<K, V> tmpIterator = (MapIterator<K, V>) iterator;
51 return tmpIterator;
52 }
53 return new UnmodifiableMapIterator<>(iterator);
54 }
55
56 /** The iterator being decorated */
57 private final MapIterator<? extends K, ? extends V> iterator;
58
59 /**
60 * Constructs a new instance.
61 *
62 * @param iterator The iterator to decorate
63 */
64 private UnmodifiableMapIterator(final MapIterator<? extends K, ? extends V> iterator) {
65 this.iterator = iterator;
66 }
67
68 @Override
69 public K getKey() {
70 return iterator.getKey();
71 }
72
73 @Override
74 public V getValue() {
75 return iterator.getValue();
76 }
77
78 @Override
79 public boolean hasNext() {
80 return iterator.hasNext();
81 }
82
83 @Override
84 public K next() {
85 return iterator.next();
86 }
87
88 /**
89 * Always throws {@link UnsupportedOperationException}.
90 *
91 * @throws UnsupportedOperationException Always thrown.
92 */
93 @Override
94 public void remove() {
95 throw new UnsupportedOperationException("remove() is not supported");
96 }
97
98 /**
99 * Always throws {@link UnsupportedOperationException}.
100 *
101 * @param value Ignored.
102 * @throws UnsupportedOperationException Always thrown.
103 */
104 @Override
105 public V setValue(final V value) {
106 throw new UnsupportedOperationException("setValue() is not supported");
107 }
108
109 }