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

io.imunity.furms.ui.charts.service.ChunkSeriesGenerator Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/*
 * Copyright (c) 2020 Bixbit s.c. All rights reserved.
 * See LICENSE file for licensing information.
 */

package io.imunity.furms.ui.charts.service;

import io.imunity.furms.domain.project_allocation_installation.ProjectAllocationChunk;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

@Component
class ChunkSeriesGenerator {

	List prepareYValuesForAllocationChunkLine(List xArguments,
	                                                  Set allChunks) {
		Map sumOfChunkValuesByValidToDates = prepareSumOfChunkValuesByValidDates(allChunks);
		double lastValue = 0;
		List yValues = new ArrayList<>();
		for (LocalDate xArgument : xArguments) {
			double value = sumOfChunkValuesByValidToDates.getOrDefault(xArgument, lastValue);
			yValues.add(value);
			lastValue = value;
		}
		return yValues;
	}

	private Map prepareSumOfChunkValuesByValidDates(Set allChunks) {
		List orderedChunks = allChunks.stream()
			.sorted(comparing(x -> x.validFrom))
			.collect(toList());

		double last = 0;
		Map sumOfChunkValuesByValidDates = new HashMap<>();
		for (ProjectAllocationChunk chunk : orderedChunks) {
			sumOfChunkValuesByValidDates.put(chunk.validFrom.toLocalDate(), chunk.amount.doubleValue() + last);
			last = chunk.amount.doubleValue() + last;
		}
		return sumOfChunkValuesByValidDates;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy