001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
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.nuxeo.ecm.platform.importer.queue.consumer;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Set;
023
024/**
025 * @since 8.3
026 */
027public class ImportStat implements Serializable {
028
029    /**
030     *
031     */
032    private static final long serialVersionUID = 1L;
033
034    private final Map<String, Long> statMap;
035
036    public ImportStat() {
037        statMap = new HashMap<>();
038    }
039
040    public ImportStat(Map<String, Long> initMap) {
041        statMap = initMap;
042    }
043
044    public void increaseStat(String key, long count) {
045        if (statMap.containsKey(key)) {
046            long previousCount = statMap.get(key);
047            count += previousCount;
048        }
049        statMap.put(key, count);
050    }
051
052    public boolean containsKey(String key) {
053        return statMap.containsKey(key);
054    }
055
056    public Set<String> keySet() {
057        return statMap.keySet();
058    }
059
060    public long getStat(String key) {
061        return statMap.get(key);
062    }
063
064    public void merge(ImportStat other) {
065        for (String key : other.keySet()) {
066            if (statMap.containsKey(key)) {
067                increaseStat(key, other.getStat(key));
068            } else {
069                statMap.put(key, other.getStat(key));
070            }
071        }
072    }
073
074    @Override
075    public String toString() {
076        StringBuilder sb = new StringBuilder("Import stat :\n");
077        for (String key : statMap.keySet()) {
078            sb.append("[");
079            sb.append(key);
080            sb.append("]: ");
081            sb.append(statMap.get(key));
082            sb.append("\n");
083        }
084
085        return sb.toString();
086    }
087
088    public Map<String, Long> getStatMap() {
089        return new HashMap<>(statMap);
090    }
091}