001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.collection; 017 018import java.util.Iterator; 019 020/** 021 * An {@link Iterator} implementation that only allows reading elements, used as a wrapper around another iterator to make the 022 * contents immutable to the user of this iterator. 023 * 024 * @param <T> the type of the elements over which the iteration is being performed 025 */ 026public final class ReadOnlyIterator<T> implements Iterator<T> { 027 028 public static <T> ReadOnlyIterator<T> around( Iterator<T> delegate ) { 029 return new ReadOnlyIterator<T>(delegate); 030 } 031 032 private final Iterator<T> delegate; 033 034 public ReadOnlyIterator( Iterator<T> delegate ) { 035 this.delegate = delegate; 036 assert this.delegate != null; 037 } 038 039 @Override 040 public boolean hasNext() { 041 return delegate.hasNext(); 042 } 043 044 @Override 045 public T next() { 046 return delegate.next(); 047 } 048 049 @Override 050 public void remove() { 051 throw new UnsupportedOperationException(); 052 } 053 054}