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

com.github.dakusui.pcond.provider.AssertionProvider Maven / Gradle / Ivy

package com.github.dakusui.pcond.provider;

import com.github.dakusui.pcond.internals.InternalUtils;
import com.github.dakusui.pcond.provider.impls.JUnit4AssertionProvider;

import java.util.Properties;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * An interface of a policy for behaviours on 'contract violations'.
 *
 * @param  The type of exception that should be thrown on an application error.
 */
public interface AssertionProvider {

  /**
   * A constant field that holds the default provider instance.
   */
  AssertionProvider INSTANCE = createAssertionProvider(System.getProperties());

  Configuration configuration();

  /**
   * Returns a provider instance created from a given `Properties` object.
   * This method reads the value for the FQCN of this class (`com.github.dakusui.pcond.provider.AssertionProvider`) and creates an instance of a class specified by the value.
   * If the value is not set, this value instantiates an object of `DefaultAssertionProvider` and returns it.
   *
   * @param properties A {@code Properties} object from which an {@code AssertionProvider} is created
   * @return Created provider instance.
   */
  static AssertionProvider createAssertionProvider(Properties properties) {
    String propertyKeyName = AssertionProvider.class.getCanonicalName();
    if (properties.containsKey(propertyKeyName)) {
      return InternalUtils.createInstanceFromClassName(AssertionProvider.class, properties.getProperty(propertyKeyName), System.getProperties());
    }
    return new JUnit4AssertionProvider(properties);
  }

  /**
   * Checks a value if it is {@code null} or not.
   * If it is not a {@code null}, this method returns the given value itself.
   *
   * @param value The given value.
   * @param    The type of the value.
   * @return The {@code value}.
   */
   T requireNonNull(T value);

  /**
   * Checks a value if it meets a requirement specified by {@code cond}.
   * If it does, the value itself will be returned.
   *
   * @param value The value to be checked.
   * @param cond  The requirement to check the {@code value}.
   * @param    The type of the value.
   * @return The value.
   */
   T requireArgument(T value, Predicate cond);

  /**
   * Checks a value if it meets a requirement specified by {@code cond}.
   * If it does, the value itself will be returned.
   *
   * @param value The value to be checked.
   * @param cond  The requirement to check the {@code value}.
   * @param    The type of the value.
   * @return The value.
   */
   T requireState(T value, Predicate cond);

   T require(T value, Predicate cond) throws E;

   T require(T value, Predicate cond, Function exceptionComposer) throws E;

   T validate(T value, Predicate cond) throws AE;

   T validate(T value, Predicate cond, Function exceptionComposer) throws E;

   T ensureNonNull(T value);

   T ensureState(T value, Predicate cond);

   T ensure(T value, Predicate cond) throws E;

   T ensure(T value, Predicate cond, Function exceptionComposer) throws E;

   void checkInvariant(T value, Predicate cond);

   void checkPrecondition(T value, Predicate cond);

   void checkPostcondition(T value, Predicate cond);

   void assertThat(T value, Predicate cond);

   void assumeThat(T value, Predicate cond);

  interface Configuration {
    default int summarizedStringLength() {
      return 40;
    }

    AE throwApplicationException(String message);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy