samples/apifest1/day1/welltestedsolution/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/welltestedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,162 @@
     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 +    
    1.29 +    public static Circuit createNotCircuit(Circuit in) {
    1.30 +        return new NotCircuit(in);
    1.31 +    }
    1.32 +    
    1.33 +    public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
    1.34 +        return new OrCircuit(in1, in2);
    1.35 +    }
    1.36 +    
    1.37 +    public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
    1.38 +        return new AndCircuit(in1, in2);
    1.39 +    }
    1.40 +    
    1.41 +    private Circuit() {
    1.42 +        
    1.43 +    }
    1.44 +    
    1.45 +    public abstract boolean evaluate(Boolean ... inputs);
    1.46 +    
    1.47 +    abstract int inputs();
    1.48 +    
    1.49 +    private static class NotCircuit extends Circuit {
    1.50 +
    1.51 +        private Circuit in;
    1.52 +        
    1.53 +        NotCircuit (Circuit in) {
    1.54 +            this.in = in;
    1.55 +        }
    1.56 +        
    1.57 +        int inputs() {
    1.58 +            return in == null? 1: in.inputs();
    1.59 +        }
    1.60 +        
    1.61 +        public boolean evaluate (Boolean ... inputs) {
    1.62 +            if (inputs == null || inputs.length != inputs()) { 
    1.63 +                throw new IllegalArgumentException();
    1.64 +            }
    1.65 +            Boolean o = inputs[0];
    1.66 +            if (in != null) { 
    1.67 +                return !in.evaluate(inputs);
    1.68 +            }
    1.69 +            else if (o != null) {
    1.70 +                return !o.booleanValue();
    1.71 +            }
    1.72 +            throw new IllegalArgumentException();
    1.73 +        }
    1.74 +    }
    1.75 +    
    1.76 +    private static class AndCircuit extends Circuit {
    1.77 +        private Circuit in1, in2;
    1.78 +        
    1.79 +        AndCircuit (Circuit in1, Circuit in2) {
    1.80 +            this.in1 = in1;
    1.81 +            this.in2 = in2;
    1.82 +        }
    1.83 +        
    1.84 +        int inputs() {
    1.85 +            return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
    1.86 +        }
    1.87 +        
    1.88 +        public boolean evaluate (Boolean ... inputs) {
    1.89 +            if (inputs == null || inputs.length != inputs()) { 
    1.90 +                throw new IllegalArgumentException();
    1.91 +            }
    1.92 +            boolean x1, x2;
    1.93 +            Boolean o1 = inputs[0];
    1.94 +            if (in1 != null) { 
    1.95 +                Boolean[] ins1 = new Boolean[in1.inputs()];
    1.96 +                System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
    1.97 +                x1 = in1.evaluate(ins1);
    1.98 +            }
    1.99 +            else if (o1 != null) {
   1.100 +                x1 = o1.booleanValue();
   1.101 +            }
   1.102 +            else {
   1.103 +                throw new IllegalArgumentException();
   1.104 +            }
   1.105 +            Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
   1.106 +            if (in2 != null) { 
   1.107 +                Boolean[] ins2 = new Boolean[in2.inputs()];
   1.108 +                System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
   1.109 +                x2 = in2.evaluate(ins2);
   1.110 +            }
   1.111 +            else if (o2 != null) {
   1.112 +                x2 = o2.booleanValue();
   1.113 +            }
   1.114 +            else {
   1.115 +                throw new IllegalArgumentException();
   1.116 +            }
   1.117 +            return x1 && x2;
   1.118 +        }
   1.119 +    }
   1.120 +    
   1.121 +    private static class OrCircuit extends Circuit {
   1.122 +        private Circuit in1, in2;
   1.123 +        
   1.124 +        OrCircuit (Circuit in1, Circuit in2) {
   1.125 +            this.in1 = in1;
   1.126 +            this.in2 = in2;
   1.127 +        }
   1.128 +        
   1.129 +        int inputs() {
   1.130 +            return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
   1.131 +        }
   1.132 +        
   1.133 +        public boolean evaluate (Boolean ... inputs) {
   1.134 +            if (inputs == null || inputs.length != inputs()) { 
   1.135 +                throw new IllegalArgumentException();
   1.136 +            }
   1.137 +            boolean x1, x2;
   1.138 +            Boolean o1 = inputs[0];
   1.139 +            if (in1 != null) { 
   1.140 +                Boolean[] ins1 = new Boolean[in1.inputs()];
   1.141 +                System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
   1.142 +                x1 = in1.evaluate(ins1);
   1.143 +            }
   1.144 +            else if (o1 != null) {
   1.145 +                x1 = o1.booleanValue();
   1.146 +            }
   1.147 +            else {
   1.148 +                throw new IllegalArgumentException();
   1.149 +            }
   1.150 +            Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
   1.151 +            if (in2 != null) { 
   1.152 +                Boolean[] ins2 = new Boolean[in2.inputs()];
   1.153 +                System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
   1.154 +                x2 = in2.evaluate(ins2);
   1.155 +            }
   1.156 +            else if (o2 != null) {
   1.157 +                x2 = o2.booleanValue();
   1.158 +            }
   1.159 +            else {
   1.160 +                throw new IllegalArgumentException();
   1.161 +            }
   1.162 +            return x1 || x2;
   1.163 +        }
   1.164 +    }
   1.165 +}