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

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

The 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.collect.Lists;

import java.io.Closeable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executor;

/**
 */
public class Sequences
{

  private static final EmptySequence EMPTY_SEQUENCE = new EmptySequence();

  public static  Sequence simple(final Iterable iterable)
  {
    return BaseSequence.simple(iterable);
  }

  @SuppressWarnings("unchecked")
  public static  Sequence empty()
  {
    return (Sequence) EMPTY_SEQUENCE;
  }

  public static  Sequence concat(Sequence... sequences)
  {
    return concat(Arrays.asList(sequences));
  }

  public static  Sequence concat(Iterable> sequences)
  {
    return concat(Sequences.simple(sequences));
  }

  public static  Sequence concat(Sequence> sequences)
  {
    return new ConcatSequence(sequences);
  }

  public static  Sequence map(Sequence sequence, Function fn)
  {
    return new MappedSequence(sequence, fn);
  }

  public static  Sequence filter(Sequence sequence, Predicate pred)
  {
    return new FilteredSequence(sequence, pred);
  }

  public static  Sequence limit(final Sequence sequence, final int limit)
  {
    return new LimitedSequence(sequence, limit);
  }

  public static  Sequence withBaggage(final Sequence seq, Closeable baggage)
  {
    return new ResourceClosingSequence(seq, baggage);
  }

  public static  Sequence withEffect(final Sequence  seq, final Runnable effect, final Executor exec)
  {
    return new Sequence()
    {
      @Override
      public  OutType accumulate(OutType initValue, Accumulator accumulator)
      {
        final OutType out = seq.accumulate(initValue, accumulator);
        exec.execute(effect);
        return out;
      }

      @Override
      public  Yielder toYielder(OutType initValue, YieldingAccumulator accumulator)
      {
        return new ExecuteWhenDoneYielder(seq.toYielder(initValue, accumulator), effect, exec);
      }
    };
  }

  // This will materialize the entire sequence in memory. Use at your own risk.
  public static  Sequence sort(final Sequence sequence, final Comparator comparator)
  {
    List seqList = Sequences.toList(sequence, Lists.newArrayList());
    Collections.sort(seqList, comparator);
    return BaseSequence.simple(seqList);
  }

  public static > ListType toList(Sequence seq, ListType list)
  {
    return seq.accumulate(list, Accumulators.list());
  }

  private static class EmptySequence implements Sequence
  {
    @Override
    public  OutType accumulate(OutType initValue, Accumulator accumulator)
    {
      return initValue;
    }

    @Override
    public  Yielder toYielder(OutType initValue, YieldingAccumulator accumulator)
    {
      return Yielders.done(initValue, null);
    }
  }
}