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
     1 /*
     2  * Element.java
     3  *
     4  * Created on 12. Ĩervenec 2006, 14:14
     5  *
     6  * To change this template, choose Tools | Template Manager
     7  * and open the template in the editor.
     8  */
     9 
    10 package org.netbeans.apifest.boolcircuit;
    11 
    12 /**
    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[])}
    19  * factory method.
    20  */
    21 public abstract class Element {
    22         
    23     /** Creates a new instance of Element */
    24     private Element() {
    25     }
    26     
    27     /**
    28       */
    29     abstract boolean evaluate(boolean[] inputs);
    30     
    31     abstract int maxInput();
    32     
    33     /**
    34      * Creates an Element representing 2-input AND function.
    35      *
    36      */
    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);
    41             }
    42             
    43             int maxInput() {
    44                 return Math.max(source1.maxInput(), source2.maxInput());
    45             }
    46         };
    47     }
    48 
    49     /**
    50      * Creates an Element representing 2-input OR function.
    51      *
    52      */
    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);
    57             }
    58             
    59             int maxInput() {
    60                 return Math.max(source1.maxInput(), source2.maxInput());
    61             }
    62         };
    63     }
    64     
    65     /**
    66      * Creates an Element representing negation.
    67      *
    68      */
    69     public static Element createNot(final Element source1) {
    70         return new Element() {
    71             boolean evaluate(boolean[] inputs) {
    72                 return !source1.evaluate(inputs);
    73             }
    74             
    75             int maxInput() {
    76                 return source1.maxInput();
    77             }
    78         };
    79     }
    80     
    81     /**
    82      * Creates an Element representing input to the logical net.
    83      *
    84      */
    85     public static Element createInput(final int pin) {
    86         return new Element() {
    87             boolean evaluate(boolean[] inputs) {
    88                 return inputs[pin];
    89             }
    90             
    91             int maxInput() {
    92                 return pin;
    93             }
    94         };
    95     }
    96     
    97 }