Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Java is nuts

By: a guest on Dec 26th, 2012  |  syntax: Java  |  size: 2.31 KB  |  hits: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public interface Formula<T> {
  2.  
  3.     public <R> visit(Visitor<T,R> visitor);
  4.  
  5.     /**
  6.      * A Visitor to perform case analysis on Formula<T>.  This is
  7.      * intended to be exhaustive; an implementation of this
  8.      * interface must say how to handle all of the subcaes.  The <R>
  9.      * variable is the result type of the Visitor.
  10.      */
  11.     public static interface Visitor<T, R> {
  12.         public R visitVariable(Variable<T> var);
  13.         public R visitNot(Not<T> var);
  14.         public R visitAnd(And<T> var);
  15.         public R visitOr(Or<T> var);
  16.     }
  17.  
  18.     public static final class Variable<T> implements Formula<T> {
  19.         private final T tag;
  20.  
  21.         public Variable(T tag) {
  22.             this.tag = tag;
  23.         }
  24.  
  25.         public T getTag() {
  26.             return tag;
  27.         }
  28.  
  29.         public <R> visit(Visitor<T,R> visitor) {
  30.             visitor.visitVariable(this);
  31.         }
  32.  
  33.     }
  34.  
  35.     public static final class Not<T> implements Formula<T> {
  36.         private final Formula<T> subformula;
  37.  
  38.         public Not(Formula<T> subformula) {
  39.             this.subformula = subformula;
  40.         }
  41.  
  42.         public Formula<T> getSubformula() {
  43.             return subformula;
  44.         }
  45.  
  46.         public <R> visit(Visitor<T,R> visitor) {
  47.             visitor.visitNot(this);
  48.         }
  49.  
  50.     }
  51.  
  52.     public static final class And<T> implements Formula<T> {
  53.         private final Formula<T> left;
  54.         private final Formula<T> right;
  55.  
  56.         public And(Formula<T> left, Formula<T> right) {
  57.             this.left = left;
  58.         }
  59.  
  60.         public Formula<T> getLeft() {
  61.             return left;
  62.         }
  63.  
  64.         public Formula<T> getRight() {
  65.             return right;
  66.         }
  67.  
  68.         public <R> visit(Visitor<T,R> visitor) {
  69.             visitor.visitAnd(this);
  70.         }
  71.  
  72.     }
  73.  
  74.     public static final class Or<T> implements Formula<T> {
  75.         private final Formula<T> left;
  76.         private final Formula<T> right;
  77.  
  78.         public Or(Formula<T> left, Formula<T> right) {
  79.             this.left = left;
  80.         }
  81.  
  82.         public Formula<T> getLeft() {
  83.             return left;
  84.         }
  85.  
  86.         public Formula<T> getRight() {
  87.             return right;
  88.         }
  89.  
  90.         public <R> visit(Visitor<T,R> visitor) {
  91.             visitor.visitOr(this);
  92.         }
  93.  
  94.     }
  95.  
  96. }