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

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

package com.compilit.validation;

import com.compilit.validation.api.ValidatingRuleBuilder;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ValidatingRuleDefinitionBuilder implements ValidatingRuleBuilder {

  private final T value;
  private final Predicate predicate;
  private final Logger logger = LoggerFactory.getLogger(Verifications.class);

  ValidatingRuleDefinitionBuilder(final T value, final Predicate predicate) {
    this.value = value;
    this.predicate = predicate;
  }

  @Override
  public boolean otherwiseReport(final String message, final Object... formatArguments) {
    final var actualMessage = String.format(message, formatArguments);
    var isValid = predicate.test(value);
    if (!isValid) {
      logger.error(actualMessage);
    }
    return isValid;
  }

  static final class WithDualInput implements ValidatingRuleBuilder {

    private final T value;
    private final Object argument;
    private final BiPredicate predicate;
    private final Logger logger = LoggerFactory.getLogger(Verifications.class);

    WithDualInput(final T value, Object argument, final BiPredicate predicate) {
      this.value = value;
      this.argument = argument;
      this.predicate = predicate;
    }

    @Override
    public boolean otherwiseReport(final String message, final Object... formatArguments) {
      final var actualMessage = String.format(message, formatArguments);
      var isValid = predicate.test(value, argument);
      if (!isValid) {
        logger.error(actualMessage);
      }
      return isValid;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy