samples/apifest1/day2/alwayscreatenewcircuit/src/org/netbeans/apifest/boolcircuit/Circuit.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/apifest1/day2/alwayscreatenewcircuit/src/org/netbeans/apifest/boolcircuit/Circuit.java Sat Jun 14 09:52:45 2008 +0200
1.3 @@ -0,0 +1,234 @@
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 +import java.util.ArrayList;
1.26 +import java.util.Arrays;
1.27 +import java.util.LinkedHashSet;
1.28 +
1.29 +/**
1.30 + */
1.31 +public abstract class Circuit {
1.32 + /** Creates a new instance of Circuits */
1.33 + protected Circuit() {
1.34 + }
1.35 +
1.36 + public static Circuit negate(Input input) {
1.37 + return new Negation(new Primitive(input));
1.38 + }
1.39 +
1.40 + @Deprecated
1.41 + public static Circuit negate(boolean input) {
1.42 + return new Negation(input);
1.43 + }
1.44 +
1.45 + public static Circuit negate(Circuit input) {
1.46 + return new Negation(input);
1.47 + }
1.48 +
1.49 + public static Circuit and(Input input1, Input input2) {
1.50 + return new And(new Primitive(input1), new Primitive(input2));
1.51 + }
1.52 +
1.53 + @Deprecated
1.54 + public static Circuit and(boolean input1, boolean input2) {
1.55 + return new And(input1, input2);
1.56 + }
1.57 +
1.58 + public static Circuit and(Circuit input1, Input input2) {
1.59 + return new And(input1, new Primitive(input2));
1.60 + }
1.61 +
1.62 + @Deprecated
1.63 + public static Circuit and(Circuit input1, boolean input2) {
1.64 + return new And(input1, new Primitive(input2));
1.65 + }
1.66 +
1.67 + public static Circuit and(Input input1, Circuit input2) {
1.68 + return new And(new Primitive(input1), input2);
1.69 + }
1.70 +
1.71 + @Deprecated
1.72 + public static Circuit and(boolean input1, Circuit input2) {
1.73 + return new And(new Primitive(input1), input2);
1.74 + }
1.75 +
1.76 + public static Circuit and(Circuit input1, Circuit input2) {
1.77 + return new And(input1, input2);
1.78 + }
1.79 +
1.80 + public static Circuit or(Input input1, Input input2) {
1.81 + return new Or(new Primitive(input1), new Primitive(input2));
1.82 + }
1.83 +
1.84 + @Deprecated
1.85 + public static Circuit or(boolean input1, boolean input2) {
1.86 + return new Or(input1, input2);
1.87 + }
1.88 +
1.89 + public static Circuit or(Circuit input1, Input input2) {
1.90 + return new Or(input1, new Primitive(input2));
1.91 + }
1.92 +
1.93 + @Deprecated
1.94 + public static Circuit or(Circuit input1, boolean input2) {
1.95 + return new Or(input1, new Primitive(input2));
1.96 + }
1.97 +
1.98 + public static Circuit or(Input input1, Circuit input2) {
1.99 + return new Or(new Primitive(input1), input2);
1.100 + }
1.101 +
1.102 + @Deprecated
1.103 + public static Circuit or(boolean input1, Circuit input2) {
1.104 + return new Or(new Primitive(input1), input2);
1.105 + }
1.106 +
1.107 + public static Circuit or(Circuit input1, Circuit input2) {
1.108 + return new Or(input1, input2);
1.109 + }
1.110 +
1.111 +
1.112 + public final boolean output() {
1.113 + double v = value();
1.114 + if (v > 1 || v < 0) {
1.115 + throw new IllegalArgumentException();
1.116 + }
1.117 + return (v > 0) ? true : false;
1.118 + }
1.119 +
1.120 + private static final double check(double v) {
1.121 + if (v > 1 || v < 0) {
1.122 + throw new IllegalArgumentException();
1.123 + }
1.124 + return v;
1.125 + }
1.126 +
1.127 +
1.128 + public abstract double value();
1.129 +
1.130 + private final static class Primitive extends Circuit {
1.131 + private Input input;
1.132 +
1.133 + Primitive(boolean input) {
1.134 + this.input = new Input(input);
1.135 + }
1.136 +
1.137 + Primitive(Input input) {
1.138 + this.input = input;
1.139 + }
1.140 +
1.141 +
1.142 +
1.143 + public double value() {
1.144 + return input.getValue();
1.145 + }
1.146 + }
1.147 +
1.148 + private final static class Negation extends Circuit {
1.149 + private Circuit input;
1.150 +
1.151 + @Deprecated
1.152 + Negation(boolean input) {
1.153 + this.input = new Primitive(input);
1.154 + }
1.155 +
1.156 + Negation(Circuit input) {
1.157 + this.input = input;
1.158 + }
1.159 +
1.160 +
1.161 + public double value() {
1.162 + double x = input.value();
1.163 + return check(1-x);
1.164 + }
1.165 + }
1.166 +
1.167 + private static class And extends Circuit {
1.168 + Circuit input1;
1.169 + Circuit input2;
1.170 +
1.171 + And(boolean input1, boolean input2) {
1.172 + this.input1 = new Primitive(input1);
1.173 + this.input2 = new Primitive(input2);
1.174 + }
1.175 +
1.176 + And(Circuit input1, Circuit input2) {
1.177 + this.input1 = input1;
1.178 + this.input2 = input2;
1.179 + }
1.180 +
1.181 + public double value() {
1.182 + double x = input1.value();
1.183 + double y = input2.value();
1.184 + return check(x * y);
1.185 + }
1.186 + }
1.187 +
1.188 + private final static class Or extends And {
1.189 + Or(boolean input1, boolean input2) {
1.190 + super(input1, input2);
1.191 + }
1.192 +
1.193 + Or(Circuit input1, Circuit input2) {
1.194 + super(input1, input2);
1.195 + }
1.196 +
1.197 + public double value() {
1.198 + double x = input1.value();
1.199 + double y = input2.value();
1.200 +
1.201 + return check(1 - (1 - x) * (1 - y));
1.202 + }
1.203 + }
1.204 +
1.205 + public static class Input {
1.206 + private double value;
1.207 +
1.208 + public static Input valueOf(boolean initValue) {
1.209 + return new Input(initValue);
1.210 + }
1.211 +
1.212 + public static Input valueOf(double initValue) {
1.213 + return new Input(initValue);
1.214 + }
1.215 +
1.216 + /** Creates a new instance of Input */
1.217 + private Input(boolean input) {
1.218 + setValue(input);
1.219 + }
1.220 +
1.221 + private Input(double input) {
1.222 + setValue(input);
1.223 + }
1.224 +
1.225 + public double getValue() {
1.226 + return value;
1.227 + }
1.228 +
1.229 + public void setValue(double input) {
1.230 + value = input;
1.231 + }
1.232 +
1.233 + public void setValue(boolean input) {
1.234 + value = (input) ? 1 : 0;
1.235 + }
1.236 + }
1.237 +}