All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.squirrelframework.foundation.fsm.Conditions Maven / Gradle / Ivy

Go to download

foundation module of squirrel framework which provided event driven infrastructure and a finite state machine implementation.

There is a newer version: 0.3.10
Show newest version
package org.squirrelframework.foundation.fsm;

import java.util.List;
/**
 * Constants conditions
 * 
 * @author Henry.He
 *
 */
public class Conditions {
    
    /**
     * Renamed to AnonymousCondition
     */
    @Deprecated
    public static abstract class AbstractCondition extends AnonymousCondition {
    }
    
    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