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 */ 017 package org.apache.commons.collections.iterators; 018 019 import org.apache.commons.collections.MapIterator; 020 import org.apache.commons.collections.Unmodifiable; 021 022 /** 023 * Decorates a map iterator such that it cannot be modified. 024 * 025 * @since Commons Collections 3.0 026 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 027 * 028 * @author Stephen Colebourne 029 */ 030 public final class UnmodifiableMapIterator implements MapIterator, Unmodifiable { 031 032 /** The iterator being decorated */ 033 private MapIterator iterator; 034 035 //----------------------------------------------------------------------- 036 /** 037 * Decorates the specified iterator such that it cannot be modified. 038 * 039 * @param iterator the iterator to decorate 040 * @throws IllegalArgumentException if the iterator is null 041 */ 042 public static MapIterator decorate(MapIterator iterator) { 043 if (iterator == null) { 044 throw new IllegalArgumentException("MapIterator must not be null"); 045 } 046 if (iterator instanceof Unmodifiable) { 047 return iterator; 048 } 049 return new UnmodifiableMapIterator(iterator); 050 } 051 052 //----------------------------------------------------------------------- 053 /** 054 * Constructor. 055 * 056 * @param iterator the iterator to decorate 057 */ 058 private UnmodifiableMapIterator(MapIterator iterator) { 059 super(); 060 this.iterator = iterator; 061 } 062 063 //----------------------------------------------------------------------- 064 public boolean hasNext() { 065 return iterator.hasNext(); 066 } 067 068 public Object next() { 069 return iterator.next(); 070 } 071 072 public Object getKey() { 073 return iterator.getKey(); 074 } 075 076 public Object getValue() { 077 return iterator.getValue(); 078 } 079 080 public Object setValue(Object value) { 081 throw new UnsupportedOperationException("setValue() is not supported"); 082 } 083 084 public void remove() { 085 throw new UnsupportedOperationException("remove() is not supported"); 086 } 087 088 }