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

io.thedocs.soyuz.err.Errors Maven / Gradle / Ivy

There is a newer version: 3.03
Show newest version
package io.thedocs.soyuz.err;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by fbelov on 31.05.16.
 */
@EqualsAndHashCode
@ToString
public class Errors implements Iterable {

    private List errors;

    private Errors(List errors) {
        this.errors = errors;
    }

    public Errors add(Errors errors) {
        if (errors != null) add(errors.get());

        return this;
    }

    public Errors add(Err... errors) {
        if (errors != null) add(Arrays.asList(errors));

        return this;
    }

    public Errors add(Collection errors) {
        if (errors != null) this.errors.addAll(errors);

        return this;
    }

    @JsonIgnore
    public boolean isOk() {
        return !hasErrors();
    }

    public boolean hasErrors() {
        return !errors.isEmpty();
    }

    public List get() {
        return errors;
    }

    @JsonProperty("global")
    public List getGlobalErrors() {
        return errors.stream().filter(Err::isGlobalScope).collect(Collectors.toList());
    }

    @JsonProperty("field")
    public List getFieldErrors() {
        return errors.stream().filter(Err::isFieldScope).collect(Collectors.toList());
    }

    public static Errors ok() {
        return new Errors(new ArrayList<>());
    }

    public static Errors reject(Err... errors) {
        List answer = new ArrayList<>(errors.length);
        Collections.addAll(answer, errors);
        return new Errors(answer);
    }

    public static Errors reject(Collection errors) {
        List answer = new ArrayList<>();
        answer.addAll(errors);
        return new Errors(answer);
    }

    @Override
    public Iterator iterator() {
        return errors.iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy