io.druid.java.util.common.guava.ConcatSequence Maven / Gradle / Ivy
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you 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 io.druid.java.util.common.guava;
import com.google.common.base.Throwables;
import java.io.Closeable;
import java.io.IOException;
/**
*/
public class ConcatSequence implements Sequence
{
private final Sequence> baseSequences;
public ConcatSequence(
Sequence> baseSequences
)
{
this.baseSequences = baseSequences;
}
@Override
public OutType accumulate(OutType initValue, final Accumulator accumulator)
{
return baseSequences.accumulate(
initValue, new Accumulator>()
{
@Override
public OutType accumulate(OutType accumulated, Sequence in)
{
return in.accumulate(accumulated, accumulator);
}
}
);
}
@Override
public Yielder toYielder(
final OutType initValue,
final YieldingAccumulator accumulator
)
{
Yielder> yielderYielder = baseSequences.toYielder(
null,
new YieldingAccumulator, Sequence>()
{
@Override
public Sequence accumulate(Sequence accumulated, Sequence in)
{
yield();
return in;
}
}
);
try {
return makeYielder(yielderYielder, initValue, accumulator);
}
catch (Throwable t) {
try {
yielderYielder.close();
}
catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
}
public Yielder makeYielder(
Yielder> yielderYielder,
OutType initValue,
YieldingAccumulator accumulator
)
{
while (!yielderYielder.isDone()) {
Yielder yielder = yielderYielder.get().toYielder(initValue, accumulator);
if (accumulator.yielded()) {
return wrapYielder(yielder, yielderYielder, accumulator);
}
initValue = yielder.get();
try {
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
}
yielderYielder = yielderYielder.next(null);
}
return Yielders.done(initValue, yielderYielder);
}
private Yielder wrapYielder(
final Yielder yielder,
final Yielder> yielderYielder,
final YieldingAccumulator accumulator
)
{
if (!accumulator.yielded()) {
OutType nextInit = yielder.get();
try {
yielder.close();
}
catch (IOException e) {
throw Throwables.propagate(e);
}
return makeYielder(yielderYielder.next(null), nextInit, accumulator);
}
return new Yielder()
{
@Override
public OutType get()
{
return yielder.get();
}
@Override
public Yielder next(OutType initValue)
{
return wrapYielder(yielder.next(initValue), yielderYielder, accumulator);
}
@Override
public boolean isDone()
{
return false;
}
@Override
public void close() throws IOException
{
try (Closeable toClose = yielderYielder) {
yielder.close();
}
}
};
}
}