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 */
016
017package org.modeshape.common.collection;
018
019import java.util.Iterator;
020import org.modeshape.common.function.Function;
021
022/**
023 * An iterator that adapts the value returned by a delegate iterator.
024 *
025 * @author Randall Hauch (rhauch@redhat.com)
026 * @param <T> the iterator's type
027 * @param <V> the type of the delegate iterator
028 */
029public class DelegateIterator<T, V> implements Iterator<T> {
030
031    public static <T, V> Iterator<T> around( Iterator<V> delegate,
032                                             Function<V, T> converter ) {
033        return new DelegateIterator<T, V>(delegate, converter);
034    }
035
036    private final Function<V, T> converter;
037    private final Iterator<V> delegate;
038
039    protected DelegateIterator( Iterator<V> delegate,
040                                Function<V, T> converter ) {
041        this.converter = converter;
042        this.delegate = delegate;
043    }
044
045    @Override
046    public boolean hasNext() {
047        return delegate.hasNext();
048    }
049
050    @Override
051    public T next() {
052        return converter.apply(delegate.next());
053    }
054
055    @Override
056    public void remove() {
057        delegate.remove();
058    }
059
060}