com.datumbox.framework.common.concurrency.StreamMethods Maven / Gradle / Ivy
/**
* Copyright (C) 2013-2016 Vasilis Vryniotis
*
* 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.datumbox.framework.common.concurrency;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* This class contains a number of helper methods for Java 8 streams.
*
* @author Vasilis Vryniotis
*/
public class StreamMethods {
/**
* Converts an spliterator to a stream.
*
* @param
* @param spliterator
* @param parallel
* @return
*/
public static Stream stream(Spliterator spliterator, boolean parallel) {
return StreamSupport.stream(spliterator, parallel);
}
/**
* Converts an iterable to a stream.
*
* @param
* @param iterable
* @param parallel
* @return
*/
public static Stream stream(Iterable iterable, boolean parallel) {
return StreamSupport.stream(iterable.spliterator(), parallel);
}
/**
* Converts an Stream to parallel or sequential .
*
* @param
* @param stream
* @param parallel
* @return
*/
public static Stream stream(Stream stream, boolean parallel) {
if(parallel) {
return stream.parallel();
}
else {
return stream.sequential();
}
}
/**
* Converts an iterator to a stream.
*
* @param
* @param iterator
* @param parallel
* @return
*/
public static Stream stream(Iterator iterator, boolean parallel) {
Iterable iterable = () -> iterator;
return StreamSupport.stream(iterable.spliterator(), parallel);
}
/**
* Takes a stream and enumerates it (similar to Python's enumerate()) to return
* the item along with its position.
*
* @param
* @param stream
* @return
*/
public static Stream> enumerate(Stream stream) {
Iterator> iterator = new Iterator>() {
private int counter = 0;
private final Iterator internalIterator = stream.iterator();
/** {@inheritDoc} */
@Override
public boolean hasNext() {
return internalIterator.hasNext();
}
/** {@inheritDoc} */
@Override
public Map.Entry next() {
return new AbstractMap.SimpleImmutableEntry<>(counter++, internalIterator.next());
}
/** {@inheritDoc} */
@Override
public void remove() {
throw new UnsupportedOperationException("This is a read-only iterator, remove operation is not supported.");
}
};
return stream(iterator, stream.isParallel());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy