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