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

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

/*
 * 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.Throwables;
import com.google.common.collect.Ordering;

import java.io.IOException;
import java.util.PriorityQueue;

/**
 */
public class MergeSequence extends YieldingSequenceBase
{
  private final Ordering ordering;
  private final Sequence> baseSequences;

  public MergeSequence(
      Ordering ordering,
      Sequence> baseSequences
  )
  {
    this.ordering = ordering;
    this.baseSequences = baseSequences;
  }

  @Override
  public  Yielder toYielder(OutType initValue, YieldingAccumulator accumulator)
  {
    PriorityQueue> pQueue = new PriorityQueue<>(
        32,
        ordering.onResultOf(
            new Function, T>()
            {
              @Override
              public T apply(Yielder input)
              {
                return input.get();
              }
            }
        )
    );

    pQueue = baseSequences.accumulate(
        pQueue,
        new Accumulator>, Sequence>()
        {
          @Override
          public PriorityQueue> accumulate(PriorityQueue> queue, Sequence in)
          {
            final Yielder yielder = in.toYielder(
                null,
                new YieldingAccumulator()
                {
                  @Override
                  public T accumulate(T accumulated, T in)
                  {
                    yield();
                    return in;
                  }
                }
            );

            if (!yielder.isDone()) {
              queue.add(yielder);
            } else {
              try {
                yielder.close();
              }
              catch (IOException e) {
                throw Throwables.propagate(e);
              }
            }

            return queue;
          }
        }
    );

    return makeYielder(pQueue, initValue, accumulator);
  }

  private  Yielder makeYielder(
      final PriorityQueue> pQueue,
      OutType initVal,
      final YieldingAccumulator accumulator
  )
  {
    OutType retVal = initVal;
    while (!accumulator.yielded() && !pQueue.isEmpty()) {
      Yielder yielder = pQueue.remove();
      retVal = accumulator.accumulate(retVal, yielder.get());
      yielder = yielder.next(null);
      if (yielder.isDone()) {
        try {
          yielder.close();
        }
        catch (IOException e) {
          throw Throwables.propagate(e);
        }
      } else {
        pQueue.add(yielder);
      }
    }

    if (pQueue.isEmpty() && !accumulator.yielded()) {
      return Yielders.done(retVal, null);
    }

    final OutType yieldVal = retVal;
    return new Yielder()
    {
      @Override
      public OutType get()
      {
        return yieldVal;
      }

      @Override
      public Yielder next(OutType initValue)
      {
        accumulator.reset();
        return makeYielder(pQueue, initValue, accumulator);
      }

      @Override
      public boolean isDone()
      {
        return false;
      }

      @Override
      public void close() throws IOException
      {
        while (!pQueue.isEmpty()) {
          pQueue.remove().close();
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy