All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.runtime.core.internal.stream.StreamUtil Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

There is a newer version: 3.1.18
Show newest version
/**
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * 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.
 */
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.speedment.runtime.core.internal.stream;

import com.speedment.runtime.core.db.SqlFunction;
import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.core.stream.parallel.ParallelStrategy;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static com.speedment.runtime.core.util.StaticClassUtil.instanceNotAllowed;
import static java.util.Objects.requireNonNull;

/**
 *
 * @author Emil Forslund
 */
public final class StreamUtil {
    
    public static  Stream streamOfOptional(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional element) {
        return Stream.of(element.orElse(null)).filter(e -> e != null);
    }

    public static  Stream streamOfNullable(T element) {
        // Needless to say, element is nullable...
        if (element == null) {
            return Stream.empty();
        } else {
            return Stream.of(element);
        }
    }

    public static  Stream asStream(Iterator iterator) {
        requireNonNull(iterator);
        return asStream(iterator, false);
    }

    public static  Stream asStream(Iterator iterator, boolean parallel) {
        requireNonNull(iterator);
        final Iterable iterable = () -> iterator;
        return StreamSupport.stream(iterable.spliterator(), parallel);
    }

    public static  Stream asStream(ResultSet resultSet, SqlFunction mapper) {
//        requireNonNull(resultSet);
//        requireNonNull(mapper);
//        final Iterator iterator = new ResultSetIterator<>(resultSet, mapper);
//        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.IMMUTABLE + Spliterator.NONNULL), false);
         return asStream(resultSet, mapper, ParallelStrategy.computeIntensityDefault());
    }
    
    public static  Stream asStream(ResultSet resultSet, SqlFunction mapper, ParallelStrategy parallelStrategy) {
        requireNonNull(resultSet);
        requireNonNull(mapper);
        final Iterator iterator = new ResultSetIterator<>(resultSet, mapper);
        return StreamSupport.stream(parallelStrategy.spliteratorUnknownSize(iterator, Spliterator.IMMUTABLE + Spliterator.NONNULL), false);
    }

    public static  Stream from(@SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional optional) {
        requireNonNull(optional);
        return optional.isPresent() ? Stream.of(optional.get()) : Stream.empty();
    }

    private static class ResultSetIterator implements Iterator {

        private final ResultSet resultSet;
        private final SqlFunction mapper;

        public ResultSetIterator(final ResultSet resultSet, final SqlFunction mapper) {
            this.resultSet = requireNonNull(resultSet);
            this.mapper = requireNonNull(mapper);
        }

        @Override
        public boolean hasNext() {
            try {
                return resultSet.next();
            } catch (SQLException sqle) {
                throw new SpeedmentException("Error iterating over a ResultSet", sqle);
            }
        }

        @Override
        public T next() {
            try {
                return mapper.apply(resultSet);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }

    }

    /**
     * Utility classes should not be instantiated.
     */
    private StreamUtil() {
        instanceNotAllowed(getClass());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy