org.paumard.spliterators.CyclingSpliterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of streams-utils Show documentation
Show all versions of streams-utils Show documentation
Streams Utils is a set of operations written on Java 8 Streams. It allows several basic operations that
are not available in the the Java 8, and that have proven to be very useful in some cases.
/*
* Copyright (C) 2015 José Paumard
*
* 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 org.paumard.spliterators;
import java.util.List;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.stream.Collectors.toList;
/**
* Created by José
*/
public class CyclingSpliterator implements Spliterator> {
private final List list;
public static CyclingSpliterator of(Spliterator spliterator) {
Objects.requireNonNull(spliterator);
return new CyclingSpliterator<>(spliterator);
}
private CyclingSpliterator(List list) {
this.list = list;
}
private CyclingSpliterator(Spliterator spliterator) {
this.list = StreamSupport.stream(spliterator, false).collect(toList());
}
@Override
public boolean tryAdvance(Consumer super Stream> action) {
action.accept(list.stream());
return true;
}
@Override
public Spliterator> trySplit() {
return new CyclingSpliterator<>(list);
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return Spliterator.ORDERED;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy