jtulach@52
|
1 |
/*
|
jtulach@52
|
2 |
* The contents of this file are subject to the terms of the Common Development
|
jtulach@52
|
3 |
* and Distribution License (the License). You may not use this file except in
|
jtulach@52
|
4 |
* compliance with the License.
|
jtulach@52
|
5 |
*
|
jtulach@52
|
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html
|
jtulach@52
|
7 |
* or http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
8 |
*
|
jtulach@52
|
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file
|
jtulach@52
|
10 |
* and include the License file at http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
11 |
* If applicable, add the following below the CDDL Header, with the fields
|
jtulach@52
|
12 |
* enclosed by brackets [] replaced by your own identifying information:
|
jtulach@52
|
13 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@52
|
14 |
*
|
jtulach@52
|
15 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@52
|
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
|
jtulach@52
|
17 |
* Microsystems, Inc. All Rights Reserved.
|
jtulach@52
|
18 |
*/
|
jtulach@52
|
19 |
|
jtulach@52
|
20 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
21 |
|
jtulach@52
|
22 |
/**
|
jtulach@52
|
23 |
*/
|
jtulach@52
|
24 |
public abstract class Circuit {
|
jtulach@52
|
25 |
|
jtulach@52
|
26 |
public static Circuit createNotCircuit(Circuit in) {
|
jtulach@52
|
27 |
return new NotCircuit(in);
|
jtulach@52
|
28 |
}
|
jtulach@52
|
29 |
|
jtulach@52
|
30 |
public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
|
jtulach@52
|
31 |
return new OrCircuit(in1, in2);
|
jtulach@52
|
32 |
}
|
jtulach@52
|
33 |
|
jtulach@52
|
34 |
public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
|
jtulach@52
|
35 |
return new AndCircuit(in1, in2);
|
jtulach@52
|
36 |
}
|
jtulach@52
|
37 |
|
jtulach@52
|
38 |
private Circuit() {
|
jtulach@52
|
39 |
|
jtulach@52
|
40 |
}
|
jtulach@52
|
41 |
|
jtulach@52
|
42 |
public abstract boolean evaluate(Boolean ... inputs);
|
jtulach@52
|
43 |
|
jtulach@52
|
44 |
abstract int inputs();
|
jtulach@52
|
45 |
|
jtulach@52
|
46 |
private static class NotCircuit extends Circuit {
|
jtulach@52
|
47 |
|
jtulach@52
|
48 |
private Circuit in;
|
jtulach@52
|
49 |
|
jtulach@52
|
50 |
NotCircuit (Circuit in) {
|
jtulach@52
|
51 |
this.in = in;
|
jtulach@52
|
52 |
}
|
jtulach@52
|
53 |
|
jtulach@52
|
54 |
int inputs() {
|
jtulach@52
|
55 |
return in == null? 1: in.inputs();
|
jtulach@52
|
56 |
}
|
jtulach@52
|
57 |
|
jtulach@52
|
58 |
public boolean evaluate (Boolean ... inputs) {
|
jtulach@52
|
59 |
if (inputs == null || inputs.length != inputs()) {
|
jtulach@52
|
60 |
throw new IllegalArgumentException();
|
jtulach@52
|
61 |
}
|
jtulach@52
|
62 |
Boolean o = inputs[0];
|
jtulach@52
|
63 |
if (in != null) {
|
jtulach@52
|
64 |
return !in.evaluate(inputs);
|
jtulach@52
|
65 |
}
|
jtulach@52
|
66 |
else if (o != null) {
|
jtulach@52
|
67 |
return !o.booleanValue();
|
jtulach@52
|
68 |
}
|
jtulach@52
|
69 |
throw new IllegalArgumentException();
|
jtulach@52
|
70 |
}
|
jtulach@52
|
71 |
}
|
jtulach@52
|
72 |
|
jtulach@52
|
73 |
private static class AndCircuit extends Circuit {
|
jtulach@52
|
74 |
private Circuit in1, in2;
|
jtulach@52
|
75 |
|
jtulach@52
|
76 |
AndCircuit (Circuit in1, Circuit in2) {
|
jtulach@52
|
77 |
this.in1 = in1;
|
jtulach@52
|
78 |
this.in2 = in2;
|
jtulach@52
|
79 |
}
|
jtulach@52
|
80 |
|
jtulach@52
|
81 |
int inputs() {
|
jtulach@52
|
82 |
return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
|
jtulach@52
|
83 |
}
|
jtulach@52
|
84 |
|
jtulach@52
|
85 |
public boolean evaluate (Boolean ... inputs) {
|
jtulach@52
|
86 |
if (inputs == null || inputs.length != inputs()) {
|
jtulach@52
|
87 |
throw new IllegalArgumentException();
|
jtulach@52
|
88 |
}
|
jtulach@52
|
89 |
boolean x1, x2;
|
jtulach@52
|
90 |
Boolean o1 = inputs[0];
|
jtulach@52
|
91 |
if (in1 != null) {
|
jtulach@52
|
92 |
Boolean[] ins1 = new Boolean[in1.inputs()];
|
jtulach@52
|
93 |
System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
|
jtulach@52
|
94 |
x1 = in1.evaluate(ins1);
|
jtulach@52
|
95 |
}
|
jtulach@52
|
96 |
else if (o1 != null) {
|
jtulach@52
|
97 |
x1 = o1.booleanValue();
|
jtulach@52
|
98 |
}
|
jtulach@52
|
99 |
else {
|
jtulach@52
|
100 |
throw new IllegalArgumentException();
|
jtulach@52
|
101 |
}
|
jtulach@52
|
102 |
Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
|
jtulach@52
|
103 |
if (in2 != null) {
|
jtulach@52
|
104 |
Boolean[] ins2 = new Boolean[in2.inputs()];
|
jtulach@52
|
105 |
System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
|
jtulach@52
|
106 |
x2 = in2.evaluate(ins2);
|
jtulach@52
|
107 |
}
|
jtulach@52
|
108 |
else if (o2 != null) {
|
jtulach@52
|
109 |
x2 = o2.booleanValue();
|
jtulach@52
|
110 |
}
|
jtulach@52
|
111 |
else {
|
jtulach@52
|
112 |
throw new IllegalArgumentException();
|
jtulach@52
|
113 |
}
|
jtulach@52
|
114 |
return x1 && x2;
|
jtulach@52
|
115 |
}
|
jtulach@52
|
116 |
}
|
jtulach@52
|
117 |
|
jtulach@52
|
118 |
private static class OrCircuit extends Circuit {
|
jtulach@52
|
119 |
private Circuit in1, in2;
|
jtulach@52
|
120 |
|
jtulach@52
|
121 |
OrCircuit (Circuit in1, Circuit in2) {
|
jtulach@52
|
122 |
this.in1 = in1;
|
jtulach@52
|
123 |
this.in2 = in2;
|
jtulach@52
|
124 |
}
|
jtulach@52
|
125 |
|
jtulach@52
|
126 |
int inputs() {
|
jtulach@52
|
127 |
return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
|
jtulach@52
|
128 |
}
|
jtulach@52
|
129 |
|
jtulach@52
|
130 |
public boolean evaluate (Boolean ... inputs) {
|
jtulach@52
|
131 |
if (inputs == null || inputs.length != inputs()) {
|
jtulach@52
|
132 |
throw new IllegalArgumentException();
|
jtulach@52
|
133 |
}
|
jtulach@52
|
134 |
boolean x1, x2;
|
jtulach@52
|
135 |
Boolean o1 = inputs[0];
|
jtulach@52
|
136 |
if (in1 != null) {
|
jtulach@52
|
137 |
Boolean[] ins1 = new Boolean[in1.inputs()];
|
jtulach@52
|
138 |
System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
|
jtulach@52
|
139 |
x1 = in1.evaluate(ins1);
|
jtulach@52
|
140 |
}
|
jtulach@52
|
141 |
else if (o1 != null) {
|
jtulach@52
|
142 |
x1 = o1.booleanValue();
|
jtulach@52
|
143 |
}
|
jtulach@52
|
144 |
else {
|
jtulach@52
|
145 |
throw new IllegalArgumentException();
|
jtulach@52
|
146 |
}
|
jtulach@52
|
147 |
Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
|
jtulach@52
|
148 |
if (in2 != null) {
|
jtulach@52
|
149 |
Boolean[] ins2 = new Boolean[in2.inputs()];
|
jtulach@52
|
150 |
System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
|
jtulach@52
|
151 |
x2 = in2.evaluate(ins2);
|
jtulach@52
|
152 |
}
|
jtulach@52
|
153 |
else if (o2 != null) {
|
jtulach@52
|
154 |
x2 = o2.booleanValue();
|
jtulach@52
|
155 |
}
|
jtulach@52
|
156 |
else {
|
jtulach@52
|
157 |
throw new IllegalArgumentException();
|
jtulach@52
|
158 |
}
|
jtulach@52
|
159 |
return x1 || x2;
|
jtulach@52
|
160 |
}
|
jtulach@52
|
161 |
}
|
jtulach@52
|
162 |
}
|