samples/apifest1/day1/stackbasedsolution/src/org/netbeans/apifest/boolcircuit/Operation.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
4 * Created on July 12, 2006, 3:06 PM
6 * To change this template, choose Tools | Template Manager
7 * and open the template in the editor.
10 package org.netbeans.apifest.boolcircuit;
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 ();
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);
27 if (i2 != '0' && i2 != '1') {
28 throw new IllegalArgumentException ("Invalid input parameter: " + i2);
30 return i1 == '1' && i2 == '1' ? '1' : '0';
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);
38 if (i2 != '0' && i2 != '1') {
39 throw new IllegalArgumentException ("Invalid input parameter: " + i2);
41 return i1 == '1' || i2 == '1' ? '1' : '0';
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);
49 return i1 == '1' ? '0' : '1';