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

org.unlaxer.tinyexpression.AbstractCalculationContext Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.unlaxer.tinyexpression;

import java.math.RoundingMode;
import java.util.Map;
import java.util.Optional;
import java.util.Random;

public abstract class AbstractCalculationContext implements CalculationContext{
	
	public final int scale;
	public final RoundingMode roundingMode;
	public final Angle angle;
	
	public final Map valueByName = newMap();
	
	public final Map booleanByName = newMap();
	
	public final Map stringByName = newMap();
	
	public final Map objectByName = newMap();
	
	Random random = new Random();
	
	AbstractCalculationContext(int scale, RoundingMode roundingMode , Angle angle) {
		super();
		this.scale = scale;
		this.roundingMode = roundingMode;
		this.angle = angle;
	}
	
	AbstractCalculationContext() {
		this(10,RoundingMode.HALF_UP , Angle.DEGREE);
	}

	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#set(java.lang.String, java.lang.String)
	 */
	@Override
	public void set(String name,String value) {
		stringByName.put(name, value);
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#getString(java.lang.String)
	 */
	@Override
	public Optional getString(String name){
		return Optional.ofNullable(stringByName.get(name));
	}

	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#set(java.lang.String, float)
	 */
	@Override
	public void set(String name,float value) {
	  
		valueByName.put(name, value);
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#getValue(java.lang.String)
	 */
	@Override
	public Optional getValue(String name) {

		return Optional.ofNullable((Float)valueByName.get(name));
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#set(java.lang.String, boolean)
	 */
	@Override
	public void set(String name , boolean value) {
		booleanByName.put(name, value);
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#getBoolean(java.lang.String)
	 */
	@Override
	public Optional getBoolean(String name){
		return Optional.ofNullable(booleanByName.get(name));
	}
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public  Optional getObject(String name, Class clazz) {
		return (Optional) Optional.ofNullable(objectByName.get(name));
	}

	@Override
	public void setObject(String name, Object object) {
		objectByName.put(name, object);
	}

	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#isExists(java.lang.String)
	 */
	@Override
	public boolean isExists(String name) {
		return valueByName.get(name) != null ||
				booleanByName.get(name) != null ||
				stringByName.get(name) != null ||
				objectByName.get(name) != null;
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#angle(double)
	 */
	@Override
	public double radianAngle(double angleValue) {
		if(angle == Angle.RADIAN){
			return angleValue;
		}
		return Math.toRadians(angleValue);
	}
	
	/* (non-Javadoc)
	 * @see org.unlaxer.tinyexpression.ICalculationContext#nextRandom()
	 */
	@Override
	public float nextRandom() {
		return random.nextFloat();
	}
	
	@Override
	public Angle angle() {
		return angle;
	}

	@Override
	public int scale() {
		return scale;
	}

	@Override
	public RoundingMode roundingMode() {
		return roundingMode;
	}

	public abstract  Map newMap();

  @Override
  public void set(String name, Number value) {
    valueByName.put(name, value);
  }

  @Override
  public Optional getNumber(String name) {
    return Optional.ofNullable(valueByName.get(name));
  }
	
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy