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

com.github.dakusui.floorplan.utils.Checks Maven / Gradle / Ivy

Go to download

A library to model a heterogeneous and distributed software system for testing

There is a newer version: 5.1.1
Show newest version
package com.github.dakusui.floorplan.utils;

import com.github.dakusui.floorplan.exception.Exceptions;

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

import static java.lang.String.format;

public enum Checks {
  ;

  public static  T requireArgument(T value, Predicate condition) {
    return requireArgument(value, condition, v -> format("'%s' did not not satisfy <%s>", v, condition));
  }

  public static  T requireArgument(T value, Predicate condition, Function messageComposer) {
    return requireArgument(value, condition, () -> messageComposer.apply(value));
  }

  public static  T requireArgument(T value, Predicate condition, Supplier messageComposer) {
    if (condition.test(value))
      return value;
    throw Exceptions.throwExceptionForIllegalValue(messageComposer.get());
  }

  public static  T requireState(T value, Predicate condition) {
    return requireState(value, condition, v -> format("'%s' did not not satisfy <%s>", v, condition));
  }

  public static  T requireState(T value, Predicate condition, Function messageComposer) {
    if (condition.test(value))
      return value;
    throw Exceptions.throwExceptionForIllegalState(messageComposer.apply(value));
  }

  public static  T require(T value, Predicate condition, Supplier exceptionThrower) {
    return require(value, condition, d -> exceptionThrower);
  }

  public static  T require(T value, Predicate condition, Function> exceptionThrower) {
    if (condition.test(value))
      return value;
    throw exceptionThrower.apply(value).get();
  }

  public static  T requireNonNull(T value) {
    return requireNonNull(value, () -> null);
  }

  public static  T requireNonNull(T value, String message) {
    return requireNonNull(value, () -> message);
  }

  public static  T requireNonNull(T value, Supplier messageSupplier) {
    if (value != null)
      return value;
    throw Exceptions.throwExceptionForNullValue(messageSupplier.get());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy