001    /*
002     * Licensed under the Apache License, Version 2.0 (the "License");
003     * you may not use this file except in compliance with the License.
004     * You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     * Unless required by applicable law or agreed to in writing, software
009     * distributed under the License is distributed on an "AS IS" BASIS,
010     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     * See the License for the specific language governing permissions and
012     * limitations under the License.
013     */
014    
015    package org.apache.commons.functor.generator;
016    
017    import org.apache.commons.functor.UnaryProcedure;
018    
019    import java.util.Iterator;
020    
021    /**
022     * Adapts an {@link Iterator} to the {@link Generator} interface.
023     *
024     * @since 1.0
025     * @version $Revision: 1156798 $ $Date: 2011-08-11 22:11:07 +0200 (Thu, 11 Aug 2011) $
026     * @author Jason Horman (jason@jhorman.org)
027     * @author Rodney Waldhoff
028     */
029    public final class IteratorToGeneratorAdapter<E> extends BaseGenerator<E> {
030        // instance variables
031        //-----------------------------------------------------
032    
033        private final Iterator<? extends E> iter;
034    
035        // constructors
036        //-----------------------------------------------------
037        /**
038         * Create a new IteratorToGeneratorAdapter.
039         * @param iter Iterator to adapt
040         */
041        public IteratorToGeneratorAdapter(Iterator<? extends E> iter) {
042            if (null == iter) {
043                throw new IllegalArgumentException("Iterator argument was null");
044            }
045            this.iter = iter;
046        }
047    
048        // instance methods
049        //-----------------------------------------------------
050        /**
051         * {@inheritDoc}
052         */
053        public void run(UnaryProcedure<? super E> proc) {
054            while (iter.hasNext()) {
055                proc.run(iter.next());
056                if (isStopped()) {
057                    break;
058                }
059            }
060        }
061    
062        /**
063         * {@inheritDoc}
064         */
065        public boolean equals(Object obj) {
066            if (obj == this) {
067                return true;
068            }
069            if (!(obj instanceof IteratorToGeneratorAdapter<?>)) {
070                return false;
071            }
072            IteratorToGeneratorAdapter<?> that = (IteratorToGeneratorAdapter<?>) obj;
073            return this.iter.equals(that.iter);
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        public int hashCode() {
080            int hash = "IteratorToGeneratorAdapater".hashCode();
081            hash <<= 2;
082            hash ^= iter.hashCode();
083            return hash;
084        }
085    
086        /**
087         * {@inheritDoc}
088         */
089        public String toString() {
090            return "IteratorToGeneratorAdapter<" + iter + ">";
091        }
092    
093        // static methods
094        //-----------------------------------------------------
095        /**
096         * Adapt an Iterator to the Generator interface.
097         * @param iter to adapt
098         * @return IteratorToGeneratorAdapter
099         */
100        public static <E> IteratorToGeneratorAdapter<E> adapt(Iterator<? extends E> iter) {
101            return null == iter ? null : new IteratorToGeneratorAdapter<E>(iter);
102        }
103    
104    }