samples/apifest1/day1/stackbasedsolution/src/org/netbeans/apifest/boolcircuit/Operation.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  * Operation.java
     3  *
     4  * Created on July 12, 2006, 3:06 PM
     5  *
     6  * To change this template, choose Tools | Template Manager
     7  * and open the template in the editor.
     8  */
     9 
    10 package org.netbeans.apifest.boolcircuit;
    11 
    12 /**
    13  *
    14  */
    15 public interface Operation {
    16     public char evaluate (char i1, char i2) throws IllegalArgumentException;
    17     public final static Operation OR = new Or ();
    18     public final static Operation AND = new And ();
    19     public final static Operation NEG = new Neg ();
    20     
    21     
    22     static class And implements Operation {
    23         public char evaluate(char i1, char i2) throws IllegalArgumentException {
    24             if (i1 != '0' && i1 != '1') {
    25                 throw new IllegalArgumentException ("Invalid input parameter: " + i1);
    26             }
    27             if (i2 != '0' && i2 != '1') {
    28                 throw new IllegalArgumentException ("Invalid input parameter: " + i2);
    29             }
    30             return i1 == '1' && i2 == '1' ? '1' : '0';
    31         }
    32     }
    33     static class Or implements Operation {
    34         public char evaluate(char i1, char i2) throws IllegalArgumentException {
    35             if (i1 != '0' && i1 != '1') {
    36                 throw new IllegalArgumentException ("Invalid input parameter: " + i1);
    37             }
    38             if (i2 != '0' && i2 != '1') {
    39                 throw new IllegalArgumentException ("Invalid input parameter: " + i2);
    40             }
    41             return i1 == '1' || i2 == '1' ? '1' : '0';
    42         }
    43     }
    44     static class Neg implements Operation {
    45         public char evaluate(char i1, char i2) throws IllegalArgumentException {
    46             if (i1 != '0' && i1 != '1') {
    47                 throw new IllegalArgumentException ("Invalid input parameter: " + i1);
    48             }
    49             return i1 == '1' ? '0' : '1';
    50         }
    51     }
    52 }
    53