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.MapIterator; 020import org.apache.commons.collections4.Unmodifiable; 021 022/** 023 * Decorates a 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: UnmodifiableMapIterator.html 972421 2015-11-14 20:00:04Z tn $ 029 */ 030public final class UnmodifiableMapIterator<K, V> implements MapIterator<K, V>, Unmodifiable { 031 032 /** The iterator being decorated */ 033 private final MapIterator<? 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 map iterator 043 * @throws IllegalArgumentException if the iterator is null 044 */ 045 public static <K, V> MapIterator<K, V> unmodifiableMapIterator( 046 final MapIterator<? extends K, ? extends V> iterator) { 047 if (iterator == null) { 048 throw new IllegalArgumentException("MapIterator must not be null"); 049 } 050 if (iterator instanceof Unmodifiable) { 051 @SuppressWarnings("unchecked") // safe to upcast 052 final MapIterator<K, V> tmpIterator = (MapIterator<K, V>) iterator; 053 return tmpIterator; 054 } 055 return new UnmodifiableMapIterator<K, V>(iterator); 056 } 057 058 //----------------------------------------------------------------------- 059 /** 060 * Constructor. 061 * 062 * @param iterator the iterator to decorate 063 */ 064 private UnmodifiableMapIterator(final MapIterator<? extends K, ? extends V> iterator) { 065 super(); 066 this.iterator = iterator; 067 } 068 069 //----------------------------------------------------------------------- 070 public boolean hasNext() { 071 return iterator.hasNext(); 072 } 073 074 public K next() { 075 return iterator.next(); 076 } 077 078 public K getKey() { 079 return iterator.getKey(); 080 } 081 082 public V getValue() { 083 return iterator.getValue(); 084 } 085 086 public V setValue(final V value) { 087 throw new UnsupportedOperationException("setValue() is not supported"); 088 } 089 090 public void remove() { 091 throw new UnsupportedOperationException("remove() is not supported"); 092 } 093 094}