001 /*--------------------------------------------------------------------------+ 002 $Id: UnmodifiableIterator.java 26283 2010-02-18 11:18:57Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.collections; 019 020 import java.util.Iterator; 021 022 /** 023 * This is a wrapper for a {@link Iterator} prohibiting all calls which would 024 * modify its owning container. All prohibited methods throw an 025 * {@link UnsupportedOperationException}. 026 * 027 * @author Benjamin Hummel 028 * @author $Author: juergens $ 029 * 030 * @version $Revision: 26283 $ 031 * @levd.rating GREEN Hash: 73D7E245FAE2147047C3C3D4B19E6C48 032 */ 033 public class UnmodifiableIterator<E> implements Iterator<E> { 034 035 /** The underlying iterator. */ 036 private final Iterator<E> i; 037 038 /** 039 * Creates a new unmodifiable iterator from another iterator. All 040 * modifications to the underlying iterator will directly be visible in this 041 * wrapper. 042 */ 043 public UnmodifiableIterator(Iterator<E> i) { 044 if (i == null) { 045 throw new IllegalArgumentException( 046 "Underlying iterator may not be null!"); 047 } 048 this.i = i; 049 } 050 051 /** {@inheritDoc} */ 052 public boolean hasNext() { 053 return i.hasNext(); 054 } 055 056 /** {@inheritDoc} */ 057 public E next() { 058 return i.next(); 059 } 060 061 /** 062 * Operation is not supported. 063 * 064 * @throws UnsupportedOperationException 065 */ 066 public void remove() { 067 throw new UnsupportedOperationException(); 068 } 069 }