samples/apifest1/day1/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 public static Circuit createNotCircuit(Circuit in) {
27 return new NotCircuit(in);
30 public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
31 return new OrCircuit(in1, in2);
34 public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
35 return new AndCircuit(in1, in2);
42 public abstract boolean evaluate(Boolean ... inputs);
44 abstract int inputs();
46 private static class NotCircuit extends Circuit {
50 NotCircuit (Circuit in) {
55 return in == null? 1: in.inputs();
58 public boolean evaluate (Boolean ... inputs) {
59 if (inputs == null || inputs.length != inputs()) {
60 throw new IllegalArgumentException();
62 Boolean o = inputs[0];
64 return !in.evaluate(inputs);
67 return !o.booleanValue();
69 throw new IllegalArgumentException();
73 private static class AndCircuit extends Circuit {
74 private Circuit in1, in2;
76 AndCircuit (Circuit in1, Circuit in2) {
82 return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
85 public boolean evaluate (Boolean ... inputs) {
86 if (inputs == null || inputs.length != inputs()) {
87 throw new IllegalArgumentException();
90 Boolean o1 = inputs[0];
92 Boolean[] ins1 = new Boolean[in1.inputs()];
93 System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
94 x1 = in1.evaluate(ins1);
96 else if (o1 != null) {
97 x1 = o1.booleanValue();
100 throw new IllegalArgumentException();
102 Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
104 Boolean[] ins2 = new Boolean[in2.inputs()];
105 System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
106 x2 = in2.evaluate(ins2);
108 else if (o2 != null) {
109 x2 = o2.booleanValue();
112 throw new IllegalArgumentException();
118 private static class OrCircuit extends Circuit {
119 private Circuit in1, in2;
121 OrCircuit (Circuit in1, Circuit in2) {
127 return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
130 public boolean evaluate (Boolean ... inputs) {
131 if (inputs == null || inputs.length != inputs()) {
132 throw new IllegalArgumentException();
135 Boolean o1 = inputs[0];
137 Boolean[] ins1 = new Boolean[in1.inputs()];
138 System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
139 x1 = in1.evaluate(ins1);
141 else if (o1 != null) {
142 x1 = o1.booleanValue();
145 throw new IllegalArgumentException();
147 Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
149 Boolean[] ins2 = new Boolean[in2.inputs()];
150 System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
151 x2 = in2.evaluate(ins2);
153 else if (o2 != null) {
154 x2 = o2.booleanValue();
157 throw new IllegalArgumentException();