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 {
    
    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);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy