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

org.vesalainen.nmea.script.AbstractScriptObjectFactory Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 tkv
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.vesalainen.nmea.script;

import java.io.IOException;
import java.util.List;
import org.vesalainen.util.logging.JavaLogging;

/**
 *
 * @author tkv
 * @param  */
public abstract class AbstractScriptObjectFactory implements ScriptObjectFactory
{

    @Override
    public ScriptStatement createIf(ScriptStatement expr, ScriptStatement successStat, ScriptStatement elseStat)
    {
        return new If(expr, successStat, elseStat);
    }

    @Override
    public ScriptStatement createWhile(ScriptStatement expr, ScriptStatement stat)
    {
        return new While(expr, stat);
    }

    @Override
    public ScriptStatement createBlock(List> sList)
    {
        return new Block(sList);
    }

    @Override
    public ScriptStatement createSleeper(long millis)
    {
        return new Sleeper(millis);
    }

    @Override
    public ScriptStatement createLooper(int times, ScriptStatement stat)
    {
        return new Looper(times, stat);
    }

    private static class Sleeper implements ScriptStatement
    {
        private final long millis;
        public Sleeper(long millis)
        {
            this.millis = millis;
        }

        @Override
        public synchronized Void exec(E engine) throws InterruptedException
        {
            wait(millis);
            return null;
        }

        @Override
        public String toString()
        {
            return "sleep(" + millis + ");";
        }
        
    }

    private static class Looper extends JavaLogging implements ScriptStatement
    {
        private final int times;
        private final ScriptStatement statement;

        public Looper(int times, ScriptStatement statement)
        {
            this.times = times;
            this.statement = statement;
            setLogger(this.getClass());
        }
        @Override
        public Void exec(E engine) throws IOException, InterruptedException
        {
            config("loop: %d", times);
            for (int ii=0;ii implements ScriptStatement
    {
        private final List> sList;

        public Block(List> sList)
        {
            this.sList = sList;
        }

        @Override
        public Void exec(E engine) throws IOException, InterruptedException
        {
            for (ScriptStatement s : sList)
            {
                s.exec(engine);
            }
            return null;
        }
    }

    private static class If implements ScriptStatement
    {
        private final ScriptStatement expr;
        private final ScriptStatement successStat;
        private final ScriptStatement elseStat;

        public If(ScriptStatement expr, ScriptStatement successStat, ScriptStatement elseStat)
        {
            this.expr = expr;
            this.successStat = successStat;
            this.elseStat = elseStat;
        }

        @Override
        public Void exec(E engine) throws IOException, InterruptedException
        {
            Boolean success = expr.exec(engine);
            if (success)
            {
                successStat.exec(engine);
            }
            else
            {
                if (elseStat != null)
                {
                    elseStat.exec(engine);
                }
            }
            return null;
        }
        @Override
        public String toString()
        {
            return "if()";
        }

    }

    private static class While implements ScriptStatement
    {
        private final ScriptStatement expr;
        private final ScriptStatement stat;

        public While(ScriptStatement expr, ScriptStatement stat)
        {
            this.expr = expr;
            this.stat = stat;
        }

        @Override
        public Void exec(E engine) throws IOException, InterruptedException
        {
            while (expr.exec(engine))
            {
                stat.exec(engine);
            }
            return null;
        }
        
        @Override
        public String toString()
        {
            return "while()";
        }

    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy