/*
 * Copyright (C) 2017. Kryptnostic, Inc (dba Loom)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact the owner of the copyright at support@thedataloom.com
 */

package com.dataloom.streams;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;

/**
 * @author Matthew Tamayo-Rios &lt;matthew@kryptnostic.com&gt;
 */
public class StreamUtil {
    public static <T> Stream<T> stream( Iterable<T> rs ) {
        return StreamSupport.stream( rs.spliterator(), false );
    }

    public static <T> Stream<T> parallelStream( Iterable<T> rs ) {
        return StreamSupport.stream( rs.spliterator(), true );
    }

    /**
     * Useful adapter for {@code Iterables#transform(Iterable, com.google.common.base.Function)} that allows lazy
     * evaluation of result set future. See the same function in AuthorizationUtils as well.
     * 
     * @param rsf The result set future to make a lazy evaluated iterator
     * @return The lazy evaluatable iterable
     */
    public static Iterable<Row> makeLazy( ResultSetFuture rsf ) {
        return getRowsAndFlatten( Stream.of( rsf ) )::iterator;
    }

    public static Stream<Row> getRowsAndFlatten( Stream<ResultSetFuture> stream ) {
        return stream.map( ResultSetFuture::getUninterruptibly )
                .flatMap( StreamUtil::stream );
    }
}

@@ -808,16 +808,209 @@
 reams;%0A%0A
+import com.datastax.driver.core.ResultSetFuture;%0Aimport com.datastax.driver.core.Row;%0Aimport com.google.common.util.concurrent.ListenableFuture;%0Aimport java.util.concurrent.ExecutionException;%0A
 import j
@@ -1076,93 +1076,63 @@
 rt;%0A
-%0A
 import 
-com.datastax.driver.core.ResultSetFuture;%0Aimport com.datastax.driver.core.Row
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/
@@ -1208,16 +1208,22 @@
 %0Apublic 
+final 
 class St
@@ -1233,16 +1233,137 @@
 mUtil %7B%0A
+    private static final Logger logger = LoggerFactory.getLogger( StreamUtil.class );%0A%0A    private StreamUtil() %7B%0A    %7D%0A%0A
     publ
@@ -1842,17 +1842,16 @@
 .%0A     *
- 
 %0A     * 
@@ -2310,10 +2310,285 @@
 ;%0A    %7D%0A
-%7D
+%0A    public static %3CV%3E V safeGet( ListenableFuture%3CV%3E f ) %7B%0A        try %7B%0A            return f.get();%0A        %7D catch ( InterruptedException %7C ExecutionException e ) %7B%0A            logger.error( %22Error retrieving future value.%22, e );%0A            return null;%0A        %7D%0A    %7D%0A%7D%0A
 %0A

Move StreamUtil to Rhizome