001/**
002 * Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * 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, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package org.javamoney.moneta.function;
017
018import java.util.Map;
019import java.util.Objects;
020
021import javax.money.CurrencyUnit;
022import javax.money.MonetaryAmount;
023
024/**
025 * Internal class implementing grouped statistic functionality.
026 */
027public class GroupMonetarySummaryStatistics {
028
029        private final Map<CurrencyUnit, MonetarySummaryStatistics> groupSummary = new MonetarySummaryMap();
030
031    GroupMonetarySummaryStatistics() {
032
033    }
034
035        public Map<CurrencyUnit, MonetarySummaryStatistics> get() {
036        return groupSummary;
037    }
038
039    public GroupMonetarySummaryStatistics accept(MonetaryAmount amount) {
040        CurrencyUnit currency = Objects.requireNonNull(amount).getCurrency();
041        MonetarySummaryStatistics summary = groupSummary.get(currency);
042        if(summary==null){
043            summary = new DefaultMonetarySummaryStatistics(currency);
044            groupSummary.put(currency, summary);
045        }
046        summary.accept(amount);
047        return this;
048    }
049
050    @Override
051    public int hashCode() {
052        return Objects.hash(groupSummary);
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057        if (GroupMonetarySummaryStatistics.class.isInstance(obj)) {
058            GroupMonetarySummaryStatistics other = GroupMonetarySummaryStatistics.class
059                    .cast(obj);
060            return Objects.equals(groupSummary, other.groupSummary);
061        }
062        return false;
063    }
064
065    @Override
066    public String toString() {
067        return "GroupMonetarySummaryStatistics: " + groupSummary.toString();
068    }
069
070    public GroupMonetarySummaryStatistics combine(
071            GroupMonetarySummaryStatistics another) {
072        Objects.requireNonNull(another);
073
074        for (CurrencyUnit keyCurrency : another.groupSummary.keySet()) {
075            MonetarySummaryStatistics stats = groupSummary.get(keyCurrency);
076            if(stats==null){
077                stats = new DefaultMonetarySummaryStatistics(keyCurrency);
078                groupSummary.put(keyCurrency,stats);
079            }
080
081            MonetarySummaryStatistics oldValue = groupSummary.get(keyCurrency);
082            MonetarySummaryStatistics newValue = another.groupSummary.get(keyCurrency);
083            if(oldValue!=null) {
084                newValue = oldValue.combine(another.groupSummary.get(keyCurrency));
085            }
086            groupSummary.put(keyCurrency, newValue);
087        }
088        return this;
089    }
090
091}