001/* *********************************************************************** *
002 * project: org.matsim.*
003 *                                                                         *
004 * *********************************************************************** *
005 *                                                                         *
006 * copyright       : (C) 2016 by the members listed in the COPYING,        *
007 *                   LICENSE and WARRANTY file.                            *
008 * email           : info at matsim dot org                                *
009 *                                                                         *
010 * *********************************************************************** *
011 *                                                                         *
012 *   This program is free software; you can redistribute it and/or modify  *
013 *   it under the terms of the GNU General Public License as published by  *
014 *   the Free Software Foundation; either version 2 of the License, or     *
015 *   (at your option) any later version.                                   *
016 *   See also COPYING, LICENSE and WARRANTY file                           *
017 *                                                                         *
018 * *********************************************************************** */
019
020package org.matsim.contrib.util;
021
022import java.util.EnumMap;
023
024import org.apache.commons.lang3.mutable.MutableDouble;
025
026public class DoubleEnumAdder<K extends Enum<K>> extends AbstractEnumAdder<K, Double> {
027        private final EnumMap<K, MutableDouble> sums;
028        private double totalSum;
029
030        public DoubleEnumAdder(Class<K> clazz) {
031                super(clazz);
032                sums = new EnumMap<>(clazz);
033                for (K e : keys) {
034                        sums.put(e, new MutableDouble());
035                }
036        }
037
038        public void addDouble(K e, double value) {
039                sums.get(e).add(value);
040                totalSum += value;
041        }
042
043        @Override
044        public void add(K e, Number value) {
045                addDouble(e, value.doubleValue());
046        }
047
048        public double getDouble(K e) {
049                return sums.get(e).doubleValue();
050        }
051
052        public double getDoubleTotal() {
053                return totalSum;
054        }
055
056        @Override
057        public Double get(K e) {
058                return getDouble(e);
059        }
060
061        @Override
062        public Double getTotal() {
063                return getDoubleTotal();
064        }
065
066        @Override
067        public String toString() {
068                return sums + ", total=" + totalSum;
069        }
070}