samples/apifest1/day2/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 import java.util.ArrayList;
    23 import java.util.Arrays;
    24 import java.util.LinkedHashSet;
    25 
    26 /**
    27  */
    28 public abstract class Circuit {
    29     /** Creates a new instance of Circuits */
    30     protected Circuit() {
    31     }
    32 
    33     public static Circuit negate(Input input) {
    34         return new Negation(new Primitive(input));
    35     }
    36     
    37     @Deprecated
    38     public static Circuit negate(boolean input) {
    39         return new Negation(input);
    40     }
    41     
    42     public static Circuit negate(Circuit input) {
    43         return new Negation(input);
    44     }
    45 
    46     public static Circuit and(Input input1, Input input2) {
    47         return new And(new Primitive(input1), new Primitive(input2));
    48     }
    49     
    50     @Deprecated
    51     public static Circuit and(boolean input1, boolean input2) {
    52         return new And(input1, input2);
    53     }
    54 
    55     public static Circuit and(Circuit  input1, Input input2) {
    56         return new And(input1, new Primitive(input2));
    57     }
    58     
    59     @Deprecated
    60     public static Circuit and(Circuit  input1, boolean input2) {
    61         return new And(input1, new Primitive(input2));
    62     }
    63 
    64     public static Circuit and(Input input1, Circuit input2) {
    65         return new And(new Primitive(input1), input2);
    66     }
    67     
    68     @Deprecated
    69     public static Circuit and(boolean input1, Circuit input2) {
    70         return new And(new Primitive(input1), input2);
    71     }
    72     
    73     public static Circuit and(Circuit input1, Circuit input2) {
    74         return new And(input1, input2);
    75     }
    76 
    77     public static Circuit or(Input input1, Input input2) {
    78         return new Or(new Primitive(input1), new Primitive(input2));
    79     }
    80     
    81     @Deprecated
    82     public static Circuit or(boolean input1, boolean input2) {
    83         return new Or(input1, input2);
    84     }
    85 
    86     public static Circuit or(Circuit input1, Input input2) {
    87         return new Or(input1, new Primitive(input2));
    88     }
    89     
    90     @Deprecated
    91     public static Circuit or(Circuit input1, boolean input2) {
    92         return new Or(input1, new Primitive(input2));
    93     }
    94 
    95     public static Circuit or(Input input1, Circuit input2) {
    96         return new Or(new Primitive(input1), input2);
    97     }
    98     
    99     @Deprecated
   100     public static Circuit or(boolean input1, Circuit input2) {
   101         return new Or(new Primitive(input1), input2);
   102     }
   103     
   104     public static Circuit or(Circuit input1, Circuit input2) {
   105         return new Or(input1, input2);
   106     }
   107     
   108     
   109     public  final boolean output() {
   110         double v = value();
   111         if (v > 1 || v < 0) {
   112             throw new IllegalArgumentException();
   113         }
   114         return (v > 0) ? true : false;
   115     }
   116     
   117     private static final double check(double v) {
   118         if (v > 1 || v < 0) {
   119             throw new IllegalArgumentException();
   120         }
   121         return v;
   122     }
   123     
   124     
   125     public abstract double value();
   126     
   127     private final static class Primitive extends Circuit {
   128         private Input input;
   129 
   130         Primitive(boolean input) {
   131             this.input = new Input(input);
   132         }
   133         
   134         Primitive(Input input) {
   135             this.input = input;
   136         }
   137         
   138         
   139 
   140         public double value() {
   141             return input.getValue();
   142         }
   143     }
   144     
   145     private final static class Negation extends Circuit {
   146         private Circuit input;
   147         
   148         @Deprecated
   149         Negation(boolean input) {
   150             this.input = new Primitive(input);
   151         }
   152         
   153         Negation(Circuit input) {
   154             this.input = input;
   155         }
   156         
   157 
   158         public double value() {
   159             double x = input.value();            
   160             return check(1-x);
   161         }
   162     }
   163     
   164     private static class And extends Circuit {
   165         Circuit input1;
   166         Circuit input2;
   167                 
   168         And(boolean input1, boolean input2) {
   169             this.input1 = new Primitive(input1);
   170             this.input2 = new Primitive(input2);
   171         }
   172         
   173         And(Circuit input1, Circuit input2) {
   174             this.input1 = input1;
   175             this.input2 = input2;
   176         }
   177         
   178         public double value() {
   179             double x = input1.value();
   180             double y = input2.value();            
   181             return check(x * y);
   182         }
   183     }
   184     
   185     private final static class Or extends And {        
   186         Or(boolean input1, boolean input2) {
   187             super(input1, input2);
   188         }
   189         
   190         Or(Circuit input1, Circuit input2) {
   191             super(input1, input2);
   192         }
   193         
   194         public double value() {
   195             double x = input1.value();
   196             double y = input2.value();
   197             
   198             return check(1 - (1 - x) * (1 - y));
   199         }        
   200     }
   201     
   202     public static class Input {
   203         private double value;
   204         
   205         public static Input valueOf(boolean initValue) {
   206             return new Input(initValue);
   207         }       
   208 
   209         public static Input valueOf(double initValue) {
   210             return new Input(initValue);
   211         }       
   212                         
   213         /** Creates a new instance of Input */        
   214         private Input(boolean input) {
   215             setValue(input);
   216         }
   217         
   218         private Input(double input) {
   219             setValue(input);
   220         }
   221         
   222         public double getValue() {
   223             return value;
   224         }
   225         
   226         public void setValue(double input) {
   227             value = input;
   228         }
   229         
   230         public void setValue(boolean input) {
   231             value = (input) ? 1 : 0;
   232         }        
   233     }    
   234 }