| author | Jaroslav Tulach <jtulach@netbeans.org> |
| Fri Aug 27 14:06:39 2010 +0200 | |
| changeset 365 | 0b7ec6ef8a72 |
| parent 175 | 7aaa7b256c7e |
| permissions | -rw-r--r-- |
| jtulach@170 | 1 |
package org.apidesign.visitor; |
| jtulach@170 | 2 |
|
| jtulach@170 | 3 |
public final class Language {
|
| jtulach@171 | 4 |
|
| jtulach@171 | 5 |
private Language() {
|
| jtulach@171 | 6 |
} |
| jtulach@171 | 7 |
|
| jtulach@170 | 8 |
public static abstract class Expression {
|
| jtulach@171 | 9 |
|
| jtulach@171 | 10 |
Expression() {
|
| jtulach@171 | 11 |
} |
| jtulach@171 | 12 |
|
| jtulach@170 | 13 |
public abstract void visit(Visitor v); |
| jtulach@170 | 14 |
} |
| jtulach@171 | 15 |
|
| jtulach@170 | 16 |
public static final class Plus extends Expression {
|
| jtulach@171 | 17 |
|
| jtulach@170 | 18 |
private final Expression first; |
| jtulach@170 | 19 |
private final Expression second; |
| jtulach@171 | 20 |
|
| jtulach@170 | 21 |
public Plus(Expression first, Expression second) {
|
| jtulach@170 | 22 |
this.first = first; |
| jtulach@170 | 23 |
this.second = second; |
| jtulach@170 | 24 |
} |
| jtulach@171 | 25 |
|
| jtulach@171 | 26 |
public Expression getFirst() {
|
| jtulach@171 | 27 |
return first; |
| jtulach@171 | 28 |
} |
| jtulach@171 | 29 |
|
| jtulach@171 | 30 |
public Expression getSecond() {
|
| jtulach@171 | 31 |
return second; |
| jtulach@171 | 32 |
} |
| jtulach@171 | 33 |
|
| jtulach@170 | 34 |
@Override |
| jtulach@171 | 35 |
public void visit(Visitor v) {
|
| jtulach@266 | 36 |
if (v instanceof Visitor10) {
|
| jtulach@266 | 37 |
((Visitor10) v).visitPlus(this); |
| jtulach@266 | 38 |
} else if (v instanceof Visitor30) {
|
| jtulach@266 | 39 |
((Visitor30) v).visitPlus(this); |
| jtulach@171 | 40 |
} else {
|
| jtulach@171 | 41 |
v.visitUnknown(this); |
| jtulach@171 | 42 |
} |
| jtulach@171 | 43 |
} |
| jtulach@170 | 44 |
} |
| jtulach@171 | 45 |
|
| jtulach@170 | 46 |
public static final class Number extends Expression {
|
| jtulach@171 | 47 |
|
| jtulach@170 | 48 |
private final int value; |
| jtulach@171 | 49 |
|
| jtulach@171 | 50 |
public Number(int value) {
|
| jtulach@171 | 51 |
this.value = value; |
| jtulach@171 | 52 |
} |
| jtulach@171 | 53 |
|
| jtulach@171 | 54 |
public int getValue() {
|
| jtulach@171 | 55 |
return value; |
| jtulach@171 | 56 |
} |
| jtulach@171 | 57 |
|
| jtulach@170 | 58 |
@Override |
| jtulach@171 | 59 |
public void visit(Visitor v) {
|
| jtulach@266 | 60 |
if (v instanceof Visitor10) {
|
| jtulach@266 | 61 |
((Visitor10) v).visitNumber(this); |
| jtulach@266 | 62 |
} else if (v instanceof Visitor30) {
|
| jtulach@173 | 63 |
Real wrapper = new Real(getValue()); |
| jtulach@266 | 64 |
((Visitor30) v).visitReal(wrapper); |
| jtulach@171 | 65 |
} else {
|
| jtulach@171 | 66 |
v.visitUnknown(this); |
| jtulach@171 | 67 |
} |
| jtulach@171 | 68 |
} |
| jtulach@170 | 69 |
} |
| jtulach@175 | 70 |
// BEGIN: visitor.nonmonotonic.Minus3 |
| jtulach@170 | 71 |
/** @since 2.0 */ |
| jtulach@175 | 72 |
public static final class Minus/*3.0*/ extends Expression {
|
| jtulach@170 | 73 |
private final Expression first; |
| jtulach@170 | 74 |
private final Expression second; |
| jtulach@170 | 75 |
|
| jtulach@170 | 76 |
public Minus(Expression first, Expression second) {
|
| jtulach@170 | 77 |
this.first = first; |
| jtulach@170 | 78 |
this.second = second; |
| jtulach@170 | 79 |
} |
| jtulach@170 | 80 |
public Expression getFirst() { return first; }
|
| jtulach@170 | 81 |
public Expression getSecond() { return second; }
|
| jtulach@170 | 82 |
|
| jtulach@170 | 83 |
public void visit(Visitor v) {
|
| jtulach@266 | 84 |
if (v instanceof Visitor20) {
|
| jtulach@266 | 85 |
((Visitor20)v).visitMinus(this); |
| jtulach@266 | 86 |
} else if (v instanceof Visitor30) {
|
| jtulach@266 | 87 |
((Visitor30)v).visitMinus(this); |
| jtulach@170 | 88 |
} else {
|
| jtulach@170 | 89 |
v.visitUnknown(this); |
| jtulach@170 | 90 |
} |
| jtulach@170 | 91 |
} |
| jtulach@170 | 92 |
} |
| jtulach@175 | 93 |
// END: visitor.nonmonotonic.Minus3 |
| jtulach@170 | 94 |
// BEGIN: visitor.nonmonotonic.real |
| jtulach@170 | 95 |
/** @since 3.0 */ |
| jtulach@172 | 96 |
public static final class Real extends Expression {
|
| jtulach@170 | 97 |
private final double value; |
| jtulach@170 | 98 |
public Real(double value) {
|
| jtulach@170 | 99 |
this.value = value; |
| jtulach@170 | 100 |
} |
| jtulach@170 | 101 |
public double getValue() {
|
| jtulach@170 | 102 |
return value; |
| jtulach@170 | 103 |
} |
| jtulach@170 | 104 |
public void visit(Visitor v) |
| jtulach@170 | 105 |
// FINISH: visitor.nonmonotonic.real |
| jtulach@170 | 106 |
{
|
| jtulach@266 | 107 |
if (v instanceof Visitor30) {
|
| jtulach@266 | 108 |
((Visitor30)v).visitReal(this); |
| jtulach@174 | 109 |
} else {
|
| jtulach@174 | 110 |
v.visitUnknown(this); |
| jtulach@174 | 111 |
} |
| jtulach@170 | 112 |
} |
| jtulach@170 | 113 |
} |
| jtulach@170 | 114 |
|
| jtulach@170 | 115 |
|
| jtulach@170 | 116 |
public interface Visitor {
|
| jtulach@170 | 117 |
public void visitUnknown(Expression e); |
| jtulach@170 | 118 |
} |
| jtulach@266 | 119 |
public interface Visitor10 extends Visitor {
|
| jtulach@170 | 120 |
public void visitPlus(Plus s); |
| jtulach@170 | 121 |
public void visitNumber(Number n); |
| jtulach@170 | 122 |
} |
| jtulach@170 | 123 |
/** @since 2.0 */ |
| jtulach@266 | 124 |
public interface Visitor20 extends Visitor {
|
| jtulach@170 | 125 |
public void visitMinus(Minus s); |
| jtulach@170 | 126 |
} |
| jtulach@170 | 127 |
// BEGIN: visitor.nonmonotonic.visitor |
| jtulach@170 | 128 |
/** @since 3.0 */ |
| jtulach@266 | 129 |
public interface Visitor30 extends Visitor {
|
| jtulach@170 | 130 |
public void visitPlus(Plus s); |
| jtulach@170 | 131 |
public void visitMinus(Minus s); |
| jtulach@172 | 132 |
public void visitReal(Real r); |
| jtulach@170 | 133 |
} |
| jtulach@170 | 134 |
// END: visitor.nonmonotonic.visitor |
| jtulach@170 | 135 |
} |