All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metamx.common.guava.FunctionalIterable Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/*
 * Copyright 2011,2012 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metamx.common.guava;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.metamx.common.guava.nary.BinaryFn;
import com.metamx.common.guava.nary.BinaryTransformIterable;
import com.metamx.common.guava.nary.TrinaryFn;
import com.metamx.common.guava.nary.TrinaryTransformIterable;

import java.util.Iterator;

/**
 */
public class FunctionalIterable implements Iterable
{
  private final Iterable delegate;

  public static  FunctionalIterable create(Iterable delegate)
  {
    return new FunctionalIterable<>(delegate);
  }

  public static  FunctionalIterable fromConcatenation(Iterable... delegates)
  {
    return new FunctionalIterable<>(Iterables.concat(delegates));
  }

  public static  FunctionalIterable fromConcatenation(Iterable> delegates)
  {
    return new FunctionalIterable<>(Iterables.concat(delegates));
  }

  public FunctionalIterable(
      Iterable delegate
  )
  {
    this.delegate = delegate;
  }

  public Iterator iterator()
  {
    return delegate.iterator();
  }

  public  FunctionalIterable transform(Function fn)
  {
    return new FunctionalIterable<>(Iterables.transform(delegate, fn));
  }

  public  FunctionalIterable transformCat(Function> fn)
  {
    return new FunctionalIterable<>(Iterables.concat(Iterables.transform(delegate, fn)));
  }

  public  FunctionalIterable keep(Function fn)
  {
    return new FunctionalIterable<>(Iterables.filter(Iterables.transform(delegate, fn), Predicates.notNull()));
  }

  public FunctionalIterable filter(Predicate pred)
  {
    return new FunctionalIterable<>(Iterables.filter(delegate, pred));
  }

  public FunctionalIterable drop(int numToDrop)
  {
    return new FunctionalIterable<>(new DroppingIterable<>(delegate, numToDrop));
  }

  public FunctionalIterable limit(int limit)
  {
    return new FunctionalIterable<>(Iterables.limit(delegate, limit));
  }

  public FunctionalIterable concat(Iterable... toConcat)
  {
    if (toConcat.length == 1) {
      return new FunctionalIterable<>(Iterables.concat(delegate, toConcat[0]));
    }
    return new FunctionalIterable<>(Iterables.concat(delegate, Iterables.concat(toConcat)));
  }

  public FunctionalIterable concat(Iterable> toConcat)
  {
    return new FunctionalIterable<>(Iterables.concat(delegate, Iterables.concat(toConcat)));
  }

  public  FunctionalIterable binaryTransform(
      final Iterable otherIterable, final BinaryFn binaryFn
  )
  {
    return new FunctionalIterable<>(BinaryTransformIterable.create(delegate, otherIterable, binaryFn));
  }

  public  FunctionalIterable trinaryTransform(
      final Iterable iterable1,
      final Iterable iterable2,
      final TrinaryFn trinaryFn
  )
  {
    return new FunctionalIterable<>(TrinaryTransformIterable.create(delegate, iterable1, iterable2, trinaryFn));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy