samples/apifest1/day1/elementbasedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:47 +0200
changeset 53 a0b47629aad8
parent 52 4257f4cf226b
permissions -rw-r--r--
Including solutions till day 2
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@53
    24
// BEGIN: apifest.day1.elementbasedsolution.Circuit
jtulach@52
    25
public final class Circuit {
jtulach@52
    26
    private Circuit() {
jtulach@52
    27
    }
jtulach@52
    28
    
jtulach@52
    29
    public static Element and(final Element e1, final Element e2) {
jtulach@52
    30
        return new Element() {
jtulach@52
    31
            public boolean result() {
jtulach@52
    32
                return e1.result() && e2.result();
jtulach@52
    33
            }
jtulach@52
    34
        };
jtulach@52
    35
    }
jtulach@52
    36
    public static Element or(final Element e1, final Element e2) {
jtulach@52
    37
        return new Element() {
jtulach@52
    38
            public boolean result() {
jtulach@52
    39
                return e1.result() || e2.result();
jtulach@52
    40
            }
jtulach@52
    41
        };
jtulach@52
    42
    }
jtulach@52
    43
jtulach@52
    44
    public static Element not(final Element e1) {
jtulach@52
    45
        return new Element() {
jtulach@52
    46
            public boolean result() {
jtulach@52
    47
                return !e1.result();
jtulach@52
    48
            }
jtulach@52
    49
        };
jtulach@52
    50
    }
jtulach@52
    51
    
jtulach@52
    52
    public static Variable var() {
jtulach@52
    53
        return new Variable();
jtulach@52
    54
    }
jtulach@52
    55
    
jtulach@52
    56
    public static abstract class Element {
jtulach@52
    57
        private Element() {
jtulach@52
    58
        }
jtulach@52
    59
        
jtulach@52
    60
        public abstract boolean result();
jtulach@52
    61
    }
jtulach@52
    62
    
jtulach@52
    63
    public static final class Variable extends Element {
jtulach@52
    64
        private boolean value;
jtulach@52
    65
        
jtulach@52
    66
        public void assignValue(boolean b) {
jtulach@52
    67
            value = b;
jtulach@52
    68
        }
jtulach@52
    69
jtulach@52
    70
        public boolean result() {
jtulach@52
    71
            return value;
jtulach@52
    72
        }
jtulach@52
    73
    }
jtulach@52
    74
}
jtulach@53
    75
// END: apifest.day1.elementbasedsolution.Circuit