samples/apifest1/day1/alwayscreatenewcircuit/src/org/netbeans/apifest/boolcircuit/Circuit.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  * 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.
     5  *
     6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
     7  * or http://www.netbeans.org/cddl.txt.
     8  *
     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]"
    14  *
    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.
    18  */
    19 
    20 package org.netbeans.apifest.boolcircuit;
    21 
    22 /**
    23  */
    24 public abstract class Circuit {
    25     /** Creates a new instance of Circuits */
    26     private Circuit() {
    27     }
    28 
    29     public static Circuit negate(boolean input) {
    30         return new Negation(input);
    31     }
    32 
    33     public static Circuit negate(Circuit input) {
    34         return new Negation(input);
    35     }
    36 
    37     public static Circuit and(boolean input1, boolean input2) {
    38         return new And(input1, input2);
    39     }
    40 
    41     public static Circuit and(Circuit  input1, boolean input2) {
    42         return new And(input1, new Primitive(input2));
    43     }
    44 
    45     public static Circuit and(boolean input1, Circuit input2) {
    46         return new And(new Primitive(input1), input2);
    47     }
    48 
    49     public static Circuit and(Circuit input1, Circuit input2) {
    50         return new And(input1, input2);
    51     }
    52 
    53     public static Circuit or(boolean input1, boolean input2) {
    54         return new Or(input1, input2);
    55     }
    56 
    57     public static Circuit or(Circuit input1, boolean input2) {
    58         return new Or(input1, new Primitive(input2));
    59     }
    60 
    61     public static Circuit or(boolean input1, Circuit input2) {
    62         return new Or(new Primitive(input1), input2);
    63     }
    64 
    65     public static Circuit or(Circuit input1, Circuit input2) {
    66         return new Or(input1, input2);
    67     }
    68 
    69 
    70     public abstract boolean output();
    71 
    72     private final static class Primitive extends Circuit {
    73         private boolean input;
    74 
    75         Primitive(boolean input) {
    76             this.input = input;
    77         }
    78 
    79         public final boolean output() {
    80             return input;
    81         }
    82     }
    83 
    84     private final static class Negation extends Circuit {
    85         private Circuit input;
    86 
    87         Negation(boolean input) {
    88             this.input = new Primitive(input);
    89         }
    90 
    91         Negation(Circuit input) {
    92             this.input = input;
    93         }
    94 
    95         public final boolean output() {
    96             return !(input.output());
    97         }
    98     }
    99 
   100     private final static class And extends Circuit {
   101         private Circuit input1;
   102         private Circuit input2;
   103 
   104 
   105         And(boolean input1, boolean input2) {
   106             this.input1 = new Primitive(input1);
   107             this.input2 = new Primitive(input2);
   108         }
   109 
   110         And(Circuit input1, Circuit input2) {
   111             this.input1 = input1;
   112             this.input2 = input2;
   113         }
   114 
   115         public final boolean output() {
   116             return input1.output() && input2.output();
   117         }
   118     }
   119 
   120     private final static class Or extends Circuit {
   121         private Circuit input1;
   122         private Circuit input2;
   123 
   124 
   125         Or(boolean input1, boolean input2) {
   126             this.input1 = new Primitive(input1);
   127             this.input2 = new Primitive(input2);
   128         }
   129 
   130         Or(Circuit input1, Circuit input2) {
   131             this.input1 = input1;
   132             this.input2 = input2;
   133         }
   134 
   135         public final boolean output() {
   136             return input1.output() || input2.output();
   137         }
   138     }
   139   }