samples/apifest1/day1/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
permissions -rw-r--r--
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
jtulach@52
     1
/*
jtulach@52
     2
 * Element.java
jtulach@52
     3
 *
jtulach@52
     4
 * Created on 12. Ĩervenec 2006, 14:14
jtulach@52
     5
 *
jtulach@52
     6
 * To change this template, choose Tools | Template Manager
jtulach@52
     7
 * and open the template in the editor.
jtulach@52
     8
 */
jtulach@52
     9
jtulach@52
    10
package org.netbeans.apifest.boolcircuit;
jtulach@52
    11
jtulach@52
    12
/**
jtulach@52
    13
 * Representation of an element in the circuit.
jtulach@52
    14
 * The internal behaviour of the element is opaque to the API user, it can only
jtulach@52
    15
 * be used as a building block for logical equations, using primitive operation
jtulach@52
    16
 * factories and a factory for input pin representation.
jtulach@52
    17
 * Elements are chained to create the logical net. The inputs to the net are
jtulach@52
    18
 * represented by the elements created by {@link #createInput(boolean[])}
jtulach@52
    19
 * factory method.
jtulach@52
    20
 */
jtulach@52
    21
public abstract class Element {
jtulach@52
    22
        
jtulach@52
    23
    /** Creates a new instance of Element */
jtulach@52
    24
    private Element() {
jtulach@52
    25
    }
jtulach@52
    26
    
jtulach@52
    27
    /**
jtulach@52
    28
      */
jtulach@52
    29
    abstract boolean evaluate(boolean[] inputs);
jtulach@52
    30
    
jtulach@52
    31
    abstract int maxInput();
jtulach@52
    32
    
jtulach@52
    33
    /**
jtulach@52
    34
     * Creates an Element representing 2-input AND function.
jtulach@52
    35
     *
jtulach@52
    36
     */
jtulach@52
    37
    public static Element createAnd(final Element source1, final Element source2) {
jtulach@52
    38
        return new Element() {
jtulach@52
    39
            boolean evaluate(boolean[] inputs) {
jtulach@52
    40
                return source1.evaluate(inputs) & source2.evaluate(inputs);
jtulach@52
    41
            }
jtulach@52
    42
            
jtulach@52
    43
            int maxInput() {
jtulach@52
    44
                return Math.max(source1.maxInput(), source2.maxInput());
jtulach@52
    45
            }
jtulach@52
    46
        };
jtulach@52
    47
    }
jtulach@52
    48
jtulach@52
    49
    /**
jtulach@52
    50
     * Creates an Element representing 2-input OR function.
jtulach@52
    51
     *
jtulach@52
    52
     */
jtulach@52
    53
    public static Element createOr(final Element source1, final Element source2) {
jtulach@52
    54
        return new Element() {
jtulach@52
    55
            boolean evaluate(boolean[] inputs) {
jtulach@52
    56
                return source1.evaluate(inputs) | source2.evaluate(inputs);
jtulach@52
    57
            }
jtulach@52
    58
            
jtulach@52
    59
            int maxInput() {
jtulach@52
    60
                return Math.max(source1.maxInput(), source2.maxInput());
jtulach@52
    61
            }
jtulach@52
    62
        };
jtulach@52
    63
    }
jtulach@52
    64
    
jtulach@52
    65
    /**
jtulach@52
    66
     * Creates an Element representing negation.
jtulach@52
    67
     *
jtulach@52
    68
     */
jtulach@52
    69
    public static Element createNot(final Element source1) {
jtulach@52
    70
        return new Element() {
jtulach@52
    71
            boolean evaluate(boolean[] inputs) {
jtulach@52
    72
                return !source1.evaluate(inputs);
jtulach@52
    73
            }
jtulach@52
    74
            
jtulach@52
    75
            int maxInput() {
jtulach@52
    76
                return source1.maxInput();
jtulach@52
    77
            }
jtulach@52
    78
        };
jtulach@52
    79
    }
jtulach@52
    80
    
jtulach@52
    81
    /**
jtulach@52
    82
     * Creates an Element representing input to the logical net.
jtulach@52
    83
     *
jtulach@52
    84
     */
jtulach@52
    85
    public static Element createInput(final int pin) {
jtulach@52
    86
        return new Element() {
jtulach@52
    87
            boolean evaluate(boolean[] inputs) {
jtulach@52
    88
                return inputs[pin];
jtulach@52
    89
            }
jtulach@52
    90
            
jtulach@52
    91
            int maxInput() {
jtulach@52
    92
                return pin;
jtulach@52
    93
            }
jtulach@52
    94
        };
jtulach@52
    95
    }
jtulach@52
    96
    
jtulach@52
    97
}