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.collections.iterators;
18
19 import org.apache.commons.collections.OrderedMapIterator;
20 import org.apache.commons.collections.Unmodifiable;
21
22 /**
23 * Decorates an ordered map iterator such that it cannot be modified.
24 * <p>
25 * Attempts to modify it will result in an UnsupportedOperationException.
26 *
27 * @since 3.0
28 * @version $Id: UnmodifiableOrderedMapIterator.java 1435970 2013-01-20 21:20:22Z tn $
29 */
30 public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>,
31 Unmodifiable {
32
33 /** The iterator being decorated */
34 private final OrderedMapIterator<K, V> iterator;
35
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 IllegalArgumentException if the iterator is null
45 */
46 public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator(
47 final OrderedMapIterator<K, V> iterator) {
48
49 if (iterator == null) {
50 throw new IllegalArgumentException("OrderedMapIterator must not be null");
51 }
52 if (iterator instanceof Unmodifiable) {
53 return iterator;
54 }
55 return new UnmodifiableOrderedMapIterator<K, V>(iterator);
56 }
57
58 //-----------------------------------------------------------------------
59 /**
60 * Constructor.
61 *
62 * @param iterator the iterator to decorate
63 */
64 private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, V> iterator) {
65 super();
66 this.iterator = iterator;
67 }
68
69 //-----------------------------------------------------------------------
70 public boolean hasNext() {
71 return iterator.hasNext();
72 }
73
74 public K next() {
75 return iterator.next();
76 }
77
78 public boolean hasPrevious() {
79 return iterator.hasPrevious();
80 }
81
82 public K previous() {
83 return iterator.previous();
84 }
85
86 public K getKey() {
87 return iterator.getKey();
88 }
89
90 public V getValue() {
91 return iterator.getValue();
92 }
93
94 public V setValue(final V value) {
95 throw new UnsupportedOperationException("setValue() is not supported");
96 }
97
98 public void remove() {
99 throw new UnsupportedOperationException("remove() is not supported");
100 }
101
102 }