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

com.compilit.validation.Subject Maven / Gradle / Ivy

package com.compilit.validation;

import com.compilit.validation.api.Messages;
import com.compilit.validation.api.Rule;
import com.compilit.validation.api.Validatable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

class Subject implements Validatable {

  private final T value;
  private final boolean hasDualInput;
  private final List> ruleDefinitions = new ArrayList<>();
  private final List> dualInputRuleDefinitions = new ArrayList<>();
  private final List> intermediateActions = new ArrayList<>();

  private Object argument;
  private String message = Messages.DEFAULT_MESSAGE;

  Subject(Rule ruleDefinition, final T value) {
    this.ruleDefinitions.add(ruleDefinition);
    this.value = value;
    this.argument = null;
    this.hasDualInput = false;
  }

  Subject(final Rule.WithDualInput dualInputRuleDefinitions, final T value) {
    this.dualInputRuleDefinitions.add(dualInputRuleDefinitions);
    this.value = value;
    this.hasDualInput = true;
  }

  Subject(final Collection> ruleDefinitions, final T value) {
    this.ruleDefinitions.addAll(ruleDefinitions);
    this.value = value;
    this.hasDualInput = false;
  }

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

  @Override
  public boolean validate() {
    final var stringBuilder = new StringBuilder();
    var isValid = true;
    if (hasDualInput) {
      isValid = validateDualInputRules(stringBuilder);
    } else {
      isValid = validateRules(stringBuilder);
    }
    if (!isValid) {
      message = stringBuilder.toString();
    }
    return isValid;
  }

  public T getValue() {
    return value;
  }

  public void addRule(Rule rule) {
    ruleDefinitions.add(rule);
  }

  public void addDualInputRule(Rule.WithDualInput rule) {
    dualInputRuleDefinitions.add(rule);
  }

  public void addIntermediateAction(Consumer consumer) {
    intermediateActions.add(consumer);
  }

  public void setArgument(Object argument) {
    if (this.argument == null) {
      this.argument = argument;
    }
  }

  public void processIntermediateActions() {
    intermediateActions.forEach(x -> x.accept(getValue()));
  }

  private boolean validateRules(StringBuilder stringBuilder) {
    var isValid = true;
    for (final var ruleDefinition : ruleDefinitions) {
      if (!ruleDefinition.test(value)) {
        stringBuilder.append(Messages.BROKEN_RULE_PREFIX).append(ruleDefinition.getMessage()).append("\n");
        isValid = false;
      }
    }
    return isValid;
  }

  private boolean validateDualInputRules(StringBuilder stringBuilder) {
    var isValid = true;
    for (final var biRuleDefinition : dualInputRuleDefinitions) {
      if (!biRuleDefinition.test(value, argument)) {
        stringBuilder.append(Messages.BROKEN_RULE_PREFIX)
                     .append(biRuleDefinition.getMessage())
                     .append("\n");
        isValid = false;
      }
    }
    return isValid;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy