samples/apifest1/day2/alwayscreatenewcircuit/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;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.LinkedHashSet;
28 public abstract class Circuit {
29 /** Creates a new instance of Circuits */
33 public static Circuit negate(Input input) {
34 return new Negation(new Primitive(input));
38 public static Circuit negate(boolean input) {
39 return new Negation(input);
42 public static Circuit negate(Circuit input) {
43 return new Negation(input);
46 public static Circuit and(Input input1, Input input2) {
47 return new And(new Primitive(input1), new Primitive(input2));
51 public static Circuit and(boolean input1, boolean input2) {
52 return new And(input1, input2);
55 public static Circuit and(Circuit input1, Input input2) {
56 return new And(input1, new Primitive(input2));
60 public static Circuit and(Circuit input1, boolean input2) {
61 return new And(input1, new Primitive(input2));
64 public static Circuit and(Input input1, Circuit input2) {
65 return new And(new Primitive(input1), input2);
69 public static Circuit and(boolean input1, Circuit input2) {
70 return new And(new Primitive(input1), input2);
73 public static Circuit and(Circuit input1, Circuit input2) {
74 return new And(input1, input2);
77 public static Circuit or(Input input1, Input input2) {
78 return new Or(new Primitive(input1), new Primitive(input2));
82 public static Circuit or(boolean input1, boolean input2) {
83 return new Or(input1, input2);
86 public static Circuit or(Circuit input1, Input input2) {
87 return new Or(input1, new Primitive(input2));
91 public static Circuit or(Circuit input1, boolean input2) {
92 return new Or(input1, new Primitive(input2));
95 public static Circuit or(Input input1, Circuit input2) {
96 return new Or(new Primitive(input1), input2);
100 public static Circuit or(boolean input1, Circuit input2) {
101 return new Or(new Primitive(input1), input2);
104 public static Circuit or(Circuit input1, Circuit input2) {
105 return new Or(input1, input2);
109 public final boolean output() {
111 if (v > 1 || v < 0) {
112 throw new IllegalArgumentException();
114 return (v > 0) ? true : false;
117 private static final double check(double v) {
118 if (v > 1 || v < 0) {
119 throw new IllegalArgumentException();
125 public abstract double value();
127 private final static class Primitive extends Circuit {
130 Primitive(boolean input) {
131 this.input = new Input(input);
134 Primitive(Input input) {
140 public double value() {
141 return input.getValue();
145 private final static class Negation extends Circuit {
146 private Circuit input;
149 Negation(boolean input) {
150 this.input = new Primitive(input);
153 Negation(Circuit input) {
158 public double value() {
159 double x = input.value();
164 private static class And extends Circuit {
168 And(boolean input1, boolean input2) {
169 this.input1 = new Primitive(input1);
170 this.input2 = new Primitive(input2);
173 And(Circuit input1, Circuit input2) {
174 this.input1 = input1;
175 this.input2 = input2;
178 public double value() {
179 double x = input1.value();
180 double y = input2.value();
185 private final static class Or extends And {
186 Or(boolean input1, boolean input2) {
187 super(input1, input2);
190 Or(Circuit input1, Circuit input2) {
191 super(input1, input2);
194 public double value() {
195 double x = input1.value();
196 double y = input2.value();
198 return check(1 - (1 - x) * (1 - y));
202 public static class Input {
203 private double value;
205 public static Input valueOf(boolean initValue) {
206 return new Input(initValue);
209 public static Input valueOf(double initValue) {
210 return new Input(initValue);
213 /** Creates a new instance of Input */
214 private Input(boolean input) {
218 private Input(double input) {
222 public double getValue() {
226 public void setValue(double input) {
230 public void setValue(boolean input) {
231 value = (input) ? 1 : 0;