author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:52:52 +0200 | |
changeset 59 | 02e05defc6a0 |
parent 52 | 4257f4cf226b |
child 132 | 3bc4c54f4bcc |
permissions | -rw-r--r-- |
jtulach@52 | 1 |
/* |
jtulach@52 | 2 |
* The contents of this file are subject to the terms of the Common Development |
jtulach@52 | 3 |
* and Distribution License (the License). You may not use this file except in |
jtulach@52 | 4 |
* compliance with the License. |
jtulach@52 | 5 |
* |
jtulach@52 | 6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
jtulach@52 | 7 |
* or http://www.netbeans.org/cddl.txt. |
jtulach@52 | 8 |
* |
jtulach@52 | 9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
jtulach@52 | 10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
jtulach@52 | 11 |
* If applicable, add the following below the CDDL Header, with the fields |
jtulach@52 | 12 |
* enclosed by brackets [] replaced by your own identifying information: |
jtulach@52 | 13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
jtulach@52 | 14 |
* |
jtulach@52 | 15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
jtulach@52 | 16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
jtulach@52 | 17 |
* Microsystems, Inc. All Rights Reserved. |
jtulach@52 | 18 |
*/ |
jtulach@52 | 19 |
|
jtulach@52 | 20 |
package org.netbeans.apifest.boolcircuit; |
jtulach@52 | 21 |
|
jtulach@59 | 22 |
// BEGIN: apifest.day2.elementbasedsolution.Circuit |
jtulach@52 | 23 |
public final class Circuit { |
jtulach@52 | 24 |
private Circuit() { |
jtulach@52 | 25 |
} |
jtulach@52 | 26 |
|
jtulach@52 | 27 |
public static Element and(final Element e1, final Element e2) { |
jtulach@52 | 28 |
return new Element() { |
jtulach@52 | 29 |
public boolean result() { |
jtulach@52 | 30 |
return e1.result() && e2.result(); |
jtulach@52 | 31 |
} |
jtulach@52 | 32 |
public double doubleResult() { |
jtulach@52 | 33 |
return e1.doubleResult() * e2.doubleResult(); |
jtulach@52 | 34 |
} |
jtulach@52 | 35 |
}; |
jtulach@52 | 36 |
} |
jtulach@52 | 37 |
public static Element or(final Element e1, final Element e2) { |
jtulach@52 | 38 |
return new Element() { |
jtulach@52 | 39 |
public boolean result() { |
jtulach@52 | 40 |
return e1.result() || e2.result(); |
jtulach@52 | 41 |
} |
jtulach@52 | 42 |
public double doubleResult() { |
jtulach@52 | 43 |
return 1.0 - (1.0 - e1.doubleResult()) * (1.0 - e2.doubleResult()); |
jtulach@52 | 44 |
} |
jtulach@52 | 45 |
}; |
jtulach@52 | 46 |
} |
jtulach@52 | 47 |
|
jtulach@52 | 48 |
public static Element not(final Element e1) { |
jtulach@52 | 49 |
return new Element() { |
jtulach@52 | 50 |
public boolean result() { |
jtulach@52 | 51 |
return !e1.result(); |
jtulach@52 | 52 |
} |
jtulach@52 | 53 |
public double doubleResult() { |
jtulach@52 | 54 |
return 1 - e1.doubleResult(); |
jtulach@52 | 55 |
} |
jtulach@52 | 56 |
}; |
jtulach@52 | 57 |
} |
jtulach@52 | 58 |
|
jtulach@52 | 59 |
public static Element operation(final Operation op, final Element... elements) { |
jtulach@52 | 60 |
return new Element() { |
jtulach@52 | 61 |
public boolean result() { |
jtulach@52 | 62 |
return doubleResult() >= 1.0; |
jtulach@52 | 63 |
} |
jtulach@52 | 64 |
public double doubleResult() { |
jtulach@52 | 65 |
double[] arr = new double[elements.length]; |
jtulach@52 | 66 |
for (int i = 0; i < arr.length; i++) { |
jtulach@52 | 67 |
arr[i] = elements[i].doubleResult(); |
jtulach@52 | 68 |
} |
jtulach@52 | 69 |
return op.computeResult(arr); |
jtulach@52 | 70 |
} |
jtulach@52 | 71 |
}; |
jtulach@52 | 72 |
|
jtulach@52 | 73 |
} |
jtulach@52 | 74 |
|
jtulach@52 | 75 |
public static Variable var() { |
jtulach@52 | 76 |
return new Variable(); |
jtulach@52 | 77 |
} |
jtulach@52 | 78 |
|
jtulach@52 | 79 |
public static abstract class Element { |
jtulach@52 | 80 |
private Element() { |
jtulach@52 | 81 |
} |
jtulach@52 | 82 |
|
jtulach@52 | 83 |
public abstract boolean result(); |
jtulach@52 | 84 |
public abstract double doubleResult(); |
jtulach@52 | 85 |
} |
jtulach@52 | 86 |
|
jtulach@52 | 87 |
public static final class Variable extends Element { |
jtulach@52 | 88 |
private Boolean booleanValue; |
jtulach@52 | 89 |
private Double doubleValue; |
jtulach@52 | 90 |
|
jtulach@52 | 91 |
public void assignValue(boolean b) { |
jtulach@52 | 92 |
booleanValue = b; |
jtulach@52 | 93 |
} |
jtulach@52 | 94 |
public void assignValue(double d) { |
jtulach@52 | 95 |
if (d < 0 || d > 1) { |
jtulach@52 | 96 |
throw new IllegalArgumentException(); |
jtulach@52 | 97 |
} |
jtulach@52 | 98 |
doubleValue = d; |
jtulach@52 | 99 |
} |
jtulach@52 | 100 |
|
jtulach@52 | 101 |
public boolean result() { |
jtulach@52 | 102 |
return booleanValue != null ? booleanValue : doubleValue >= 1.0; |
jtulach@52 | 103 |
} |
jtulach@52 | 104 |
|
jtulach@52 | 105 |
public double doubleResult() { |
jtulach@52 | 106 |
return doubleValue != null ? doubleValue : (booleanValue ? 1.0 : 0.0); |
jtulach@52 | 107 |
} |
jtulach@52 | 108 |
|
jtulach@52 | 109 |
} |
jtulach@52 | 110 |
|
jtulach@52 | 111 |
public static interface Operation { |
jtulach@52 | 112 |
public double computeResult(double... values); |
jtulach@52 | 113 |
} |
jtulach@52 | 114 |
} |
jtulach@59 | 115 |
// END: apifest.day2.elementbasedsolution.Circuit |