001    package fj.test;
002    
003    import fj.P1;
004    import static fj.test.Property.prop;
005    
006    /**
007     * A boolean wrapper that works well with properties.
008     *
009     * @version %build.number%<br>
010     *          <ul>
011     *          <li>$LastChangedRevision: 5 $</li>
012     *          <li>$LastChangedDate: 2008-12-06 16:49:43 +1000 (Sat, 06 Dec 2008) $</li>
013     *          <li>$LastChangedBy: tonymorris $</li>
014     *          </ul>
015     */
016    public final class Bool {
017      private final boolean b;
018    
019      private static final Bool t = new Bool(true);
020      private static final Bool f = new Bool(false);
021    
022      private Bool(final boolean b) {
023        this.b = b;
024      }
025    
026      /**
027       * Returns <code>true</code> if this value is true, <code>false</code> otherwise.
028       *
029       * @return <code>true</code> if this value is true, <code>false</code> otherwise.
030       */
031      public boolean is() {
032        return b;
033      }
034    
035      /**
036       * Returns <code>false</code> if this value is true, <code>true</code> otherwise.
037       *
038       * @return <code>false</code> if this value is true, <code>true</code> otherwise.
039       */
040      public boolean isNot() {
041        return !b;
042      }
043    
044      /**
045       * Returns a property that produces a result only if this value is true. The result will be taken
046       * from the given property.
047       *
048       * @param p The property to return if this value is true.
049       * @return a property that produces a result only if this value is true.
050       */
051      public Property implies(final P1<Property> p) {
052        return Property.implies(b, p);
053      }
054    
055      /**
056       * Returns a property that produces a result only if this value is true. The result will be taken
057       * from the given property.
058       *
059       * @param p The property to return if this value is true.
060       * @return a property that produces a result only if this value is true.
061       */
062      public Property implies(final Property p) {
063        return Property.implies(b, new P1<Property>() {
064          public Property _1() {
065            return p;
066          }
067        });
068      }
069    
070      /**
071       * Returns a property that produces a result only if this value is true.
072       *
073       * @param c The value to construct a property with to return if this value is true.
074       * @return a property that produces a result only if this value is true.
075       */
076      public Property implies(final Bool c) {
077        return implies(prop(c.b));
078      }
079    
080      /**
081       * Returns a property that produces a result only if this value is true.
082       *
083       * @param c The value to construct a property with to return if this value is true.
084       * @return a property that produces a result only if this value is true.
085       */
086      public Property implies(final boolean c) {
087        return Property.implies(b, new P1<Property>() {
088          public Property _1() {
089            return prop(c);
090          }
091        });
092      }
093    
094      /**
095       * Construct a <code>Bool</code> from the given value.
096       *
097       * @param b The value to construct a <code>Bool</code> with.
098       * @return A <code>Bool</code> from the given value.
099       */
100      public static Bool bool(final boolean b) {
101        return b ? t : f;
102      }
103    }