001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.modeshape.utils;
017
018import static java.util.Spliterator.IMMUTABLE;
019import static java.util.Spliterators.spliteratorUnknownSize;
020import static java.util.stream.StreamSupport.stream;
021
022import java.util.Iterator;
023import java.util.stream.Stream;
024
025/**
026 * @author acoburn
027 * @since February 10, 2016
028 */
029public class StreamUtils {
030
031    /**
032     * Convert an Iterator to a Stream
033     *
034     * @param iterator the iterator
035     * @param <T> the type of the Stream
036     * @return the stream
037     */
038    public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator) {
039        return iteratorToStream(iterator, false);
040    }
041
042    /**
043     * Convert an Iterator to a Stream
044     *
045     * @param <T> the type of the Stream
046     * @param iterator the iterator
047     * @param parallel whether to parallelize the stream
048     * @return the stream
049     */
050    public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator, final Boolean parallel) {
051        return stream(spliteratorUnknownSize(iterator, IMMUTABLE), parallel);
052    }
053    private StreamUtils() {
054        // prevent instantiation
055    }
056}
057