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
jtulach@52
     1
/*
jtulach@52
     2
 * The contents of this file are subject to the terms of the Common Development
jtulach@52
     3
 * and Distribution License (the License). You may not use this file except in
jtulach@52
     4
 * compliance with the License.
jtulach@52
     5
 *
jtulach@52
     6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
jtulach@52
     7
 * or http://www.netbeans.org/cddl.txt.
jtulach@52
     8
 *
jtulach@52
     9
 * When distributing Covered Code, include this CDDL Header Notice in each file
jtulach@52
    10
 * and include the License file at http://www.netbeans.org/cddl.txt.
jtulach@52
    11
 * If applicable, add the following below the CDDL Header, with the fields
jtulach@52
    12
 * enclosed by brackets [] replaced by your own identifying information:
jtulach@52
    13
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@52
    14
 *
jtulach@52
    15
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@52
    16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@52
    17
 * Microsystems, Inc. All Rights Reserved.
jtulach@52
    18
 */
jtulach@52
    19
jtulach@52
    20
package org.netbeans.apifest.boolcircuit;
jtulach@52
    21
jtulach@52
    22
/**
jtulach@52
    23
 */
jtulach@52
    24
public abstract class Circuit {
jtulach@52
    25
    /** Creates a new instance of Circuits */
jtulach@52
    26
    private Circuit() {
jtulach@52
    27
    }
jtulach@52
    28
jtulach@52
    29
    public static Circuit negate(boolean input) {
jtulach@52
    30
        return new Negation(input);
jtulach@52
    31
    }
jtulach@52
    32
jtulach@52
    33
    public static Circuit negate(Circuit input) {
jtulach@52
    34
        return new Negation(input);
jtulach@52
    35
    }
jtulach@52
    36
jtulach@52
    37
    public static Circuit and(boolean input1, boolean input2) {
jtulach@52
    38
        return new And(input1, input2);
jtulach@52
    39
    }
jtulach@52
    40
jtulach@52
    41
    public static Circuit and(Circuit  input1, boolean input2) {
jtulach@52
    42
        return new And(input1, new Primitive(input2));
jtulach@52
    43
    }
jtulach@52
    44
jtulach@52
    45
    public static Circuit and(boolean input1, Circuit input2) {
jtulach@52
    46
        return new And(new Primitive(input1), input2);
jtulach@52
    47
    }
jtulach@52
    48
jtulach@52
    49
    public static Circuit and(Circuit input1, Circuit input2) {
jtulach@52
    50
        return new And(input1, input2);
jtulach@52
    51
    }
jtulach@52
    52
jtulach@52
    53
    public static Circuit or(boolean input1, boolean input2) {
jtulach@52
    54
        return new Or(input1, input2);
jtulach@52
    55
    }
jtulach@52
    56
jtulach@52
    57
    public static Circuit or(Circuit input1, boolean input2) {
jtulach@52
    58
        return new Or(input1, new Primitive(input2));
jtulach@52
    59
    }
jtulach@52
    60
jtulach@52
    61
    public static Circuit or(boolean input1, Circuit input2) {
jtulach@52
    62
        return new Or(new Primitive(input1), input2);
jtulach@52
    63
    }
jtulach@52
    64
jtulach@52
    65
    public static Circuit or(Circuit input1, Circuit input2) {
jtulach@52
    66
        return new Or(input1, input2);
jtulach@52
    67
    }
jtulach@52
    68
jtulach@52
    69
jtulach@52
    70
    public abstract boolean output();
jtulach@52
    71
jtulach@52
    72
    private final static class Primitive extends Circuit {
jtulach@52
    73
        private boolean input;
jtulach@52
    74
jtulach@52
    75
        Primitive(boolean input) {
jtulach@52
    76
            this.input = input;
jtulach@52
    77
        }
jtulach@52
    78
jtulach@52
    79
        public final boolean output() {
jtulach@52
    80
            return input;
jtulach@52
    81
        }
jtulach@52
    82
    }
jtulach@52
    83
jtulach@52
    84
    private final static class Negation extends Circuit {
jtulach@52
    85
        private Circuit input;
jtulach@52
    86
jtulach@52
    87
        Negation(boolean input) {
jtulach@52
    88
            this.input = new Primitive(input);
jtulach@52
    89
        }
jtulach@52
    90
jtulach@52
    91
        Negation(Circuit input) {
jtulach@52
    92
            this.input = input;
jtulach@52
    93
        }
jtulach@52
    94
jtulach@52
    95
        public final boolean output() {
jtulach@52
    96
            return !(input.output());
jtulach@52
    97
        }
jtulach@52
    98
    }
jtulach@52
    99
jtulach@52
   100
    private final static class And extends Circuit {
jtulach@52
   101
        private Circuit input1;
jtulach@52
   102
        private Circuit input2;
jtulach@52
   103
jtulach@52
   104
jtulach@52
   105
        And(boolean input1, boolean input2) {
jtulach@52
   106
            this.input1 = new Primitive(input1);
jtulach@52
   107
            this.input2 = new Primitive(input2);
jtulach@52
   108
        }
jtulach@52
   109
jtulach@52
   110
        And(Circuit input1, Circuit input2) {
jtulach@52
   111
            this.input1 = input1;
jtulach@52
   112
            this.input2 = input2;
jtulach@52
   113
        }
jtulach@52
   114
jtulach@52
   115
        public final boolean output() {
jtulach@52
   116
            return input1.output() && input2.output();
jtulach@52
   117
        }
jtulach@52
   118
    }
jtulach@52
   119
jtulach@52
   120
    private final static class Or extends Circuit {
jtulach@52
   121
        private Circuit input1;
jtulach@52
   122
        private Circuit input2;
jtulach@52
   123
jtulach@52
   124
jtulach@52
   125
        Or(boolean input1, boolean input2) {
jtulach@52
   126
            this.input1 = new Primitive(input1);
jtulach@52
   127
            this.input2 = new Primitive(input2);
jtulach@52
   128
        }
jtulach@52
   129
jtulach@52
   130
        Or(Circuit input1, Circuit input2) {
jtulach@52
   131
            this.input1 = input1;
jtulach@52
   132
            this.input2 = input2;
jtulach@52
   133
        }
jtulach@52
   134
jtulach@52
   135
        public final boolean output() {
jtulach@52
   136
            return input1.output() || input2.output();
jtulach@52
   137
        }
jtulach@52
   138
    }
jtulach@52
   139
  }