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

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

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Created by fbelov on 31.05.16.
 */
@EqualsAndHashCode
@ToString
@JsonSerialize(using = AnswerOrErrors.Serializer.class)
public class AnswerOrErrors {

    private T answer;
    private Errors errors;

    private AnswerOrErrors(T answer) {
        this.answer = answer;
    }

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

    private AnswerOrErrors(T answer, Errors errors) {
        this.answer = answer;
        this.errors = errors;
    }

    public AnswerOrErrors ifOk(Consumer> consumer) {
        if (consumer != null && isOk()) {
            consumer.accept(this);
        }

        return this;
    }

    public AnswerOrErrors ifOk(Runnable runnable) {
        if (runnable != null && isOk()) {
            runnable.run();
        }

        return this;
    }

    public AnswerOrErrors ifOk(Supplier> supplier) {
        if (supplier != null && isOk()) {
            return supplier.get();
        } else {
            return this;
        }
    }

    public AnswerOrErrors ifOk(Function, AnswerOrErrors> func) {
        if (func != null && isOk()) {
            return func.apply(this);
        } else {
            return this;
        }
    }

    public AnswerOrErrors ifHasErrors(Consumer> consumer) {
        if (consumer != null && hasErrors()) {
            consumer.accept(this);
        }

        return this;
    }

    public AnswerOrErrors ifHasErrors(Runnable runnable) {
        if (runnable != null && hasErrors()) {
            runnable.run();
        }

        return this;
    }

    public AnswerOrErrors ifHasErrors(Supplier> supplier) {
        if (supplier != null && hasErrors()) {
            return supplier.get();
        } else {
            return this;
        }
    }

    public AnswerOrErrors ifHasErrors(Function, AnswerOrErrors> func) {
        if (func != null && hasErrors()) {
            return func.apply(this);
        } else {
            return this;
        }
    }

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

    public boolean hasErrors() {
        return errors != null && errors.hasErrors();
    }

    @Nullable
    public T getAnswer() {
        return answer;
    }

    @Nullable
    public Errors getErrors() {
        return errors;
    }

    public static  AnswerOrErrors ok() {
        return new AnswerOrErrors<>((T) null);
    }

    public static  AnswerOrErrors ok(T object) {
        return new AnswerOrErrors<>(object);
    }

    public static  AnswerOrErrors failure(T object, Errors errors) {
        return new AnswerOrErrors<>(object, errors);
    }

    public static  AnswerOrErrors failure(T object, Err... errors) {
        if (object instanceof Err) {
            return new AnswerOrErrors<>(null, Errors.reject((Err) object));
        } else {
            return new AnswerOrErrors<>(object, Errors.reject(errors));
        }
    }

    public static  AnswerOrErrors failure(Errors errors) {
        return new AnswerOrErrors<>(errors);
    }

    public static  AnswerOrErrors failure(Err... errors) {
        return new AnswerOrErrors<>(Errors.reject(errors));
    }

    protected static class Serializer extends JsonSerializer {

        @Override
        public void serialize(AnswerOrErrors value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
            Map a = new HashMap();

            a.put("answer", value.getAnswer());
            a.put("ok", value.isOk());

            if (!value.hasErrors()) {
                a.put("errors", null);
            } else {
                a.put("errors", value.errors.get());
            }

            jgen.writeObject(a);
        }

    }

    @Getter
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Deserializer {

        private T answer;
        private List errors;

        public AnswerOrErrors toAnswerOrErrors() {
            if (errors != null && errors.size() > 0) {
                return new AnswerOrErrors(answer, Errors.reject(errors));
            } else {
                return AnswerOrErrors.ok(answer);
            }
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy