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 boolean isSatified(Condition condition, C context) {
return condition!=null && context!=null && condition.isSatisfied(context);
}
public static boolean isNotSatified(Condition condition, C context) {
return condition==null || context==null || !condition.isSatisfied(context);
}
public static class Always extends AnonymousCondition {
@Override
public boolean isSatisfied(C context) {
return true;
}
}
public static Condition always() {
return new Always();
}
public static class Never extends AnonymousCondition {
@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);
}
@Override
public String name() {
return first.name()+"And"+second.name();
}
};
}
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;
}
@Override
public String name() {
String name = null;
for(Condition c : conditions) {
if(name==null)
name=c.name();
else
name = name+"And"+c.name();
}
return name;
}
};
}
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);
}
@Override
public String name() {
return first.name()+"Or"+second.name();
}
};
}
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;
}
@Override
public String name() {
String name = null;
for(Condition c : conditions) {
if(name==null)
name=c.name();
else
name = name+"Or"+c.name();
}
return name;
}
};
}
public static Condition not(final Condition condition) {
return new Condition() {
@Override
public boolean isSatisfied(C context) {
return !condition.isSatisfied(context);
}
@Override
public String name() {
return "Not"+condition.name();
}
};
}
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);
}
@Override
public String name() {
return first.name()+"Xor"+second.name();
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy