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

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

The newest version!
package org.unlaxer.tinyexpression;

import java.math.RoundingMode;
import java.time.DayOfWeek;
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));
  }
	
  @Override
  public boolean inDayTimeRange(
      DayOfWeek fromDayInclusive, float fromDayHourInclusive,
      DayOfWeek toDayInclusive, float toDayHourExclusive) {

    Optional nowHourOpt = getValue("nowHour");
    Optional nowDayOfWeekOpt = getValue("nowDayOfWeek");

    if (nowHourOpt.isEmpty() || nowDayOfWeekOpt.isEmpty()) {
      return false;
    }

    float nowHour = nowHourOpt.get();
    float nowDayOfWeek = nowDayOfWeekOpt.get();

    boolean spansWeek = fromDayInclusive.getValue() > toDayInclusive.getValue();

    boolean withinDayOfWeek = false;
    if (spansWeek) {
      withinDayOfWeek = (nowDayOfWeek >= fromDayInclusive.getValue())
          || (nowDayOfWeek <= toDayInclusive.getValue());
    } else {
      withinDayOfWeek = (nowDayOfWeek >= fromDayInclusive.getValue())
          && (nowDayOfWeek <= toDayInclusive.getValue());
    }

    if (! withinDayOfWeek) return false;

    boolean withinTime = false;
    if (fromDayInclusive.getValue() == toDayInclusive.getValue()) {
      withinTime = nowHour >= fromDayHourInclusive && nowHour < toDayHourExclusive;
    } else if (fromDayInclusive.getValue() == nowDayOfWeek) {
      withinTime = nowHour >= fromDayHourInclusive;
    } else if (toDayInclusive.getValue() == nowDayOfWeek) {
      withinTime = nowHour < toDayHourExclusive;
    } else {
      // If we're on any day between to from and to day then we're definitely within the time range
      withinTime = true;
    }

    return withinTime;
  }

	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy