001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.iterators; 018 019import org.apache.commons.collections4.OrderedMapIterator; 020import org.apache.commons.collections4.Unmodifiable; 021 022/** 023 * Decorates an ordered map iterator such that it cannot be modified. 024 * <p> 025 * Attempts to modify it will result in an UnsupportedOperationException. 026 * 027 * @since 3.0 028 * @version $Id: UnmodifiableOrderedMapIterator.html 972421 2015-11-14 20:00:04Z tn $ 029 */ 030public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>, 031 Unmodifiable { 032 033 /** The iterator being decorated */ 034 private final OrderedMapIterator<? extends K, ? extends V> iterator; 035 036 //----------------------------------------------------------------------- 037 /** 038 * Decorates the specified iterator such that it cannot be modified. 039 * 040 * @param <K> the key type 041 * @param <V> the value type 042 * @param iterator the iterator to decorate 043 * @return a new unmodifiable ordered map iterator 044 * @throws IllegalArgumentException if the iterator is null 045 */ 046 public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator( 047 final OrderedMapIterator<K, ? extends V> iterator) { 048 049 if (iterator == null) { 050 throw new IllegalArgumentException("OrderedMapIterator must not be null"); 051 } 052 if (iterator instanceof Unmodifiable) { 053 @SuppressWarnings("unchecked") // safe to upcast 054 final OrderedMapIterator<K, V> tmpIterator = (OrderedMapIterator<K, V>) iterator; 055 return tmpIterator; 056 } 057 return new UnmodifiableOrderedMapIterator<K, V>(iterator); 058 } 059 060 //----------------------------------------------------------------------- 061 /** 062 * Constructor. 063 * 064 * @param iterator the iterator to decorate 065 */ 066 private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, ? extends V> iterator) { 067 super(); 068 this.iterator = iterator; 069 } 070 071 //----------------------------------------------------------------------- 072 public boolean hasNext() { 073 return iterator.hasNext(); 074 } 075 076 public K next() { 077 return iterator.next(); 078 } 079 080 public boolean hasPrevious() { 081 return iterator.hasPrevious(); 082 } 083 084 public K previous() { 085 return iterator.previous(); 086 } 087 088 public K getKey() { 089 return iterator.getKey(); 090 } 091 092 public V getValue() { 093 return iterator.getValue(); 094 } 095 096 public V setValue(final V value) { 097 throw new UnsupportedOperationException("setValue() is not supported"); 098 } 099 100 public void remove() { 101 throw new UnsupportedOperationException("remove() is not supported"); 102 } 103 104}