Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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,E> successStat, ScriptStatement,E> elseStat)
{
return new If(expr, successStat, elseStat);
}
@Override
public ScriptStatement createWhile(ScriptStatement expr, ScriptStatement, E> 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,E> s : sList)
{
s.exec(engine);
}
return null;
}
}
private static class If implements ScriptStatement
{
private final ScriptStatement expr;
private final ScriptStatement,E> successStat;
private final ScriptStatement,E> elseStat;
public If(ScriptStatement expr, ScriptStatement,E> successStat, ScriptStatement,E> 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,E> stat;
public While(ScriptStatement expr, ScriptStatement, E> 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()";
}
}
}