StatisticsManager.java 2.66 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1 2 3 4 5 6 7 8 9 10
/**
 * $RCSfile  $
 * $Revision  $
 * $Date  $
 *
 * Copyright (C) 1999-2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */
11
package org.jivesoftware.openfire.stats;
Alex Wenckus's avatar
Alex Wenckus committed
12

Alex Wenckus's avatar
Alex Wenckus committed
13
import java.util.*;
14
import java.util.concurrent.ConcurrentHashMap;
Alex Wenckus's avatar
Alex Wenckus committed
15 16 17 18 19

/**
 * Stores statistics being tracked by the server.
 */
public class StatisticsManager {
20

Alex Wenckus's avatar
Alex Wenckus committed
21 22 23 24 25 26
    private static StatisticsManager instance = new StatisticsManager();

    public static StatisticsManager getInstance() {
        return instance;
    }

27
    private final Map<String, Statistic> statistics = new ConcurrentHashMap<String, Statistic>();
Alex Wenckus's avatar
Alex Wenckus committed
28 29 30
    private final Map<String, List<String>> multiStatGroups = new ConcurrentHashMap<String, List<String>>();
    private final Map<String, String> keyToGroupMap = new ConcurrentHashMap<String, String>();

31 32 33
    private StatisticsManager() {
        
    }
Alex Wenckus's avatar
Alex Wenckus committed
34 35 36 37

    /**
     * Adds a stat to be tracked to the StatManager.
     *
38 39
     * @param statKey the statistic key.
     * @param definition the statistic to be tracked.
Alex Wenckus's avatar
Alex Wenckus committed
40
     */
Alex Wenckus's avatar
Alex Wenckus committed
41 42
    public void addStatistic(String statKey, Statistic definition) {
        statistics.put(statKey, definition);
Alex Wenckus's avatar
Alex Wenckus committed
43 44 45 46 47 48 49 50 51 52 53 54
    }

    /**
     * Returns a statistic being tracked by the StatManager.
     *
     * @param statKey The key of the definition.
     * @return Returns the related stat.
     */
    public Statistic getStatistic(String statKey) {
        return statistics.get(statKey);
    }

Alex Wenckus's avatar
Alex Wenckus committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    public void addMultiStatistic(String statKey, String groupName, Statistic statistic) {
        addStatistic(statKey, statistic);
        List<String> group = multiStatGroups.get(groupName);
        if(group == null) {
            group = new ArrayList<String>();
            multiStatGroups.put(groupName, group);
        }
        group.add(statKey);
        keyToGroupMap.put(statKey, groupName);
    }

    public List<String> getStatGroup(String statGroup) {
        return multiStatGroups.get(statGroup);
    }

    public String getMultistatGroup(String statKey) {
        return keyToGroupMap.get(statKey);
    }

Alex Wenckus's avatar
Alex Wenckus committed
74 75 76 77
    /**
     * Returns all statistics that the StatManager is tracking.
     * @return Returns all statistics that the StatManager is tracking.
     */
Alex Wenckus's avatar
Alex Wenckus committed
78 79
    public Set<Map.Entry<String, Statistic>> getAllStatistics() {
        return statistics.entrySet();
Alex Wenckus's avatar
Alex Wenckus committed
80 81
    }

82 83 84 85 86 87 88 89 90
    /**
     * Removes a statistic from the server.
     *
     * @param statKey The key of the stat to be removed.
     */
    public void removeStatistic(String statKey) {
        statistics.remove(statKey);
    }

91
}