author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Thu, 30 Oct 2014 21:30:10 +0100 | |
changeset 409 | 40cabcdcd2be |
parent 177 | 67d6dceb1002 |
permissions | -rw-r--r-- |
jtulach@177 | 1 |
package org.apidesign.visitor; |
jtulach@177 | 2 |
|
jtulach@177 | 3 |
public final class Language { |
jtulach@177 | 4 |
private Language() { } |
jtulach@177 | 5 |
|
jtulach@177 | 6 |
public interface Expression { |
jtulach@177 | 7 |
public abstract void visit(Visitor v); |
jtulach@177 | 8 |
} |
jtulach@177 | 9 |
public interface Plus extends Expression { |
jtulach@177 | 10 |
public Expression getFirst(); |
jtulach@177 | 11 |
public Expression getSecond(); |
jtulach@177 | 12 |
} |
jtulach@177 | 13 |
public interface Number extends Expression { |
jtulach@177 | 14 |
public int getValue(); |
jtulach@177 | 15 |
} |
jtulach@177 | 16 |
/** @since 2.0 */ |
jtulach@177 | 17 |
public interface Minus extends Expression { |
jtulach@177 | 18 |
public Expression getFirst(); |
jtulach@177 | 19 |
public Expression getSecond(); |
jtulach@177 | 20 |
} |
jtulach@177 | 21 |
|
jtulach@177 | 22 |
// BEGIN: visitor.clientprovider.v3 |
jtulach@177 | 23 |
/** @since 3.0 */ |
jtulach@177 | 24 |
public interface Real extends Expression { |
jtulach@177 | 25 |
public double getValue(); |
jtulach@177 | 26 |
} |
jtulach@177 | 27 |
public static abstract class Visitor { |
jtulach@177 | 28 |
Visitor() {} |
jtulach@177 | 29 |
|
jtulach@177 | 30 |
|
jtulach@177 | 31 |
/** @since 3.0 */ |
jtulach@266 | 32 |
public static Visitor create(Version30 v) { |
jtulach@266 | 33 |
return create30(v); |
jtulach@177 | 34 |
} |
jtulach@177 | 35 |
|
jtulach@177 | 36 |
/** @since 3.0 */ |
jtulach@266 | 37 |
public interface Version30 { |
jtulach@177 | 38 |
public boolean visitUnknown(Expression e); |
jtulach@177 | 39 |
public void visitPlus(Plus s); |
jtulach@177 | 40 |
public void visitMinus(Minus s); |
jtulach@177 | 41 |
public void visitReal(Real r); |
jtulach@177 | 42 |
} |
jtulach@177 | 43 |
|
jtulach@177 | 44 |
/** @since 3.0 */ |
jtulach@177 | 45 |
public abstract void dispatchReal(Real r); |
jtulach@177 | 46 |
|
jtulach@177 | 47 |
// FINISH: visitor.clientprovider.v3 |
jtulach@177 | 48 |
/** @since 2.0 */ |
jtulach@266 | 49 |
public static Visitor create(Version20 v) { |
jtulach@266 | 50 |
return create20(v); |
jtulach@177 | 51 |
} |
jtulach@177 | 52 |
|
jtulach@177 | 53 |
/** @since 2.0 */ |
jtulach@266 | 54 |
public interface Version20 extends Version10 { |
jtulach@177 | 55 |
public void visitMinus(Minus m); |
jtulach@177 | 56 |
} |
jtulach@177 | 57 |
|
jtulach@177 | 58 |
|
jtulach@177 | 59 |
/** @since 2.0 */ |
jtulach@177 | 60 |
public abstract void dispatchNumber(Number n); |
jtulach@177 | 61 |
|
jtulach@266 | 62 |
public static Visitor create(Version10 v) { |
jtulach@266 | 63 |
return create10(v); |
jtulach@177 | 64 |
} |
jtulach@177 | 65 |
|
jtulach@266 | 66 |
public interface Version10 { |
jtulach@177 | 67 |
public boolean visitUnknown(Expression e); |
jtulach@177 | 68 |
public void visitPlus(Plus s); |
jtulach@177 | 69 |
public void visitNumber(Number n); |
jtulach@177 | 70 |
} |
jtulach@177 | 71 |
|
jtulach@177 | 72 |
public abstract void dispatchPlus(Plus p); |
jtulach@177 | 73 |
public abstract void dispatchMinus(Minus m); |
jtulach@177 | 74 |
} |
jtulach@177 | 75 |
|
jtulach@177 | 76 |
// BEGIN: visitor.clientprovider.dispatch.v3.l1 |
jtulach@266 | 77 |
static Visitor create10(final Visitor.Version10 v) { |
jtulach@177 | 78 |
return new Visitor() { |
jtulach@177 | 79 |
@Override |
jtulach@177 | 80 |
public void dispatchPlus(Plus p) { |
jtulach@177 | 81 |
v.visitPlus(p); |
jtulach@177 | 82 |
} |
jtulach@177 | 83 |
|
jtulach@177 | 84 |
@Override |
jtulach@177 | 85 |
public void dispatchNumber(Number n) { |
jtulach@177 | 86 |
v.visitNumber(n); |
jtulach@177 | 87 |
} |
jtulach@177 | 88 |
|
jtulach@177 | 89 |
@Override |
jtulach@177 | 90 |
public void dispatchMinus(Minus m) { |
jtulach@177 | 91 |
if (v.visitUnknown(m)) { |
jtulach@177 | 92 |
m.getFirst().visit(this); |
jtulach@177 | 93 |
m.getSecond().visit(this); |
jtulach@177 | 94 |
} |
jtulach@177 | 95 |
} |
jtulach@177 | 96 |
|
jtulach@177 | 97 |
@Override |
jtulach@177 | 98 |
public void dispatchReal(Real r) { |
jtulach@177 | 99 |
v.visitUnknown(r); |
jtulach@177 | 100 |
} |
jtulach@177 | 101 |
}; |
jtulach@177 | 102 |
} |
jtulach@177 | 103 |
// END: visitor.clientprovider.dispatch.v3.l1 |
jtulach@177 | 104 |
// BEGIN: visitor.clientprovider.dispatch.v3.l2 |
jtulach@266 | 105 |
static Visitor create20(final Visitor.Version20 v) { |
jtulach@177 | 106 |
return new Visitor() { |
jtulach@177 | 107 |
@Override |
jtulach@177 | 108 |
public void dispatchPlus(Plus p) { |
jtulach@177 | 109 |
v.visitPlus(p); |
jtulach@177 | 110 |
} |
jtulach@177 | 111 |
|
jtulach@177 | 112 |
@Override |
jtulach@177 | 113 |
public void dispatchNumber(Number n) { |
jtulach@177 | 114 |
v.visitNumber(n); |
jtulach@177 | 115 |
} |
jtulach@177 | 116 |
|
jtulach@177 | 117 |
@Override |
jtulach@177 | 118 |
public void dispatchMinus(Minus m) { |
jtulach@177 | 119 |
v.visitMinus(m); |
jtulach@177 | 120 |
} |
jtulach@177 | 121 |
|
jtulach@177 | 122 |
@Override |
jtulach@177 | 123 |
public void dispatchReal(Real r) { |
jtulach@177 | 124 |
v.visitUnknown(r); |
jtulach@177 | 125 |
} |
jtulach@177 | 126 |
}; |
jtulach@177 | 127 |
} |
jtulach@177 | 128 |
// END: visitor.clientprovider.dispatch.v3.l2 |
jtulach@177 | 129 |
// BEGIN: visitor.clientprovider.dispatch.v3.l3 |
jtulach@266 | 130 |
static Visitor create30(final Visitor.Version30 v) { |
jtulach@177 | 131 |
return new Visitor() { |
jtulach@177 | 132 |
@Override |
jtulach@177 | 133 |
public void dispatchReal(Real r) { |
jtulach@177 | 134 |
v.visitReal(r); |
jtulach@177 | 135 |
} |
jtulach@177 | 136 |
|
jtulach@177 | 137 |
@Override |
jtulach@177 | 138 |
public void dispatchNumber(final Number n) { |
jtulach@177 | 139 |
class RealWrapper implements Real { |
jtulach@177 | 140 |
public double getValue() { |
jtulach@177 | 141 |
return n.getValue(); |
jtulach@177 | 142 |
} |
jtulach@177 | 143 |
public void visit(Visitor v) { |
jtulach@177 | 144 |
n.visit(v); |
jtulach@177 | 145 |
} |
jtulach@177 | 146 |
} |
jtulach@177 | 147 |
v.visitReal(new RealWrapper()); |
jtulach@177 | 148 |
} |
jtulach@177 | 149 |
|
jtulach@177 | 150 |
@Override |
jtulach@177 | 151 |
public void dispatchPlus(Plus p) { |
jtulach@177 | 152 |
v.visitPlus(p); |
jtulach@177 | 153 |
} |
jtulach@177 | 154 |
|
jtulach@177 | 155 |
@Override |
jtulach@177 | 156 |
public void dispatchMinus(Minus m) { |
jtulach@177 | 157 |
v.visitMinus(m); |
jtulach@177 | 158 |
} |
jtulach@177 | 159 |
}; |
jtulach@177 | 160 |
} |
jtulach@177 | 161 |
// END: visitor.clientprovider.dispatch.v3.l3 |
jtulach@177 | 162 |
} |