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

fj.data.Java8 Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

The newest version!
package fj.data;

import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;

import fj.F;
import fj.F2;
import fj.P;
import fj.P1;
import fj.Try;
import fj.Unit;
import fj.function.Try0;
import fj.function.Try1;
import fj.function.Try2;

/**
 * Created by mperry on 3/06/2014.
 */
public final class Java8 {

    private Java8() {
        throw new UnsupportedOperationException();
    }

    public static  P1 Supplier_P1(final Supplier s) {
        return P.lazy(s::get);
    }

    public static  F, P1> Supplier_P1() {
        return Java8::Supplier_P1;
    }

    public static  Supplier P1_Supplier(final P1 p) {
        return p::_1;
    }

    public static  F, Supplier> P1_Supplier() {
        return Java8::P1_Supplier;
    }

    public static  F Function_F(final Function f) {
        return f::apply;
    }

    public static  F, F> Function_F() {
        return Java8::Function_F;
    }

    public static  Function F_Function(final F f) {
        return f::f;
    }

    public static  F, Function> F_Function() {
        return Java8::F_Function;
    }

    public static  F2 BiFunction_F2(final BiFunction f) {
        return f::apply;
    }

    public static  F, F2> BiFunction_F2() {
        return Java8::BiFunction_F2;
    }

    public static  BiFunction F2_BiFunction(final F2 f) {
        return f::f;
    }

    public static  F, BiFunction> F2_BiFunction() {
        return Java8::F2_BiFunction;
    }

    public static  Supplier> TryCatch0_Supplier(final Try0 t) {
        return () -> Try.f(t)._1();
    }

    public static  F, Supplier>> TryCatch0_Supplier() {
        return Java8::TryCatch0_Supplier;
    }

    public static  Function> TryCatch1_Function(final Try1 t) {
        return a -> Try.f(t).f(a);
    }

    public static  F, Function>> TryCatch1_Function() {
        return Java8::TryCatch1_Function;
    }

    public static  BiFunction> TryCatch2_BiFunction(final Try2 t) {
        return (a, b) -> Try.f(t).f(a, b);
    }

    public static  F, BiFunction>> TryCatch2_BiFunction() {
        return Java8::TryCatch2_BiFunction;
    }

    public static  java.util.stream.Stream List_JavaStream(final List list) {
        return Iterable_JavaStream(list);
    }

    public static  Option Optional_Option(final Optional o) {
        return o.isPresent() ? Option.some(o.get()) : Option.none();
    }

    public static  F, Option> Optional_Option() {
        return Java8::Optional_Option;
    }

    /**
     * Convert an Option to {@link Optional}. Will throw a {@link NullPointerException} if the Option is some(null).
     */
    public static  Optional Option_Optional(final Option o) {
        return o.option(Optional.empty(), Optional::of);
    }

    public static  F, Optional> Option_Optional() {
        return Java8::Option_Optional;
    }

    public static  F, F> Consumer_F() {
        return Java8::Consumer_F;
    }

    public static  F Consumer_F(final Consumer c) {
        return a -> {
            c.accept(a);
            return Unit.unit();
        };
    }

    public static  java.util.stream.Stream Stream_JavaStream(final fj.data.Stream s) {
        return Iterable_JavaStream(s);
    }

    public static  java.util.stream.Stream Iterable_JavaStream(final Iterable it) {
        return StreamSupport.stream(it.spliterator(), false);
    }

    public static  java.util.stream.Stream Iterator_JavaStream(final Iterator it) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, 0), false);
    }

    public static  F, java.util.stream.Stream> Stream_JavaStream() {
        return Java8::Stream_JavaStream;
    }

    public static  Stream JavaStream_Stream(final java.util.stream.Stream s) {
        return Stream.iteratorStream(s.iterator());
    }

    public static  List JavaStream_List(final java.util.stream.Stream s) {
        return s.collect(Collectors.toList());
    }

    public static  Array JavaStream_Array(final java.util.stream.Stream s) {
        return s.collect(Collectors.toArray());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy