samples/apifest1/day1/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
4 * Created on 12. Ĩervenec 2006, 14:14
6 * To change this template, choose Tools | Template Manager
7 * and open the template in the editor.
10 package org.netbeans.apifest.boolcircuit;
13 * Representation of an element in the circuit.
14 * The internal behaviour of the element is opaque to the API user, it can only
15 * be used as a building block for logical equations, using primitive operation
16 * factories and a factory for input pin representation.
17 * Elements are chained to create the logical net. The inputs to the net are
18 * represented by the elements created by {@link #createInput(boolean[])}
21 public abstract class Element {
23 /** Creates a new instance of Element */
29 abstract boolean evaluate(boolean[] inputs);
31 abstract int maxInput();
34 * Creates an Element representing 2-input AND function.
37 public static Element createAnd(final Element source1, final Element source2) {
38 return new Element() {
39 boolean evaluate(boolean[] inputs) {
40 return source1.evaluate(inputs) & source2.evaluate(inputs);
44 return Math.max(source1.maxInput(), source2.maxInput());
50 * Creates an Element representing 2-input OR function.
53 public static Element createOr(final Element source1, final Element source2) {
54 return new Element() {
55 boolean evaluate(boolean[] inputs) {
56 return source1.evaluate(inputs) | source2.evaluate(inputs);
60 return Math.max(source1.maxInput(), source2.maxInput());
66 * Creates an Element representing negation.
69 public static Element createNot(final Element source1) {
70 return new Element() {
71 boolean evaluate(boolean[] inputs) {
72 return !source1.evaluate(inputs);
76 return source1.maxInput();
82 * Creates an Element representing input to the logical net.
85 public static Element createInput(final int pin) {
86 return new Element() {
87 boolean evaluate(boolean[] inputs) {