/*
 * Copyright (C) 2008 TranceCode Software
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 * $Id$
 */
package org.trancecode.collection;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.BlockingQueue;

import org.trancecode.annotation.ReturnsNullable;
import org.trancecode.function.TcFunctions;

/**
 * Utility methods related to {@link Iterable}.
 * 
 * @author Herve Quiroz
 */
public final class TcIterables
{
    private TcIterables()
    {
        // No instantiation
    }

    /**
     * Build an {@link Iterable} sequence of element from a {@link Iterator}
     * provided by a {@link Supplier}.
     */
    public static <T> Iterable<T> newIterable(final Supplier<Iterator<T>> iteratorSupplier)
    {
        return new IteratorIterable<T>(iteratorSupplier);
    }

    private static class IteratorIterable<T> implements Iterable<T>
    {
        private final Supplier<Iterator<T>> iteratorSupplier;

        public IteratorIterable(final Supplier<Iterator<T>> iteratorSupplier)
        {
            this.iteratorSupplier = Preconditions.checkNotNull(iteratorSupplier);
        }

        @Override
        public Iterator<T> iterator()
        {
            return iteratorSupplier.get();
        }
    }

    /**
     * Get the last argument from a sequence, or <code>null</code> if there is
     * no such last element.
     */
    @ReturnsNullable
    public static <T> T getLast(final Iterable<T> elements)
    {
        return getLast(elements, null);
    }

    /**
     * Get the last argument from a sequence, or <code>defaultElement</code> if
     * there is no such last element.
     */
    @ReturnsNullable
    public static <T> T getLast(final Iterable<T> elements, final T defaultElement)
    {
        try
        {
            return Iterables.getLast(elements);
        }
        catch (final NoSuchElementException e)
        {
            return defaultElement;
        }
    }

    public static <T> Iterable<T> append(final Iterable<T> iterable, final T... elements)
    {
        return Iterables.concat(iterable, ImmutableList.copyOf(elements));
    }

    public static <T> Iterable<T> prepend(final Iterable<T> iterable, final T... elements)
    {
        return Iterables.concat(ImmutableList.copyOf(elements), iterable);
    }

    /**
     * Compute a sequence of results by applying each function form the list to
     * the same argument.
     */
    public static <F, T> Iterable<T> applyFunctions(final Iterable<Function<F, T>> functions, final F argument)
    {
        final Function<Function<F, T>, T> applyFunction = TcFunctions.applyTo(argument);
        return Iterables.transform(functions, applyFunction);
    }

    public static <T> Iterable<T> getDescendants(final Iterable<T> parentElements,
            final Function<T, Iterable<T>> getChildFunction)
    {
        if (Iterables.isEmpty(parentElements))
        {
            return parentElements;
        }

        final Iterable<T> children = Iterables.concat(Iterables.transform(parentElements, getChildFunction));

        return Iterables.concat(parentElements, getDescendants(children, getChildFunction));
    }

    public static <T> Iterable<T> getDescendants(final T parentElement, final Function<T, Iterable<T>> getChildFunction)
    {
        return getDescendants(ImmutableList.of(parentElement), getChildFunction);
    }

    public static boolean removeAll(final Iterable<?> iterable)
    {
        boolean removed = false;
        for (final Iterator<?> iterator = iterable.iterator(); iterator.hasNext();)
        {
            iterator.next();
            iterator.remove();
            removed = true;
        }

        return removed;
    }

    /**
     * @see TcIterators#concurrentModifiable(List)
     */
    public static <T> Iterable<T> concurrentModifiable(final List<T> sequence)
    {
        return new Iterable<T>()
        {
            @Override
            public Iterator<T> iterator()
            {
                return TcIterators.concurrentModifiable(sequence);
            }
        };
    }

    /**
     * Returns the elements from the passed sequence up to the first element
     * that matches the passed predicate (this element is excluded from the
     * result sequence).
     * <p>
     * Returned {@link Iterable} will throw {@link NoSuchElementException} if
     * the predicate does not match for any element from the sequence.
     */
    public static <T> Iterable<T> until(final Iterable<T> elements, final Predicate<T> predicate)
    {
        Preconditions.checkNotNull(elements);
        Preconditions.checkNotNull(predicate);

        return new Iterable<T>()
        {
            @Override
            public Iterator<T> iterator()
            {
                return TcIterators.until(elements.iterator(), predicate);
            }
        };
    }

    /**
     * Returns an {@link Iterable} that contains the elements from the queue.
     * 
     * @see TcIterators#removeAll(BlockingQueue)
     */
    public static <T> Iterable<T> removeAll(final BlockingQueue<T> fromQueue)
    {
        return new Iterable<T>()
        {
            @Override
            public Iterator<T> iterator()
            {
                return TcIterators.removeAll(fromQueue);
            }
        };
    }
}

@@ -958,48 +958,8 @@
 te;%0A
-import com.google.common.base.Supplier;%0A
 impo
@@ -1465,747 +1465,8 @@
  %7D%0A%0A
-    /**%0A     * Build an %7B@link Iterable%7D sequence of element from a %7B@link Iterator%7D%0A     * provided by a %7B@link Supplier%7D.%0A     */%0A    public static %3CT%3E Iterable%3CT%3E newIterable(final Supplier%3CIterator%3CT%3E%3E iteratorSupplier)%0A    %7B%0A        return new IteratorIterable%3CT%3E(iteratorSupplier);%0A    %7D%0A%0A    private static class IteratorIterable%3CT%3E implements Iterable%3CT%3E%0A    %7B%0A        private final Supplier%3CIterator%3CT%3E%3E iteratorSupplier;%0A%0A        public IteratorIterable(final Supplier%3CIterator%3CT%3E%3E iteratorSupplier)%0A        %7B%0A            this.iteratorSupplier = Preconditions.checkNotNull(iteratorSupplier);%0A        %7D%0A%0A        @Override%0A        public Iterator%3CT%3E iterator()%0A        %7B%0A            return iteratorSupplier.get();%0A        %7D%0A    %7D%0A%0A
     

Remove unused methods