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