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 */ 029public final class UnmodifiableOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>, 030 Unmodifiable { 031 032 /** The iterator being decorated */ 033 private final OrderedMapIterator<? extends K, ? extends V> iterator; 034 035 //----------------------------------------------------------------------- 036 /** 037 * Decorates the specified iterator such that it cannot be modified. 038 * 039 * @param <K> the key type 040 * @param <V> the value type 041 * @param iterator the iterator to decorate 042 * @return a new unmodifiable ordered map iterator 043 * @throws NullPointerException if the iterator is null 044 */ 045 public static <K, V> OrderedMapIterator<K, V> unmodifiableOrderedMapIterator( 046 final OrderedMapIterator<K, ? extends V> iterator) { 047 048 if (iterator == null) { 049 throw new NullPointerException("OrderedMapIterator must not be null"); 050 } 051 if (iterator instanceof Unmodifiable) { 052 @SuppressWarnings("unchecked") // safe to upcast 053 final OrderedMapIterator<K, V> tmpIterator = (OrderedMapIterator<K, V>) iterator; 054 return tmpIterator; 055 } 056 return new UnmodifiableOrderedMapIterator<>(iterator); 057 } 058 059 //----------------------------------------------------------------------- 060 /** 061 * Constructor. 062 * 063 * @param iterator the iterator to decorate 064 */ 065 private UnmodifiableOrderedMapIterator(final OrderedMapIterator<K, ? extends V> iterator) { 066 super(); 067 this.iterator = iterator; 068 } 069 070 //----------------------------------------------------------------------- 071 @Override 072 public boolean hasNext() { 073 return iterator.hasNext(); 074 } 075 076 @Override 077 public K next() { 078 return iterator.next(); 079 } 080 081 @Override 082 public boolean hasPrevious() { 083 return iterator.hasPrevious(); 084 } 085 086 @Override 087 public K previous() { 088 return iterator.previous(); 089 } 090 091 @Override 092 public K getKey() { 093 return iterator.getKey(); 094 } 095 096 @Override 097 public V getValue() { 098 return iterator.getValue(); 099 } 100 101 @Override 102 public V setValue(final V value) { 103 throw new UnsupportedOperationException("setValue() is not supported"); 104 } 105 106 @Override 107 public void remove() { 108 throw new UnsupportedOperationException("remove() is not supported"); 109 } 110 111}