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 number of hours for each nature and can contain an original value for overridden course parts.
024 */
025public class VolumesOfHours
026{
027    private Double _originalVolumeOfHours;
028    
029    /** Volume of hours by nature */
030    private Map<String, Double> _volumeOfHoursByNature;
031    
032    /** The constructor 
033     */
034    public VolumesOfHours()
035    {
036        _volumeOfHoursByNature = new HashMap<>();
037    }
038    
039    /** Get the volume of hours by nature 
040     * @return a {@link Map} the volumes of hours by nature
041     */
042    public Map<String, Double> getVolumes()
043    {
044        return _volumeOfHoursByNature;
045    }
046    
047    /**
048     * Get the volume of hours for the given nature
049     * @param natureId the nature identifier
050     * @return the volume of hours for the given nature, <code>null</code> if not defined
051     */
052    public Double getVolume(String natureId)
053    {
054        return _volumeOfHoursByNature.get(natureId);
055    }
056    
057    /**
058     * Add an volume of hours for the given nature
059     * @param natureId the nature of the volume
060     * @param value the number of hours
061     */
062    public void addVolume(String natureId, Double value)
063    {
064        _volumeOfHoursByNature.put(natureId, value);
065    }
066    
067    /**
068     * Add an overridden volume of hours for the given nature and store the old value into the original volume of hours
069     * @param natureId the nature of the volume
070     * @param value the number of hours
071     */
072    public void addOverriddenVolume(String natureId, Double value)
073    {
074        _originalVolumeOfHours = _volumeOfHoursByNature.get(natureId);
075        _volumeOfHoursByNature.put(natureId, value);
076    }
077    
078    /**
079     * Get the original volume of hours for this item (should be a course part only)
080     * @return the original value if it has been overridden, otherwise <code>null</code>
081     */
082    public Double getOriginalVolumeOfHours()
083    {
084        return _originalVolumeOfHours;
085    }
086    
087    /**
088     * Sum the volume of hours with its weight on the current object
089     * @param volumesOfHours The volumes to sum with the original object
090     * @param weight the weight
091     */
092    public void sum(VolumesOfHours volumesOfHours, Double weight)
093    {
094        Map<String, Double> volumeOfHoursByNature = volumesOfHours.getVolumes();
095        for (String natureId : volumeOfHoursByNature.keySet())
096        {
097            _volumeOfHoursByNature.put(natureId, _volumeOfHoursByNature.getOrDefault(natureId,  0D) + volumeOfHoursByNature.get(natureId) * weight);
098        }
099    }
100    
101    /**
102     * Get the total volume of hours of all natures
103     * @return the total volume of hours
104     */
105    public Double getTotal()
106    {
107        return _volumeOfHoursByNature.values()
108            .stream()
109            .reduce(0D, Double::sum);
110    }
111}