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

cdm.event.common.functions.TransfersForDate Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.event.common.functions;

import cdm.base.datetime.AdjustableOrAdjustedOrRelativeDate;
import cdm.event.common.Transfer;
import cdm.event.common.Transfer.TransferBuilder;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.functions.ModelObjectValidator;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.records.Date;
import com.rosetta.model.metafields.FieldWithMetaDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.inject.Inject;

import static com.rosetta.model.lib.expression.ExpressionOperators.*;

@ImplementedBy(TransfersForDate.TransfersForDateDefault.class)
public abstract class TransfersForDate implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;

	/**
	* @param transfers 
	* @param date 
	* @return transfersForDate 
	*/
	public List evaluate(List transfers, Date date) {
		List transfersForDateBuilder = doEvaluate(transfers, date);
		
		final List transfersForDate;
		if (transfersForDateBuilder == null) {
			transfersForDate = null;
		} else {
			transfersForDate = transfersForDateBuilder.stream().map(Transfer::build).collect(Collectors.toList());
			objectValidator.validate(Transfer.class, transfersForDate);
		}
		
		return transfersForDate;
	}

	protected abstract List doEvaluate(List transfers, Date date);

	public static class TransfersForDateDefault extends TransfersForDate {
		@Override
		protected List doEvaluate(List transfers, Date date) {
			if (transfers == null) {
				transfers = Collections.emptyList();
			}
			List transfersForDate = new ArrayList<>();
			return assignOutput(transfersForDate, transfers, date);
		}
		
		protected List assignOutput(List transfersForDate, List transfers, Date date) {
			transfersForDate.addAll(toBuilder(MapperC.of(transfers)
				.filterItemNullSafe(item -> areEqual(item.map("getSettlementDate", transfer -> transfer.getSettlementDate()).map("getAdjustedDate", adjustableOrAdjustedOrRelativeDate -> adjustableOrAdjustedOrRelativeDate.getAdjustedDate()).map("getValue", _f->_f.getValue()), MapperS.of(date), CardinalityOperator.All).get()).getMulti()));
			
			return Optional.ofNullable(transfersForDate)
				.map(o -> o.stream().map(i -> i.prune()).collect(Collectors.toList()))
				.orElse(null);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy