samples/apifest1/day2/elementbasedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 59 02e05defc6a0
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
jtulach@52
     1
package org.netbeans.apifest.boolcircuit;
jtulach@52
     2
jtulach@59
     3
// BEGIN: apifest.day2.elementbasedsolution.Circuit
jtulach@52
     4
public final class Circuit {
jtulach@52
     5
    private Circuit() {
jtulach@52
     6
    }
jtulach@52
     7
    
jtulach@52
     8
    public static Element and(final Element e1, final Element e2) {
jtulach@52
     9
        return new Element() {
jtulach@52
    10
            public boolean result() {
jtulach@52
    11
                return e1.result() && e2.result();
jtulach@52
    12
            }
jtulach@52
    13
            public double doubleResult() {
jtulach@52
    14
                return e1.doubleResult() * e2.doubleResult();
jtulach@52
    15
            }
jtulach@52
    16
        };
jtulach@52
    17
    }
jtulach@52
    18
    public static Element or(final Element e1, final Element e2) {
jtulach@52
    19
        return new Element() {
jtulach@52
    20
            public boolean result() {
jtulach@52
    21
                return e1.result() || e2.result();
jtulach@52
    22
            }
jtulach@52
    23
            public double doubleResult() {
jtulach@132
    24
                return 1.0 - 
jtulach@132
    25
                    (1.0 - e1.doubleResult()) * (1.0 - e2.doubleResult());
jtulach@52
    26
            }
jtulach@52
    27
        };
jtulach@52
    28
    }
jtulach@52
    29
jtulach@52
    30
    public static Element not(final Element e1) {
jtulach@52
    31
        return new Element() {
jtulach@52
    32
            public boolean result() {
jtulach@52
    33
                return !e1.result();
jtulach@52
    34
            }
jtulach@52
    35
            public double doubleResult() {
jtulach@52
    36
                return 1 - e1.doubleResult();
jtulach@52
    37
            }
jtulach@52
    38
        };
jtulach@52
    39
    }
jtulach@52
    40
    
jtulach@132
    41
    public static Element operation(
jtulach@132
    42
        final Operation op, final Element... elements
jtulach@132
    43
    ) {
jtulach@52
    44
        return new Element() {
jtulach@52
    45
            public boolean result() {
jtulach@52
    46
                return doubleResult() >= 1.0;
jtulach@52
    47
            }
jtulach@52
    48
            public double doubleResult() {
jtulach@52
    49
                double[] arr = new double[elements.length];
jtulach@52
    50
                for (int i = 0; i < arr.length; i++) {
jtulach@52
    51
                    arr[i] = elements[i].doubleResult();
jtulach@52
    52
                }
jtulach@52
    53
                return op.computeResult(arr);
jtulach@52
    54
            }
jtulach@52
    55
        };
jtulach@52
    56
        
jtulach@52
    57
    }
jtulach@52
    58
    
jtulach@52
    59
    public static Variable var() {
jtulach@52
    60
        return new Variable();
jtulach@52
    61
    }
jtulach@52
    62
    
jtulach@52
    63
    public static abstract class Element {
jtulach@52
    64
        private Element() {
jtulach@52
    65
        }
jtulach@52
    66
        
jtulach@52
    67
        public abstract boolean result();
jtulach@52
    68
        public abstract double doubleResult();
jtulach@52
    69
    }
jtulach@52
    70
    
jtulach@52
    71
    public static final class Variable extends Element {
jtulach@52
    72
        private Boolean booleanValue;
jtulach@52
    73
        private Double doubleValue;
jtulach@52
    74
        
jtulach@52
    75
        public void assignValue(boolean b) {
jtulach@52
    76
            booleanValue = b;
jtulach@52
    77
        }
jtulach@52
    78
        public void assignValue(double d) {
jtulach@52
    79
            if (d < 0 || d > 1) {
jtulach@52
    80
                throw new IllegalArgumentException();
jtulach@52
    81
            }
jtulach@52
    82
            doubleValue = d;
jtulach@52
    83
        }
jtulach@52
    84
jtulach@52
    85
        public boolean result() {
jtulach@132
    86
            return booleanValue != null ? 
jtulach@132
    87
                booleanValue : doubleValue >= 1.0;
jtulach@52
    88
        }
jtulach@52
    89
jtulach@52
    90
        public double doubleResult() {
jtulach@132
    91
            return doubleValue != null ? 
jtulach@132
    92
                doubleValue : (booleanValue ? 1.0 : 0.0);
jtulach@52
    93
        }
jtulach@52
    94
jtulach@52
    95
    }
jtulach@52
    96
    
jtulach@52
    97
    public static interface Operation {
jtulach@52
    98
        public double computeResult(double... values);
jtulach@52
    99
    }
jtulach@52
   100
}
jtulach@59
   101
// END: apifest.day2.elementbasedsolution.Circuit