SE450: Composite: N-ary Ops [17/24] Previous pageContentsNext page

file:composite/four/Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package composite.four;
public class Main {
  public static void main(String[] args) {
    Expr onePtwo = ExprFactory.newPlus
        (ExprFactory.newConst(1), ExprFactory.newConst(2));

    ExprBuilder eb = ExprFactory.newMultBuilder();
    eb.add(ExprFactory.newConst(3));
    eb.add(ExprFactory.newConst(4));
    eb.add(onePtwo);
    Expr multiplies = eb.toExpr();
    Expr m = ExprFactory.newMinus (onePtwo, multiplies);

    System.out.println(m);
    System.out.println("Value: " + m.eval());
  }
}

file:composite/four/ExprBuilder.java [source] [doc-public] [doc-private]
01
02
03
04
05
package composite.four;
public interface ExprBuilder {
  Expr toExpr();
  void add(Expr e);
}

file:composite/four/ExprFactory.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package composite.four;
import java.util.List;
import java.util.LinkedList;

public class ExprFactory {
  private ExprFactory() {}
  static public Expr newConst(int v) {
    return new Const(v);
  }
  static public Expr newPlus(Expr l, Expr r) {
    return new BinOp(l, new OpAdd(), r);
  }
  static public Expr newMinus(Expr l, Expr r) {
    return new BinOp(l, new OpSub(), r);
  }
  static public Expr newMult(Expr l, Expr r) {
    return new BinOp(l, new OpMul(), r);
  }
  static public Expr newQuot(Expr l, Expr r) {
    return new BinOp(l, new OpDiv(), r);
  }
  static public ExprBuilder newPlusBuilder() {
    return new NaryOpBuilder(new OpAdd(), new Const(0));
  }
  static public ExprBuilder newMultBuilder() {
    return new NaryOpBuilder(new OpMul(), new Const(1));
  }
}

final class Const implements Expr {
  private final int v;
  public Const(int v) {
    this.v = v;
  }
  public int eval() {
    return v;
  }
  public String toString() {
    return Integer.toString(v);
  }
}

final class BinOp implements Expr {
  private final Expr l;
  private final Expr r;
  private final Op op;
  public BinOp(Expr l, Op op, Expr r) {
    if ((l == null) || (op == null) || (r == null)) {
      throw new IllegalArgumentException();
    }
    this.op = op;
    this.l = l;
    this.r = r;
  }
  public int eval() {
    return op.eval(l.eval(), r.eval());
  }
  public String toString() {
    return l.toString() + " " + r.toString() + " " + op.toString();
  }
}

final class NaryOp implements Expr {
  private final Expr[] args;
  private final Expr zero;
  private final Op op;
  public NaryOp(Expr[] args, Op op, Expr zero) {
    // Don't need to test these, since the builder checks them.
    //     if ((args == null) || (op == null) || (zero == null))
    //       throw new IllegalArgumentException();
    //     for (int i=0; i<args.length; i++)
    //       if (args[i] == null)
    //         throw new IllegalArgumentException();
    this.op = op;
    this.args = args;
    this.zero = zero;
  }
  public int eval() {
    int result = zero.eval();
    for (int i=0; i<args.length; i++)
      result = op.eval(result, args[i].eval());
    return result;
  }
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int i=0; i<args.length; i++) {
      sb.append(args[i].toString());
      if (i+1<args.length)
        sb.append(", ");
    }
    sb.append("]");
    sb.append(op.toString());
    sb.append(" ");
    return sb.toString();
  }
}

final class NaryOpBuilder implements ExprBuilder {
  private final List<Expr> args;
  private final Expr zero;
  private final Op op;
  public NaryOpBuilder(Op op, Expr zero) {
    if ((op == null) || (zero == null))
      throw new IllegalArgumentException();
    this.args = new LinkedList<Expr>();
    this.op = op;
    this.zero = zero;
  }
  public void add(Expr e) {
    if (e == null)
      throw new IllegalArgumentException();
    args.add(e);
  }
  public Expr toExpr() {
    return new NaryOp(args.toArray(new Expr[0]), op, zero);
  }
}

interface Op {
  public abstract int eval(int x, int y);
}
final class OpAdd implements Op {
  public String toString() { return "+"; }
  public int eval(int x, int y) { return x+y; }
}
final class OpSub implements Op {
  public String toString() { return "-"; }
  public int eval(int x, int y) { return x-y; }
}
final class OpMul implements Op {
  public String toString() { return "*"; }
  public int eval(int x, int y) { return x*y; }
}
final class OpDiv implements Op {
  public String toString() { return "/"; }
  public int eval(int x, int y) { return x/y; }
}

Previous pageContentsNext page