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

com.aol.cyclops.functionaljava.comprehenders.ValidationComprehender Maven / Gradle / Ivy

There is a newer version: 9.0.0-MI7
Show newest version
package com.aol.cyclops.functionaljava.comprehenders;

import java.util.function.Function;
import java.util.function.Predicate;

import com.aol.cyclops.types.extensability.Comprehender;
import com.aol.cyclops.types.extensability.ValueComprehender;

import fj.data.Option;
import fj.data.Validation;

public class ValidationComprehender implements ValueComprehender {

    public Object filter(Validation t, Predicate p) {
        return t.filter(x -> p.test(x));
    }

    @Override
    public Object map(Validation t, Function fn) {
        return t.map(i -> fn.apply(i));
    }

    @Override
    public Object flatMap(Validation t, Function fn) {
        return t.bind(i -> fn.apply(i));
    }

    @Override
    public Validation of(Object o) {
        return Validation.success(o);
    }

    @Override
    public Validation empty() {
        return Validation.success(Option.none());
    }

    @Override
    public Class getTargetClass() {
        return Validation.class;
    }

    public Object resolveForCrossTypeFlatMap(Comprehender comp, Validation apply) {
        if (apply.isSuccess())
            return comp.of(apply.success());
        else
            return comp.empty();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy