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

fj.test.Bool Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

The newest version!
package fj.test;

import fj.F0;

import static fj.test.Property.prop;

/**
 * A boolean wrapper that works well with properties.
 *
 * @version %build.number%
 */
public final class Bool {
  private final boolean b;

  private static final Bool t = new Bool(true);
  private static final Bool f = new Bool(false);

  private Bool(final boolean b) {
    this.b = b;
  }

  /**
   * Returns true if this value is true, false otherwise.
   *
   * @return true if this value is true, false otherwise.
   */
  public boolean is() {
    return b;
  }

  /**
   * Returns false if this value is true, true otherwise.
   *
   * @return false if this value is true, true otherwise.
   */
  public boolean isNot() {
    return !b;
  }

  /**
   * Returns a property that produces a result only if this value is true. The result will be taken
   * from the given property.
   *
   * @param p The property to return if this value is true.
   * @return a property that produces a result only if this value is true.
   */
  public Property implies(final F0 p) {
    return Property.implies(b, p);
  }

  /**
   * Returns a property that produces a result only if this value is true. The result will be taken
   * from the given property.
   *
   * @param p The property to return if this value is true.
   * @return a property that produces a result only if this value is true.
   */
  public Property implies(final Property p) {
    return Property.implies(b, () -> p);
  }

  /**
   * Returns a property that produces a result only if this value is true.
   *
   * @param c The value to construct a property with to return if this value is true.
   * @return a property that produces a result only if this value is true.
   */
  public Property implies(final Bool c) {
    return implies(prop(c.b));
  }

  /**
   * Returns a property that produces a result only if this value is true.
   *
   * @param c The value to construct a property with to return if this value is true.
   * @return a property that produces a result only if this value is true.
   */
  public Property implies(final boolean c) {
    return Property.implies(b, () -> prop(c));
  }

  /**
   * Construct a Bool from the given value.
   *
   * @param b The value to construct a Bool with.
   * @return A Bool from the given value.
   */
  public static Bool bool(final boolean b) {
    return b ? t : f;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy