samples/apifest1/day1/welltestedsolution/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     
    26     public static Circuit createNotCircuit(Circuit in) {
    27         return new NotCircuit(in);
    28     }
    29     
    30     public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
    31         return new OrCircuit(in1, in2);
    32     }
    33     
    34     public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
    35         return new AndCircuit(in1, in2);
    36     }
    37     
    38     private Circuit() {
    39         
    40     }
    41     
    42     public abstract boolean evaluate(Boolean ... inputs);
    43     
    44     abstract int inputs();
    45     
    46     private static class NotCircuit extends Circuit {
    47 
    48         private Circuit in;
    49         
    50         NotCircuit (Circuit in) {
    51             this.in = in;
    52         }
    53         
    54         int inputs() {
    55             return in == null? 1: in.inputs();
    56         }
    57         
    58         public boolean evaluate (Boolean ... inputs) {
    59             if (inputs == null || inputs.length != inputs()) { 
    60                 throw new IllegalArgumentException();
    61             }
    62             Boolean o = inputs[0];
    63             if (in != null) { 
    64                 return !in.evaluate(inputs);
    65             }
    66             else if (o != null) {
    67                 return !o.booleanValue();
    68             }
    69             throw new IllegalArgumentException();
    70         }
    71     }
    72     
    73     private static class AndCircuit extends Circuit {
    74         private Circuit in1, in2;
    75         
    76         AndCircuit (Circuit in1, Circuit in2) {
    77             this.in1 = in1;
    78             this.in2 = in2;
    79         }
    80         
    81         int inputs() {
    82             return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
    83         }
    84         
    85         public boolean evaluate (Boolean ... inputs) {
    86             if (inputs == null || inputs.length != inputs()) { 
    87                 throw new IllegalArgumentException();
    88             }
    89             boolean x1, x2;
    90             Boolean o1 = inputs[0];
    91             if (in1 != null) { 
    92                 Boolean[] ins1 = new Boolean[in1.inputs()];
    93                 System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
    94                 x1 = in1.evaluate(ins1);
    95             }
    96             else if (o1 != null) {
    97                 x1 = o1.booleanValue();
    98             }
    99             else {
   100                 throw new IllegalArgumentException();
   101             }
   102             Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
   103             if (in2 != null) { 
   104                 Boolean[] ins2 = new Boolean[in2.inputs()];
   105                 System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
   106                 x2 = in2.evaluate(ins2);
   107             }
   108             else if (o2 != null) {
   109                 x2 = o2.booleanValue();
   110             }
   111             else {
   112                 throw new IllegalArgumentException();
   113             }
   114             return x1 && x2;
   115         }
   116     }
   117     
   118     private static class OrCircuit extends Circuit {
   119         private Circuit in1, in2;
   120         
   121         OrCircuit (Circuit in1, Circuit in2) {
   122             this.in1 = in1;
   123             this.in2 = in2;
   124         }
   125         
   126         int inputs() {
   127             return (in1 == null? 1: in1.inputs()) + (in2 == null? 1: in2.inputs()) ;
   128         }
   129         
   130         public boolean evaluate (Boolean ... inputs) {
   131             if (inputs == null || inputs.length != inputs()) { 
   132                 throw new IllegalArgumentException();
   133             }
   134             boolean x1, x2;
   135             Boolean o1 = inputs[0];
   136             if (in1 != null) { 
   137                 Boolean[] ins1 = new Boolean[in1.inputs()];
   138                 System.arraycopy(inputs, 0, ins1, 0, in1.inputs());
   139                 x1 = in1.evaluate(ins1);
   140             }
   141             else if (o1 != null) {
   142                 x1 = o1.booleanValue();
   143             }
   144             else {
   145                 throw new IllegalArgumentException();
   146             }
   147             Boolean o2 = inputs[in1 != null? in1.inputs(): 1];
   148             if (in2 != null) { 
   149                 Boolean[] ins2 = new Boolean[in2.inputs()];
   150                 System.arraycopy(inputs, in1 != null? in1.inputs(): 1, ins2, 0, in2.inputs());
   151                 x2 = in2.evaluate(ins2);
   152             }
   153             else if (o2 != null) {
   154                 x2 = o2.booleanValue();
   155             }
   156             else {
   157                 throw new IllegalArgumentException();
   158             }
   159             return x1 || x2;
   160         }
   161     }
   162 }