samples/apifest1/day1/alwayscreatenewcircuit/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 {
25 /** Creates a new instance of Circuits */
29 public static Circuit negate(boolean input) {
30 return new Negation(input);
33 public static Circuit negate(Circuit input) {
34 return new Negation(input);
37 public static Circuit and(boolean input1, boolean input2) {
38 return new And(input1, input2);
41 public static Circuit and(Circuit input1, boolean input2) {
42 return new And(input1, new Primitive(input2));
45 public static Circuit and(boolean input1, Circuit input2) {
46 return new And(new Primitive(input1), input2);
49 public static Circuit and(Circuit input1, Circuit input2) {
50 return new And(input1, input2);
53 public static Circuit or(boolean input1, boolean input2) {
54 return new Or(input1, input2);
57 public static Circuit or(Circuit input1, boolean input2) {
58 return new Or(input1, new Primitive(input2));
61 public static Circuit or(boolean input1, Circuit input2) {
62 return new Or(new Primitive(input1), input2);
65 public static Circuit or(Circuit input1, Circuit input2) {
66 return new Or(input1, input2);
70 public abstract boolean output();
72 private final static class Primitive extends Circuit {
73 private boolean input;
75 Primitive(boolean input) {
79 public final boolean output() {
84 private final static class Negation extends Circuit {
85 private Circuit input;
87 Negation(boolean input) {
88 this.input = new Primitive(input);
91 Negation(Circuit input) {
95 public final boolean output() {
96 return !(input.output());
100 private final static class And extends Circuit {
101 private Circuit input1;
102 private Circuit input2;
105 And(boolean input1, boolean input2) {
106 this.input1 = new Primitive(input1);
107 this.input2 = new Primitive(input2);
110 And(Circuit input1, Circuit input2) {
111 this.input1 = input1;
112 this.input2 = input2;
115 public final boolean output() {
116 return input1.output() && input2.output();
120 private final static class Or extends Circuit {
121 private Circuit input1;
122 private Circuit input2;
125 Or(boolean input1, boolean input2) {
126 this.input1 = new Primitive(input1);
127 this.input2 = new Primitive(input2);
130 Or(Circuit input1, Circuit input2) {
131 this.input1 = input1;
132 this.input2 = input2;
135 public final boolean output() {
136 return input1.output() || input2.output();