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

io.toolisticon.aptk.tools.wrapper.ElementWrapperValidator Maven / Gradle / Ivy

The newest version!
package io.toolisticon.aptk.tools.wrapper;

import io.toolisticon.aptk.tools.MessagerUtils;
import io.toolisticon.aptk.tools.corematcher.ValidationMessage;

import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class ElementWrapperValidator> {

    static class MessageConfig {
        Diagnostic.Kind kind = Diagnostic.Kind.ERROR;
        ValidationMessage customMessage = new DummyValidationMessage(null, "Validation FAILED!");
        Object[] customMessageVars = null;
    }

    public ElementWrapperValidator(WRAPPER wrapper) {
        this.wrapper = wrapper;
    }

    private final WRAPPER wrapper;

    private final MessageConfig messageConfig = new MessageConfig();

    private final List> checks = new ArrayList<>();

    public ElementWrapperValidatorInterface.FirstValidation start() {
        return new FirstValidationImpl();
    }

    private void addCustomMessage(ValidationMessage message, Object... vars) {
        messageConfig.customMessage = message;
        messageConfig.customMessageVars = vars;
    }

    private void addCheck(Predicate predicate) {
        checks.add(predicate);
    }

    static class DummyValidationMessage implements ValidationMessage {

        private final String code;
        private final String message;

        public DummyValidationMessage(String code, String message) {
            this.code = code;
            this.message = message;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getMessage() {
            return message;
        }
    }

    class FirstValidationImpl implements ElementWrapperValidatorInterface.FirstValidation {

        public ElementWrapperValidatorInterface.FirstValidationWithScope asInfo() {
            messageConfig.kind = Diagnostic.Kind.NOTE;
            return new FirstValidationWithScopeImpl();
        }

        public ElementWrapperValidatorInterface.FirstValidationWithScope asWarning() {
            messageConfig.kind = Diagnostic.Kind.WARNING;
            return new FirstValidationWithScopeImpl();
        }

        public ElementWrapperValidatorInterface.FirstValidationWithScope asMandatoryWarning() {
            messageConfig.kind = Diagnostic.Kind.MANDATORY_WARNING;
            return new FirstValidationWithScopeImpl();
        }

        public ElementWrapperValidatorInterface.FirstValidationWithScope asError() {
            messageConfig.kind = Diagnostic.Kind.ERROR;
            return new FirstValidationWithScopeImpl();
        }


        public ElementWrapperValidatorInterface.FirstValidationWithScopeAndCustomMessage withCustomMessage(ValidationMessage message, Object... vars) {
            addCustomMessage(message, vars);
            return new FirstValidationWithScopeAndCustomMessageImpl();
        }

        public ElementWrapperValidatorInterface.FirstValidationWithScopeAndCustomMessage withCustomMessage(String message, Object... vars) {
            addCustomMessage(new DummyValidationMessage(null, message), vars);
            return new FirstValidationWithScopeAndCustomMessageImpl();
        }

        public ElementWrapperValidatorInterface.FollowUpValidationOrEndValidation check(Predicate predicate) {
            addCheck(predicate);
            return new FollowUpValidationOrEndValidationImpl();
        }

    }

    class FirstValidationWithScopeImpl implements ElementWrapperValidatorInterface.FirstValidationWithScope {

        public ElementWrapperValidatorInterface.FirstValidationWithScopeAndCustomMessage withCustomMessage(ValidationMessage message, Object... vars) {
            addCustomMessage(message, vars);
            return new FirstValidationWithScopeAndCustomMessageImpl();
        }

        public ElementWrapperValidatorInterface.FirstValidationWithScopeAndCustomMessage withCustomMessage(String message, Object... vars) {
            addCustomMessage(new DummyValidationMessage(null, message), vars);
            return new FirstValidationWithScopeAndCustomMessageImpl();
        }

        public ElementWrapperValidatorInterface.FollowUpValidationOrEndValidation check(Predicate predicate) {
            addCheck(predicate);
            return new FollowUpValidationOrEndValidationImpl();
        }

    }

    class FirstValidationWithScopeAndCustomMessageImpl implements ElementWrapperValidatorInterface.FirstValidationWithScopeAndCustomMessage {

        public ElementWrapperValidatorInterface.FollowUpValidationOrEndValidation check(Predicate predicate) {
            addCheck(predicate);
            return new FollowUpValidationOrEndValidationImpl();
        }

    }

    class FollowUpValidationOrEndValidationImpl implements ElementWrapperValidatorInterface.FollowUpValidationOrEndValidation {

        public ElementWrapperValidatorInterface.FollowUpValidationOrEndValidation and(Predicate predicate) {
            addCheck(predicate);
            return new FollowUpValidationOrEndValidationImpl();
        }

        public boolean validate() {
            boolean result = true;
            for (Predicate check : checks) {
                result = result & check.test(wrapper);
            }

            return result;
        }

        public boolean validateAndIssueMessages() {
            boolean result = validate();
            if (!result) {
                MessagerUtils.printMessage(wrapper.element, Diagnostic.Kind.ERROR, messageConfig.customMessage, messageConfig.customMessageVars);
            }
            return result;
        }
    }

    public static > ElementWrapperValidatorInterface.FirstValidation startValidation(WRAPPER wrapper) {
        return new ElementWrapperValidator<>(wrapper).start();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy