jtulach@52: package org.netbeans.apifest.boolcircuit; jtulach@52: jtulach@59: // BEGIN: apifest.day2.elementbasedsolution.Circuit jtulach@52: public final class Circuit { jtulach@52: private Circuit() { jtulach@52: } jtulach@52: jtulach@52: public static Element and(final Element e1, final Element e2) { jtulach@52: return new Element() { jtulach@52: public boolean result() { jtulach@52: return e1.result() && e2.result(); jtulach@52: } jtulach@52: public double doubleResult() { jtulach@52: return e1.doubleResult() * e2.doubleResult(); jtulach@52: } jtulach@52: }; jtulach@52: } jtulach@52: public static Element or(final Element e1, final Element e2) { jtulach@52: return new Element() { jtulach@52: public boolean result() { jtulach@52: return e1.result() || e2.result(); jtulach@52: } jtulach@52: public double doubleResult() { jtulach@132: return 1.0 - jtulach@132: (1.0 - e1.doubleResult()) * (1.0 - e2.doubleResult()); jtulach@52: } jtulach@52: }; jtulach@52: } jtulach@52: jtulach@52: public static Element not(final Element e1) { jtulach@52: return new Element() { jtulach@52: public boolean result() { jtulach@52: return !e1.result(); jtulach@52: } jtulach@52: public double doubleResult() { jtulach@52: return 1 - e1.doubleResult(); jtulach@52: } jtulach@52: }; jtulach@52: } jtulach@52: jtulach@132: public static Element operation( jtulach@132: final Operation op, final Element... elements jtulach@132: ) { jtulach@52: return new Element() { jtulach@52: public boolean result() { jtulach@52: return doubleResult() >= 1.0; jtulach@52: } jtulach@52: public double doubleResult() { jtulach@52: double[] arr = new double[elements.length]; jtulach@52: for (int i = 0; i < arr.length; i++) { jtulach@52: arr[i] = elements[i].doubleResult(); jtulach@52: } jtulach@52: return op.computeResult(arr); jtulach@52: } jtulach@52: }; jtulach@52: jtulach@52: } jtulach@52: jtulach@52: public static Variable var() { jtulach@52: return new Variable(); jtulach@52: } jtulach@52: jtulach@52: public static abstract class Element { jtulach@52: private Element() { jtulach@52: } jtulach@52: jtulach@52: public abstract boolean result(); jtulach@52: public abstract double doubleResult(); jtulach@52: } jtulach@52: jtulach@52: public static final class Variable extends Element { jtulach@52: private Boolean booleanValue; jtulach@52: private Double doubleValue; jtulach@52: jtulach@52: public void assignValue(boolean b) { jtulach@52: booleanValue = b; jtulach@52: } jtulach@52: public void assignValue(double d) { jtulach@52: if (d < 0 || d > 1) { jtulach@52: throw new IllegalArgumentException(); jtulach@52: } jtulach@52: doubleValue = d; jtulach@52: } jtulach@52: jtulach@52: public boolean result() { jtulach@132: return booleanValue != null ? jtulach@132: booleanValue : doubleValue >= 1.0; jtulach@52: } jtulach@52: jtulach@52: public double doubleResult() { jtulach@132: return doubleValue != null ? jtulach@132: doubleValue : (booleanValue ? 1.0 : 0.0); jtulach@52: } jtulach@52: jtulach@52: } jtulach@52: jtulach@52: public static interface Operation { jtulach@52: public double computeResult(double... values); jtulach@52: } jtulach@52: } jtulach@59: // END: apifest.day2.elementbasedsolution.Circuit