com.metamx.common.guava.ConcatSequence 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.Throwables;
import com.google.common.io.Closeables;
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 (RuntimeException e) {
// We caught a RuntimeException instead of returning a really, real, live, real boy, errr, iterator
// So we better try to close our stuff, 'cause the exception is what is making it out of here.
Closeables.closeQuietly(yielderYielder);
throw e;
}
}
public Yielder makeYielder(
Yielder> yielderYielder,
OutType initValue,
YieldingAccumulator accumulator
)
{
if (yielderYielder.isDone()) {
return Yielders.done(initValue, yielderYielder);
}
while (!yielderYielder.isDone()) {
Yielder yielder = yielderYielder.get().toYielder(initValue, accumulator);
if (accumulator.yielded()) {
return wrapYielder(yielder, yielderYielder.next(null), 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, 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
{
yielder.close();
yielderYielder.close();
}
};
}
}