Java Stream of toStream(final T source)

Here you can find the source of toStream(final T source)

Description

to Stream

License

Open Source License

Declaration

public static final <T> Stream<T> toStream(final T source) 

Method Source Code

//package com.java2s;
/*//from  w  ww  .j  a  v  a 2 s  . com
 * Copyright (c) 2018, The Modern Way. 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.
 */

import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class Main {
    public static final <T> Stream<T> toStream(final T source) {
        return Stream.of(source);
    }

    @SafeVarargs
    public static final <T> Stream<T> toStream(final T... source) {
        if ((null == source) || (source.length == 0)) {
            return Stream.empty();
        }
        if (source.length == 1) {
            return Stream.of(source[0]);
        }
        return Stream.of(source);
    }

    public static final IntStream toStream(final int source) {
        return IntStream.of(source);
    }

    public static final IntStream toStream(final int... source) {
        if ((null == source) || (source.length == 0)) {
            return IntStream.empty();
        }
        if (source.length == 1) {
            return IntStream.of(source[0]);
        }
        return IntStream.of(source);
    }

    public static final LongStream toStream(final long source) {
        return LongStream.of(source);
    }

    public static final LongStream toStream(final long... source) {
        if ((null == source) || (source.length == 0)) {
            return LongStream.empty();
        }
        if (source.length == 1) {
            return LongStream.of(source[0]);
        }
        return LongStream.of(source);
    }

    public static final DoubleStream toStream(final double source) {
        return DoubleStream.of(source);
    }

    public static final DoubleStream toStream(final double... source) {
        if ((null == source) || (source.length == 0)) {
            return DoubleStream.empty();
        }
        if (source.length == 1) {
            return DoubleStream.of(source[0]);
        }
        return DoubleStream.of(source);
    }
}

Related

  1. toParallelStream(Iterator iter)
  2. toStream(Collection collection)
  3. toStream(Enumeration e)
  4. toStream(final Iterable iterable)
  5. toStream(final Iterable iterable)
  6. toStream(Iterable iterable)
  7. toStream(Iterator iter, boolean parallel)
  8. toStream(Optional value)
  9. toStream(T[] array, boolean parallel)