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

com.github.vickumar1981.svalidate.util.Validation Maven / Gradle / Ivy

The newest version!
package com.github.vickumar1981.svalidate.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;;

public class Validation {

    private ArrayList exceptions;

    private Validation(ArrayList errors) {
        this.exceptions = errors;
    }

    public static  Validation of(Validation ...validations) {
        ArrayList errs = new ArrayList<>();
        for (Validation v: validations) {
            errs.addAll(v.errors());
        }
        return new Validation<>(errs);
    }

    public List errors() {
        return exceptions;
    }

    public Validation append(Validation other) {
        this.exceptions.addAll(other.errors());
        return this;
    }

    public Validation andThen(Boolean cond, Supplier> other) {
        if (cond) {
            Validation  otherValidation = other.get();
            append(otherValidation);
        }
        return this;
    }

    public Validation orElse(Boolean cond, Supplier> other) {
        if (!cond) {
            Validation  otherValidation = other.get();
            append(otherValidation);
        }
        return this;
    }

    public boolean isSuccess() {
        return exceptions.isEmpty();
    }

    public boolean isFailure() {
        return !isSuccess();
    }

    public static  Validation fail(A ...value) {
        ArrayList errors = new ArrayList<>(Arrays.asList(value));
        return new Validation<>(errors);
    }

    public static  Validation success() {
        return new Validation<>(new ArrayList<>());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy