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

cdm.base.datetime.functions.IsBusinessDay Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.92
Show newest version
package cdm.base.datetime.functions;

import cdm.base.datetime.BusinessCenterEnum;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.Mapper;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.mapper.MapperUtils;
import com.rosetta.model.lib.records.Date;
import java.util.List;
import javax.inject.Inject;


@ImplementedBy(IsBusinessDay.IsBusinessDayDefault.class)
public abstract class IsBusinessDay implements RosettaFunction {
	
	// RosettaFunction dependencies
	//
	@Inject protected IsHoliday isHoliday;
	@Inject protected IsWeekend isWeekend;

	/**
	* @param date The date for which we want to determine whether it's a good business day
	* @param businessCenters The list of business centers to use
	* @return isGoodBusinessDay True if a good business day, false if a weekend or holiday
	*/
	public Boolean evaluate(Date date, List businessCenters) {
		Boolean isGoodBusinessDay = doEvaluate(date, businessCenters);
		
		return isGoodBusinessDay;
	}

	protected abstract Boolean doEvaluate(Date date, List businessCenters);

	protected abstract Mapper weekend(Date date, List businessCenters);

	protected abstract Mapper holiday(Date date, List businessCenters);

	public static class IsBusinessDayDefault extends IsBusinessDay {
		@Override
		protected Boolean doEvaluate(Date date, List businessCenters) {
			Boolean isGoodBusinessDay = null;
			return assignOutput(isGoodBusinessDay, date, businessCenters);
		}
		
		protected Boolean assignOutput(Boolean isGoodBusinessDay, Date date, List businessCenters) {
			isGoodBusinessDay = MapperUtils.runSingle(() -> {
				if (MapperS.of(weekend(date, businessCenters).get()).getOrDefault(false)) {
					return MapperS.of(Boolean.valueOf(false));
				}
				else if (MapperS.of(holiday(date, businessCenters).get()).getOrDefault(false)) {
					return MapperS.of(Boolean.valueOf(false));
				}
				else {
					return MapperS.of(Boolean.valueOf(true));
				}
			}).get();
			
			return isGoodBusinessDay;
		}
		
		@Override
		protected Mapper weekend(Date date, List businessCenters) {
			return MapperS.of(isWeekend.evaluate(MapperS.of(date).get(), MapperC.of(businessCenters).getMulti()));
		}
		
		@Override
		protected Mapper holiday(Date date, List businessCenters) {
			return MapperS.of(isHoliday.evaluate(MapperS.of(date).get(), MapperC.of(businessCenters).getMulti()));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy