samples/apifest1/day2/welltestedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
2 * The contents of this file are subject to the terms of the Common Development
3 * and Distribution License (the License). You may not use this file except in
4 * compliance with the License.
6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7 * or http://www.netbeans.org/cddl.txt.
9 * When distributing Covered Code, include this CDDL Header Notice in each file
10 * and include the License file at http://www.netbeans.org/cddl.txt.
11 * If applicable, add the following below the CDDL Header, with the fields
12 * enclosed by brackets [] replaced by your own identifying information:
13 * "Portions Copyrighted [year] [name of copyright owner]"
15 * The Original Software is NetBeans. The Initial Developer of the Original
16 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17 * Microsystems, Inc. All Rights Reserved.
20 package org.netbeans.apifest.boolcircuit;
24 public abstract class Circuit {
26 /** Creates a simple Circuit that is associated with given input pin.
29 public static Circuit input(int pin) {
30 return new Input(pin);
33 /** Create circuit that inverts results of input circuit.
35 public static Circuit createNotCircuit(Circuit in) {
39 public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
40 return new Binary(in1, in2, true);
43 public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
44 return new Binary(in1, in2, false);
51 public final boolean evaluate(Boolean ... inputs) {
53 throw new IllegalArgumentException();
54 double[] vals = new double [inputs.length];
55 for (int i = 0; i < inputs.length; i++)
56 vals[i] = inputs[i]? 1.0: 0.0;
57 return evaluateFuzzy(vals) == 1.0;
60 public final double evaluateFuzzy(double ... inputs) {
62 throw new IllegalArgumentException();
63 return doEvaluate(inputs);
66 protected abstract double doEvaluate(double ... inputs);
68 public abstract int maxInputs();
70 private interface OldEval {
72 double doEvaluate1(int offset, double ... inputs);
75 private static class Input extends Circuit {
83 protected double doEvaluate(double ... inputs) {
84 if (inputs == null || inputs.length < maxInputs())
85 throw new IllegalArgumentException();
90 public int maxInputs() {
96 private static class Not extends Circuit implements OldEval {
104 public double doEvaluate(double ... inputs) {
105 return doEvaluate1(0, inputs);
108 public double doEvaluate1(int i, double ... inputs) {
110 if (inputs == null || inputs.length < i + 1)
111 throw new IllegalArgumentException();
112 return 1 - inputs[i];
115 if (in instanceof OldEval) {
116 return 1 - ((OldEval)in).doEvaluate1(i, inputs[i]);
119 return 1 - in.evaluateFuzzy(inputs);
123 public int maxInputs() {
124 return in != null? in.maxInputs(): 1;
128 private static class Binary extends Circuit implements OldEval {
130 private Circuit in1, in2;
133 Binary (Circuit in1, Circuit in2, boolean or) {
139 public double doEvaluate(double ... inputs) {
140 return doEvaluate1(0, inputs);
143 public double doEvaluate1(int i, double ... inputs) {
146 if (inputs == null || inputs.length < i + 1)
147 throw new IllegalArgumentException();
150 if (in1 instanceof OldEval) {
151 x1 = ((OldEval)in1).doEvaluate1(i, inputs);
154 x1 = in1.evaluateFuzzy(inputs);
158 if (inputs == null || inputs.length < i + (in1 != null? in1.maxInputs(): 1) + 1)
159 throw new IllegalArgumentException();
160 x2 = inputs[i + (in1 != null? in1.maxInputs(): 1)];
162 if (in2 instanceof OldEval) {
163 x2 = ((OldEval)in2).doEvaluate1(i + (in1 != null? in1.maxInputs(): 1), inputs);
166 x2 = in2.evaluateFuzzy(inputs);
169 return or? 1 - (1 - x1) * (1 - x2) : x1 * x2;
172 public int maxInputs() {
173 return (in1 != null? in1.maxInputs(): 1) +
174 (in2 != null? in2.maxInputs(): 1) ;