1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-02 15:33:30 +01:00
Jobs/com/gamingmesh/jobs/resources/jfep/VariableNode.java

130 lines
3.2 KiB
Java
Raw Normal View History

2015-08-21 15:10:08 +02:00
/**
* Copyright 2006 Bertoli Marco
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamingmesh.jobs.resources.jfep;
/**
* <p><b>Name:</b> VariableNode</p>
* <p><b>Description:</b>
* A node holding a double variable.
* </p>
* <p><b>Date:</b> 08/dic/06
* <b>Time:</b> 15:56:59</p>
* @author Bertoli Marco
* @version 1.0
*/
public class VariableNode implements ExpressionNode {
/** Value of the variable */
protected double value;
/** True if variable was not initialized */
protected boolean error;
/** Name of the variable */
protected String name;
/** An empty array with children */
protected ExpressionNode[] children = new ExpressionNode[0];
2016-06-25 15:27:01 +02:00
2015-08-21 15:10:08 +02:00
/**
* Creates a new variable node with given name.
* @param name name of the variable
* @param error throws an exception if value is get but variable
* is not initialized. Otherwise 0.0 is returned.
*/
public VariableNode(String name, boolean error) {
2016-06-25 15:27:01 +02:00
this.name = name;
value = 0.0;
this.error = error;
2015-08-21 15:10:08 +02:00
}
2016-06-25 15:27:01 +02:00
2015-08-21 15:10:08 +02:00
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#count()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public int count() {
2016-06-25 15:27:01 +02:00
return 1;
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#getDepth()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public int getDepth() {
2016-06-25 15:27:01 +02:00
return 1; // This is a leaf node
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#getSubtype()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public String getSubtype() {
2016-06-25 15:27:01 +02:00
return name;
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#getType()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public int getType() {
2016-06-25 15:27:01 +02:00
return ExpressionNode.VARIABLE_NODE;
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#getValue()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public double getValue() {
2016-06-25 15:27:01 +02:00
if (!error)
return value;
throw new EvaluationException("Variable '" + name + "' was not initialized.");
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see jmt.engine.math.parser.ExpressionNode#setVariable(java.lang.String, double)
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public void setVariable(String name, double value) {
2016-06-25 15:27:01 +02:00
if (this.name.equals(name)) {
this.value = value;
error = false;
}
2015-08-21 15:10:08 +02:00
}
2016-06-25 15:27:01 +02:00
2015-08-21 15:10:08 +02:00
/* (non-Javadoc)
* @see org.mbertoli.jfep.ExpressionNode#getChildrenNodes()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public ExpressionNode[] getChildrenNodes() {
2016-06-25 15:27:01 +02:00
return children;
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see java.lang.Object#clone()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public Object clone() {
2016-06-25 15:27:01 +02:00
VariableNode node = new VariableNode(name, error);
node.value = value;
return node;
2015-08-21 15:10:08 +02:00
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
2016-06-25 17:56:59 +02:00
@Override
2015-08-21 15:10:08 +02:00
public String toString() {
2016-06-25 15:27:01 +02:00
return getSubtype();
2015-08-21 15:10:08 +02:00
}
}