org.squirrelframework.foundation.fsm.Conditions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squirrel-foundation Show documentation
Show all versions of squirrel-foundation Show documentation
foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.
package org.squirrelframework.foundation.fsm;
import java.util.List;
/**
* Constants conditions
*
* @author Henry.He
*
*/
public class Conditions {
public static class Always implements Condition {
@Override
public boolean isSatisfied(C context) {
return true;
}
}
public static Condition always() {
return new Always();
}
public static class Never implements Condition {
@Override
public boolean isSatisfied(C context) {
return false;
}
}
public static Condition never() {
return new Never();
}
public static Condition and(final Condition first, final Condition second) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
return first.isSatisfied(context) && second.isSatisfied(context);
}
};
}
public static Condition and(final List> conditions) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
for (Condition condition : conditions) {
if (!condition.isSatisfied(context)) {
return false;
}
}
return true;
}
};
}
public static Condition or(final Condition first, final Condition second) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
return first.isSatisfied(context) || second.isSatisfied(context);
}
};
}
public static Condition or(final List> conditions) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
for (Condition condition : conditions) {
if (condition.isSatisfied(context)) {
return true;
}
}
return false;
}
};
}
public static Condition not(final Condition condition) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
return !condition.isSatisfied(context);
}
};
}
public static Condition xor(final Condition first, final Condition second) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
return first.isSatisfied(context) ^ second.isSatisfied(context);
}
};
}
}