org.eclipse.edc.util.stream.PartitionIterator Maven / Gradle / Ivy
/*
* Copyright (c) 2022 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*
*/
package org.eclipse.edc.util.stream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;
/**
* Partitions a source collection into units.
*/
public class PartitionIterator implements Iterator> {
private final Iterator source;
private final int partitionSize;
public static Stream> streamOf(Stream source, int partitionSize) {
var iterator = new PartitionIterator<>(source.iterator(), partitionSize);
return stream(spliteratorUnknownSize(iterator, ORDERED), false);
}
public PartitionIterator(Iterator source, int partitionSize) {
this.source = Objects.requireNonNull(source);
if (partitionSize <= 0) {
throw new IllegalArgumentException("Invalid partition size:" + partitionSize);
}
this.partitionSize = partitionSize;
}
@Override
public boolean hasNext() {
return source.hasNext();
}
@Override
public List next() {
List partition = new ArrayList<>(partitionSize);
while (partition.size() < partitionSize && source.hasNext()) {
partition.add(source.next());
}
return partition;
}
}