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 iterable that adapts the values returned by a delegate iterable.
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 DelegateIterable<T, V> implements Iterable<T> {
030
031    public static <T, V> Iterable<T> around( Iterable<V> delegate,
032                                             Function<V, T> converter ) {
033        return new DelegateIterable<T, V>(delegate, converter);
034    }
035
036    private final Function<V, T> converter;
037    private final Iterable<V> delegate;
038
039    protected DelegateIterable( Iterable<V> delegate,
040                                Function<V, T> converter ) {
041        this.converter = converter;
042        this.delegate = delegate;
043    }
044
045    @Override
046    public Iterator<T> iterator() {
047        return DelegateIterator.around(delegate.iterator(), converter);
048    }
049}