samples/apifest1/day2/elementbasedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
changeset 52 4257f4cf226b
child 59 02e05defc6a0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day2/elementbasedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + * The contents of this file are subject to the terms of the Common Development
     1.6 + * and Distribution License (the License). You may not use this file except in
     1.7 + * compliance with the License.
     1.8 + *
     1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    1.10 + * or http://www.netbeans.org/cddl.txt.
    1.11 + *
    1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    1.14 + * If applicable, add the following below the CDDL Header, with the fields
    1.15 + * enclosed by brackets [] replaced by your own identifying information:
    1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.17 + *
    1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.20 + * Microsystems, Inc. All Rights Reserved.
    1.21 + */
    1.22 +
    1.23 +package org.netbeans.apifest.boolcircuit;
    1.24 +
    1.25 +/**
    1.26 + */
    1.27 +public final class Circuit {
    1.28 +    private Circuit() {
    1.29 +    }
    1.30 +    
    1.31 +    public static Element and(final Element e1, final Element e2) {
    1.32 +        return new Element() {
    1.33 +            public boolean result() {
    1.34 +                return e1.result() && e2.result();
    1.35 +            }
    1.36 +            public double doubleResult() {
    1.37 +                return e1.doubleResult() * e2.doubleResult();
    1.38 +            }
    1.39 +        };
    1.40 +    }
    1.41 +    public static Element or(final Element e1, final Element e2) {
    1.42 +        return new Element() {
    1.43 +            public boolean result() {
    1.44 +                return e1.result() || e2.result();
    1.45 +            }
    1.46 +            public double doubleResult() {
    1.47 +                return 1.0 - (1.0 - e1.doubleResult()) * (1.0 - e2.doubleResult());
    1.48 +            }
    1.49 +        };
    1.50 +    }
    1.51 +
    1.52 +    public static Element not(final Element e1) {
    1.53 +        return new Element() {
    1.54 +            public boolean result() {
    1.55 +                return !e1.result();
    1.56 +            }
    1.57 +            public double doubleResult() {
    1.58 +                return 1 - e1.doubleResult();
    1.59 +            }
    1.60 +        };
    1.61 +    }
    1.62 +    
    1.63 +    public static Element operation(final Operation op, final Element... elements) {
    1.64 +        return new Element() {
    1.65 +            public boolean result() {
    1.66 +                return doubleResult() >= 1.0;
    1.67 +            }
    1.68 +            public double doubleResult() {
    1.69 +                double[] arr = new double[elements.length];
    1.70 +                for (int i = 0; i < arr.length; i++) {
    1.71 +                    arr[i] = elements[i].doubleResult();
    1.72 +                }
    1.73 +                return op.computeResult(arr);
    1.74 +            }
    1.75 +        };
    1.76 +        
    1.77 +    }
    1.78 +    
    1.79 +    public static Variable var() {
    1.80 +        return new Variable();
    1.81 +    }
    1.82 +    
    1.83 +    public static abstract class Element {
    1.84 +        private Element() {
    1.85 +        }
    1.86 +        
    1.87 +        public abstract boolean result();
    1.88 +        public abstract double doubleResult();
    1.89 +    }
    1.90 +    
    1.91 +    public static final class Variable extends Element {
    1.92 +        private Boolean booleanValue;
    1.93 +        private Double doubleValue;
    1.94 +        
    1.95 +        public void assignValue(boolean b) {
    1.96 +            booleanValue = b;
    1.97 +        }
    1.98 +        public void assignValue(double d) {
    1.99 +            if (d < 0 || d > 1) {
   1.100 +                throw new IllegalArgumentException();
   1.101 +            }
   1.102 +            doubleValue = d;
   1.103 +        }
   1.104 +
   1.105 +        public boolean result() {
   1.106 +            return booleanValue != null ? booleanValue : doubleValue >= 1.0;
   1.107 +        }
   1.108 +
   1.109 +        public double doubleResult() {
   1.110 +            return doubleValue != null ? doubleValue : (booleanValue ? 1.0 : 0.0);
   1.111 +        }
   1.112 +
   1.113 +    }
   1.114 +    
   1.115 +    public static interface Operation {
   1.116 +        public double computeResult(double... values);
   1.117 +    }
   1.118 +}