samples/apifest1/day1/alwayscreatenewcircuit/src/org/netbeans/apifest/boolcircuit/Circuit.java
changeset 52 4257f4cf226b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day1/alwayscreatenewcircuit/src/org/netbeans/apifest/boolcircuit/Circuit.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,139 @@
     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 +/**
    1.26 + */
    1.27 +public abstract class Circuit {
    1.28 +    /** Creates a new instance of Circuits */
    1.29 +    private Circuit() {
    1.30 +    }
    1.31 +
    1.32 +    public static Circuit negate(boolean input) {
    1.33 +        return new Negation(input);
    1.34 +    }
    1.35 +
    1.36 +    public static Circuit negate(Circuit input) {
    1.37 +        return new Negation(input);
    1.38 +    }
    1.39 +
    1.40 +    public static Circuit and(boolean input1, boolean input2) {
    1.41 +        return new And(input1, input2);
    1.42 +    }
    1.43 +
    1.44 +    public static Circuit and(Circuit  input1, boolean input2) {
    1.45 +        return new And(input1, new Primitive(input2));
    1.46 +    }
    1.47 +
    1.48 +    public static Circuit and(boolean input1, Circuit input2) {
    1.49 +        return new And(new Primitive(input1), input2);
    1.50 +    }
    1.51 +
    1.52 +    public static Circuit and(Circuit input1, Circuit input2) {
    1.53 +        return new And(input1, input2);
    1.54 +    }
    1.55 +
    1.56 +    public static Circuit or(boolean input1, boolean input2) {
    1.57 +        return new Or(input1, input2);
    1.58 +    }
    1.59 +
    1.60 +    public static Circuit or(Circuit input1, boolean input2) {
    1.61 +        return new Or(input1, new Primitive(input2));
    1.62 +    }
    1.63 +
    1.64 +    public static Circuit or(boolean input1, Circuit input2) {
    1.65 +        return new Or(new Primitive(input1), input2);
    1.66 +    }
    1.67 +
    1.68 +    public static Circuit or(Circuit input1, Circuit input2) {
    1.69 +        return new Or(input1, input2);
    1.70 +    }
    1.71 +
    1.72 +
    1.73 +    public abstract boolean output();
    1.74 +
    1.75 +    private final static class Primitive extends Circuit {
    1.76 +        private boolean input;
    1.77 +
    1.78 +        Primitive(boolean input) {
    1.79 +            this.input = input;
    1.80 +        }
    1.81 +
    1.82 +        public final boolean output() {
    1.83 +            return input;
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    private final static class Negation extends Circuit {
    1.88 +        private Circuit input;
    1.89 +
    1.90 +        Negation(boolean input) {
    1.91 +            this.input = new Primitive(input);
    1.92 +        }
    1.93 +
    1.94 +        Negation(Circuit input) {
    1.95 +            this.input = input;
    1.96 +        }
    1.97 +
    1.98 +        public final boolean output() {
    1.99 +            return !(input.output());
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    private final static class And extends Circuit {
   1.104 +        private Circuit input1;
   1.105 +        private Circuit input2;
   1.106 +
   1.107 +
   1.108 +        And(boolean input1, boolean input2) {
   1.109 +            this.input1 = new Primitive(input1);
   1.110 +            this.input2 = new Primitive(input2);
   1.111 +        }
   1.112 +
   1.113 +        And(Circuit input1, Circuit input2) {
   1.114 +            this.input1 = input1;
   1.115 +            this.input2 = input2;
   1.116 +        }
   1.117 +
   1.118 +        public final boolean output() {
   1.119 +            return input1.output() && input2.output();
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    private final static class Or extends Circuit {
   1.124 +        private Circuit input1;
   1.125 +        private Circuit input2;
   1.126 +
   1.127 +
   1.128 +        Or(boolean input1, boolean input2) {
   1.129 +            this.input1 = new Primitive(input1);
   1.130 +            this.input2 = new Primitive(input2);
   1.131 +        }
   1.132 +
   1.133 +        Or(Circuit input1, Circuit input2) {
   1.134 +            this.input1 = input1;
   1.135 +            this.input2 = input2;
   1.136 +        }
   1.137 +
   1.138 +        public final boolean output() {
   1.139 +            return input1.output() || input2.output();
   1.140 +        }
   1.141 +    }
   1.142 +  }
   1.143 \ No newline at end of file