Skip to content

Instantly share code, notes, and snippets.

@cokeSchlumpf
Created July 16, 2013 21:42
Show Gist options
  • Save cokeSchlumpf/6015417 to your computer and use it in GitHub Desktop.
Save cokeSchlumpf/6015417 to your computer and use it in GitHub Desktop.
Logische Algebra Java ..
// === AND.java ===
package com.ibm.fp.java.patternmatching;
public final class AND extends BinaryExpression {
public AND(Expression left, Expression right) {
super(left, right);
}
}
// === BinaryExpression.java ===
package com.ibm.fp.java.patternmatching;
public abstract class BinaryExpression implements Expression {
private final Expression left;
private final Expression right;
public BinaryExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
public Expression getLeft() {
return this.left;
}
public Expression getRight() {
return this.right;
}
public String toString() {
return this.getClass().getSimpleName() + "(" + this.getLeft().toString() + "," + this.getRight().toString() + ")";
}
}
// === Expression.java ===
package com.ibm.fp.java.patternmatching;
public interface Expression { }
// === FALSE.java ===
package com.ibm.fp.java.patternmatching;
public class FALSE implements Expression {
private static FALSE instance;
private FALSE() {
}
public static FALSE getInstance() {
if (instance == null) {
instance = new FALSE();
}
return instance;
}
@Override
public String toString() {
return "FALSE";
}
}
// === NOT.java ===
package com.ibm.fp.java.patternmatching;
public final class NOT extends UnaryExpression {
public NOT(Expression value) {
super(value);
}
}
// === OR.java ===
package com.ibm.fp.java.patternmatching;
public class OR extends BinaryExpression {
public OR(Expression left, Expression right) {
super(left, right);
}
}
// === PatternMatching.java ===
package com.ibm.fp.java.patternmatching;
public class PatternMatching {
/**
* @param args
*/
public static void main(String[] args) {
new PatternMatching();
}
private final Expression a = new NOT(new AND(TRUE.getInstance(), new NOT(new OR(FALSE.getInstance(), TRUE.getInstance()))));
public PatternMatching() {
System.out.println("a = " + a);
System.out.println("eval(a) = " + eval(a));
}
private boolean eval(Expression e) {
if (e instanceof AND) {
AND and = (AND) e;
return eval(and.getLeft()) && eval(and.getRight());
} else if (e instanceof OR) {
OR or = (OR) e;
return eval(or.getLeft()) || eval(or.getRight());
} else if (e instanceof NOT) {
return !eval(((NOT) e).getValue());
} else if (e == TRUE.getInstance()) {
return true;
} else if (e == FALSE.getInstance()) {
return false;
} else {
throw new IllegalArgumentException("Unknown Expression.");
}
}
}
// === TRUE ===
package com.ibm.fp.java.patternmatching;
public class TRUE implements Expression {
private static TRUE instance;
private TRUE() {
}
public static TRUE getInstance() {
if (instance == null) {
instance = new TRUE();
}
return instance;
}
@Override
public String toString() {
return "TRUE";
}
}
// === UnaryExpression.java ===
package com.ibm.fp.java.patternmatching;
public abstract class UnaryExpression implements Expression {
private final Expression value;
public UnaryExpression(Expression value) {
this.value = value;
}
public Expression getValue() {
return this.value;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "(" + this.getValue() + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment