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

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

package com.compilit.validation;

import com.compilit.validation.api.Rule;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

final class RuleDefinition implements Rule {

  private final Predicate predicate;
  private final String message;

  RuleDefinition(final Predicate predicate, final String message) {
    this.predicate = predicate;
    this.message = message;
  }

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

  @Override
  public boolean test(final T value) {
    try {
      return predicate.test(value);
    } catch (Exception ignored) {
      return false;
    }
  }

  static final class WithDualInput implements Rule.WithDualInput {

    private final BiPredicate predicate;
    private final String message;

    WithDualInput(final BiPredicate predicate, final String message) {
      this.predicate = predicate;
      this.message = message;
    }

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

    @Override
    public boolean test(final T value, final Object argument) {
      return predicate.test(value, argument);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy