StatisticsManager.java 2.98 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1
/**
2
 * Copyright (C) 1999-2008 Jive Software. All rights reserved.
Alex Wenckus's avatar
Alex Wenckus committed
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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
15
 */
16
package org.jivesoftware.openfire.stats;
Alex Wenckus's avatar
Alex Wenckus committed
17

Alex Wenckus's avatar
Alex Wenckus committed
18
import java.util.*;
19
import java.util.concurrent.ConcurrentHashMap;
Alex Wenckus's avatar
Alex Wenckus committed
20 21 22 23 24

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

Alex Wenckus's avatar
Alex Wenckus committed
26 27 28 29 30 31
    private static StatisticsManager instance = new StatisticsManager();

    public static StatisticsManager getInstance() {
        return instance;
    }

32 33 34
    private final Map<String, Statistic> statistics = new ConcurrentHashMap<>();
    private final Map<String, List<String>> multiStatGroups = new ConcurrentHashMap<>();
    private final Map<String, String> keyToGroupMap = new ConcurrentHashMap<>();
Alex Wenckus's avatar
Alex Wenckus committed
35

36 37 38
    private StatisticsManager() {
        
    }
Alex Wenckus's avatar
Alex Wenckus committed
39 40 41 42

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

    /**
     * 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
60 61 62 63
    public void addMultiStatistic(String statKey, String groupName, Statistic statistic) {
        addStatistic(statKey, statistic);
        List<String> group = multiStatGroups.get(groupName);
        if(group == null) {
64
            group = new ArrayList<>();
Alex Wenckus's avatar
Alex Wenckus committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
            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
79 80 81 82
    /**
     * Returns all statistics that the StatManager is tracking.
     * @return Returns all statistics that the StatManager is tracking.
     */
Alex Wenckus's avatar
Alex Wenckus committed
83 84
    public Set<Map.Entry<String, Statistic>> getAllStatistics() {
        return statistics.entrySet();
Alex Wenckus's avatar
Alex Wenckus committed
85 86
    }

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

96
}