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.collections.iterators;
18  
19  import org.apache.commons.collections.MapIterator;
20  import org.apache.commons.collections.Unmodifiable;
21  
22  /** 
23   * Decorates a 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: UnmodifiableMapIterator.java 1429905 2013-01-07 17:15:14Z ggregory $
29   */
30  public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable {
31  
32      /** The iterator being decorated */
33      private final MapIterator<K, V> iterator;
34  
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 IllegalArgumentException if the iterator is null
44       */
45      public static <K, V> MapIterator<K, V> unmodifiableMapIterator(final MapIterator<K, V> iterator) {
46          if (iterator == null) {
47              throw new IllegalArgumentException("MapIterator must not be null");
48          }
49          if (iterator instanceof Unmodifiable) {
50              return iterator;
51          }
52          return new UnmodifiableMapIterator<K, V>(iterator);
53      }
54  
55      //-----------------------------------------------------------------------
56      /**
57       * Constructor.
58       *
59       * @param iterator  the iterator to decorate
60       */
61      private UnmodifiableMapIterator(final MapIterator<K, V> iterator) {
62          super();
63          this.iterator = iterator;
64      }
65  
66      //-----------------------------------------------------------------------
67      public boolean hasNext() {
68          return iterator.hasNext();
69      }
70  
71      public K next() {
72          return iterator.next();
73      }
74  
75      public K getKey() {
76          return iterator.getKey();
77      }
78  
79      public V getValue() {
80          return iterator.getValue();
81      }
82  
83      public V setValue(final V value) {
84          throw new UnsupportedOperationException("setValue() is not supported");
85      }
86  
87      public void remove() {
88          throw new UnsupportedOperationException("remove() is not supported");
89      }
90  
91  }