com.metamx.common.guava.nary.TrinaryTransformIterator 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.nary;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*/
public class TrinaryTransformIterator implements Iterator
{
public static TrinaryTransformIterator create(
Iterator iterator1,
Iterator iterator2,
Iterator iterator3,
TrinaryFn fn
)
{
return new TrinaryTransformIterator<>(iterator1, iterator2, iterator3, fn);
}
private final Iterator iterator1;
private final Iterator iterator2;
private final Iterator iterator3;
private final TrinaryFn trinaryFn;
public TrinaryTransformIterator(
Iterator iterator1,
Iterator iterator2,
Iterator iterator3,
TrinaryFn trinaryFn
)
{
this.iterator1 = iterator1;
this.iterator2 = iterator2;
this.iterator3 = iterator3;
this.trinaryFn = trinaryFn;
}
@Override
public boolean hasNext()
{
return iterator1.hasNext() || iterator2.hasNext() || iterator3.hasNext();
}
@Override
public RetType next()
{
if (!hasNext()) {
throw new NoSuchElementException();
}
return trinaryFn.apply(
iterator1.hasNext() ? iterator1.next() : null,
iterator2.hasNext() ? iterator2.next() : null,
iterator3.hasNext() ? iterator3.next() : null
);
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}