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

There is a newer version: 4.8.1
Show newest version
package fj.data;

import fj.*;
import fj.function.Try0;
import fj.function.Try1;
import fj.function.Try2;

import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
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;

/**
 * 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 Java8.Supplier_P1().f(s);
    }

    public static  F, P1> Supplier_P1() {
        return s -> P.lazy(u -> s.get());
    }

    public static  Supplier P1_Supplier(final P1 p) {
        return Java8.P1_Supplier().f(p);
    }

    public static  F, Supplier> P1_Supplier() {
        return (p) -> () -> p._1();
    }

    public static  F Function_F(final Function f) {
        return Java8.Function_F().f(f);
    }

    public static  F, F> Function_F() {
        return f -> a -> f.apply(a);
    }

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

    public static  F, Function> F_Function() {
        return f -> a -> f.f(a);
    }

    public static  F2 BiFunction_F2(final BiFunction f) {
        return Java8.BiFunction_F2().f(f);
    }

    public static  F, F2> BiFunction_F2() {
        return f -> (a, b) -> f.apply(a, b);
    }

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

    public static  F, BiFunction> F2_BiFunction() {
        return f -> (a, b) -> f.f(a, b);
    }

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

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

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

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

    static public  BiFunction> TryCatch2_BiFunction(final Try2 t) {
        return Java8.TryCatch2_BiFunction().f(t);
    }

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

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

    static public  Option Optional_Option(final Optional o) {
        return Java8.Optional_Option().f(o);
    }

    static public  F, Option> Optional_Option() {
        return o -> o.isPresent() ? Option.fromNull(o.get()) : Option.none();
    }

    static public  Optional Option_Optional(final Option o) {
        return Java8.Option_Optional().f(o);
    }

    static public  F, Optional> Option_Optional() {
        return o -> o.isSome() ? Optional.ofNullable(o.some()) : Optional.empty();
    }

    static public  F, F> Consumer_F() {
        return c -> Consumer_F(c);
    }

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

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

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

    static public  java.util.stream.Stream Iterator_JavaStream(Iterator it) {
        return Iterable_JavaStream(() -> it);
    }

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

    static public  Stream JavaStream_Stream(java.util.stream.Stream s) {
        return s.collect(Collectors.toStream());
    }

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy