StatisticsManager.java 3.08 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1 2 3 4 5
/**
 * $RCSfile  $
 * $Revision  $
 * $Date  $
 *
6
 * Copyright (C) 1999-2008 Jive Software. All rights reserved.
Alex Wenckus's avatar
Alex Wenckus committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Alex Wenckus's avatar
Alex Wenckus committed
19
 */
20
package org.jivesoftware.openfire.stats;
Alex Wenckus's avatar
Alex Wenckus committed
21

Alex Wenckus's avatar
Alex Wenckus committed
22
import java.util.*;
23
import java.util.concurrent.ConcurrentHashMap;
Alex Wenckus's avatar
Alex Wenckus committed
24 25 26 27 28

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

Alex Wenckus's avatar
Alex Wenckus committed
30 31 32 33 34 35
    private static StatisticsManager instance = new StatisticsManager();

    public static StatisticsManager getInstance() {
        return instance;
    }

36
    private final Map<String, Statistic> statistics = new ConcurrentHashMap<String, Statistic>();
Alex Wenckus's avatar
Alex Wenckus committed
37 38 39
    private final Map<String, List<String>> multiStatGroups = new ConcurrentHashMap<String, List<String>>();
    private final Map<String, String> keyToGroupMap = new ConcurrentHashMap<String, String>();

40 41 42
    private StatisticsManager() {
        
    }
Alex Wenckus's avatar
Alex Wenckus committed
43 44 45 46

    /**
     * Adds a stat to be tracked to the StatManager.
     *
47 48
     * @param statKey the statistic key.
     * @param definition the statistic to be tracked.
Alex Wenckus's avatar
Alex Wenckus committed
49
     */
Alex Wenckus's avatar
Alex Wenckus committed
50 51
    public void addStatistic(String statKey, Statistic definition) {
        statistics.put(statKey, definition);
Alex Wenckus's avatar
Alex Wenckus committed
52 53 54 55 56 57 58 59 60 61 62 63
    }

    /**
     * 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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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
83 84 85 86
    /**
     * Returns all statistics that the StatManager is tracking.
     * @return Returns all statistics that the StatManager is tracking.
     */
Alex Wenckus's avatar
Alex Wenckus committed
87 88
    public Set<Map.Entry<String, Statistic>> getAllStatistics() {
        return statistics.entrySet();
Alex Wenckus's avatar
Alex Wenckus committed
89 90
    }

91 92 93 94 95 96 97 98 99
    /**
     * Removes a statistic from the server.
     *
     * @param statKey The key of the stat to be removed.
     */
    public void removeStatistic(String statKey) {
        statistics.remove(statKey);
    }

100
}