Skip to content

Instantly share code, notes, and snippets.

@samleb
Created April 29, 2009 12:47
Show Gist options
  • Save samleb/103747 to your computer and use it in GitHub Desktop.
Save samleb/103747 to your computer and use it in GitHub Desktop.
public interface ICondition<T> {
boolean isSatisfiedBy(T object);
ICondition<T> negate();
ICondition<T> and(ICondition<T> other);
ICondition<T> or(ICondition<T> other);
}
package com.maptimize.conditions;
public abstract class AbstractCondition<T> implements ICondition<T> {
public ICondition<T> negate() {
// final ICondition<T> self = this;
return new AbstractCondition<T>() {
public boolean isSatisfiedBy(T object) {
// Does the same thing, surprisingly
return !AbstractCondition.this.isSatisfiedBy(object);
}
};
}
public ICondition<T> and(final ICondition<T> other) {
// final ICondition<T> self = this;
return new AbstractCondition<T>() {
public boolean isSatisfiedBy(T object) {
return AbstractCondition.this.isSatisfiedBy(object) && other.isSatisfiedBy(object);
}
};
}
public ICondition<T> or(final ICondition<T> other) {
// final ICondition<T> self = this;
return new AbstractCondition<T>() {
public boolean isSatisfiedBy(T object) {
return AbstractCondition.this.isSatisfiedBy(object) || other.isSatisfiedBy(object);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment