001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.plugins.odfpilotage.cost.entity;
018
019import java.util.HashMap;
020import java.util.Map;
021
022/**
023 * Class representing a {@link VolumesOfHours}
024 */
025public class VolumesOfHours
026{
027    /** Map of volume of hours */
028    private Map<String, Double> _volumeOfHours;
029    
030    /** The id of the programItem containing these volumes of hours */
031    private String _programItemId;
032    
033    /** The constructor 
034     * @param programItemId the programItemId
035     */
036    public VolumesOfHours(String programItemId)
037    {
038        _volumeOfHours = new HashMap<>();
039        this._programItemId = programItemId;
040    }
041    
042    /** Get the volume of hours map 
043     * @return the hourly volumes map
044     */
045    public Map<String, Double> getVolumes()
046    {
047        return _volumeOfHours;
048    }
049    
050    /**
051     * Add an hourly volume to the map
052     * @param natureId the nature of the hourly volume
053     * @param value the hourly volume
054     */
055    public void addVolumes(String natureId, Double value)
056    {
057        _volumeOfHours.put(natureId, value);
058    }
059    
060    /**
061     * Get the programItemId
062     * @return the programItemId
063     */
064    public String getProgramItemId()
065    {
066        return _programItemId;
067    }
068    
069    /**
070     * Set the programItemId
071     * @param programItemId the programItemId
072     */
073    public void setProgramItemId(String programItemId)
074    {
075        _programItemId = programItemId;
076    }
077    
078    /**
079     * Sum two VolumeHoraire objects and weight the result by a value
080     * @param volumeHoraire The second VolumeHoraire object to sum
081     * @param weight the weight
082     */
083    public void sum(VolumesOfHours volumeHoraire, Double weight)
084    {
085        Map<String, Double> volumeHoraires = volumeHoraire.getVolumes();
086        for (String natureId : volumeHoraires.keySet())
087        {
088            Double oldValue = _volumeOfHours.getOrDefault(natureId, 0D);
089            Double newValue = volumeHoraires.getOrDefault(natureId, 0D);
090            _volumeOfHours.put(natureId, oldValue + newValue * weight);
091        }
092    }
093    
094    /**
095     * Get the total volume of hours of all natures
096     * @return the total volume of hours
097     */
098    public Double getTotal()
099    {
100        Double total = 0d;
101        for (Double value : _volumeOfHours.values())
102        {
103            total += value;
104        }
105        return total;
106    }
107}