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

com.github.dakusui.valid8j_pcond.validator.MessageComposer Maven / Gradle / Ivy

The newest version!
package com.github.dakusui.valid8j_pcond.validator;

import java.util.function.Predicate;

import static com.github.dakusui.valid8j_pcond.internals.InternalUtils.formatObject;
import static java.lang.String.format;

/**
 * An interface that defines methods to compose a message when a value violates
 * a given condition based on a context.
 */
public interface MessageComposer {
  /**
   * Compose a message string for a `value`, which violates a precondition given as `predicate`.
   *
   * @param value     A value for which a message is created.
   * @param predicate A condition that a given `value` violated.
   * @param        The type of the `value`.
   * @return A composed message string.
   */
   String composeMessageForPrecondition(T value, Predicate predicate);

  /**
   * Compose a message string for a `value`, which violates a postcondition given as `predicate`.
   *
   * @param value     A value for which a message is created.
   * @param predicate A condition that a given `value` violated.
   * @param        The type of the `value`.
   * @return A composed message string.
   */
   String composeMessageForPostcondition(T value, Predicate predicate);

  /**
   * Compose a message string for a `value`, which violates a general condition given as `predicate`.
   * Used for invariant conditions, test assertion (`assertThat`), and test prerequisition
   * checking (`assumeThat`).
   *
   * @param value     A value for which a message is created.
   * @param predicate A condition that a given `value` violated.
   * @param        The type of the `value`.
   * @return A composed message string.
   */
   String composeMessageForAssertion(T value, Predicate predicate);

  /**
   * Compose a message string for a `value`, which violates a user input checking
   * condition given as `predicate`.
   *
   * @param value     A value for which a message is created.
   * @param predicate A condition that a given `value` violated.
   * @param        The type of the `value`.
   * @return A composed message string.
   */
   String composeMessageForValidation(T value, Predicate predicate);

  /**
   * A default implementation of `MessageComposer`.
   */
  class Default implements MessageComposer {
    @Override
    public  String composeMessageForPrecondition(T value, Predicate predicate) {
      return format("value:<%s> violated precondition:value %s", formatObject(value), predicate);
    }

    @Override
    public  String composeMessageForPostcondition(T value, Predicate predicate) {
      return format("value:<%s> violated postcondition:value %s", formatObject(value), predicate);
    }

    @Override
    public  String composeMessageForAssertion(T value, Predicate predicate) {
      return "Value:" + formatObject(value) + " violated: " + predicate.toString();
    }

    @Override
    public  String composeMessageForValidation(T value, Predicate predicate) {
      return "Value:" + formatObject(value) + " violated: " + predicate.toString();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy