samples/apifest1/day1/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java
changeset 52 4257f4cf226b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day1/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,97 @@
     1.4 +/*
     1.5 + * Element.java
     1.6 + *
     1.7 + * Created on 12. Ĩervenec 2006, 14:14
     1.8 + *
     1.9 + * To change this template, choose Tools | Template Manager
    1.10 + * and open the template in the editor.
    1.11 + */
    1.12 +
    1.13 +package org.netbeans.apifest.boolcircuit;
    1.14 +
    1.15 +/**
    1.16 + * Representation of an element in the circuit.
    1.17 + * The internal behaviour of the element is opaque to the API user, it can only
    1.18 + * be used as a building block for logical equations, using primitive operation
    1.19 + * factories and a factory for input pin representation.
    1.20 + * Elements are chained to create the logical net. The inputs to the net are
    1.21 + * represented by the elements created by {@link #createInput(boolean[])}
    1.22 + * factory method.
    1.23 + */
    1.24 +public abstract class Element {
    1.25 +        
    1.26 +    /** Creates a new instance of Element */
    1.27 +    private Element() {
    1.28 +    }
    1.29 +    
    1.30 +    /**
    1.31 +      */
    1.32 +    abstract boolean evaluate(boolean[] inputs);
    1.33 +    
    1.34 +    abstract int maxInput();
    1.35 +    
    1.36 +    /**
    1.37 +     * Creates an Element representing 2-input AND function.
    1.38 +     *
    1.39 +     */
    1.40 +    public static Element createAnd(final Element source1, final Element source2) {
    1.41 +        return new Element() {
    1.42 +            boolean evaluate(boolean[] inputs) {
    1.43 +                return source1.evaluate(inputs) & source2.evaluate(inputs);
    1.44 +            }
    1.45 +            
    1.46 +            int maxInput() {
    1.47 +                return Math.max(source1.maxInput(), source2.maxInput());
    1.48 +            }
    1.49 +        };
    1.50 +    }
    1.51 +
    1.52 +    /**
    1.53 +     * Creates an Element representing 2-input OR function.
    1.54 +     *
    1.55 +     */
    1.56 +    public static Element createOr(final Element source1, final Element source2) {
    1.57 +        return new Element() {
    1.58 +            boolean evaluate(boolean[] inputs) {
    1.59 +                return source1.evaluate(inputs) | source2.evaluate(inputs);
    1.60 +            }
    1.61 +            
    1.62 +            int maxInput() {
    1.63 +                return Math.max(source1.maxInput(), source2.maxInput());
    1.64 +            }
    1.65 +        };
    1.66 +    }
    1.67 +    
    1.68 +    /**
    1.69 +     * Creates an Element representing negation.
    1.70 +     *
    1.71 +     */
    1.72 +    public static Element createNot(final Element source1) {
    1.73 +        return new Element() {
    1.74 +            boolean evaluate(boolean[] inputs) {
    1.75 +                return !source1.evaluate(inputs);
    1.76 +            }
    1.77 +            
    1.78 +            int maxInput() {
    1.79 +                return source1.maxInput();
    1.80 +            }
    1.81 +        };
    1.82 +    }
    1.83 +    
    1.84 +    /**
    1.85 +     * Creates an Element representing input to the logical net.
    1.86 +     *
    1.87 +     */
    1.88 +    public static Element createInput(final int pin) {
    1.89 +        return new Element() {
    1.90 +            boolean evaluate(boolean[] inputs) {
    1.91 +                return inputs[pin];
    1.92 +            }
    1.93 +            
    1.94 +            int maxInput() {
    1.95 +                return pin;
    1.96 +            }
    1.97 +        };
    1.98 +    }
    1.99 +    
   1.100 +}