001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.management;
019    
020    import java.util.*;
021    import javax.management.j2ee.statistics.Statistic;
022    import javax.management.j2ee.statistics.Stats;
023    
024    
025    /**
026     * Base class for a Stats implementation
027     *
028     * @version $Revision: 1.1.1.1 $
029     */
030    public class StatsImpl extends StatisticImpl implements Stats, Resettable{
031        private Map map;
032    
033        public StatsImpl() {
034            this(new HashMap());
035        }
036    
037        public StatsImpl(Map map) {
038            super("stats", "many", "Used only as container, not Statistic");
039            this.map = map;
040        }
041    
042        public void reset() {
043            Statistic[] stats = getStatistics();
044            for (int i = 0, size = stats.length; i < size; i++) {
045                Statistic stat = stats[i];
046                if (stat instanceof Resettable) {
047                    Resettable r = (Resettable) stat;
048                    r.reset();
049                }
050            }
051        }
052    
053        public Statistic getStatistic(String name) {
054            return (Statistic) map.get(name);
055        }
056    
057        public String[] getStatisticNames() {
058            Set keys = map.keySet();
059            String[] answer = new String[keys.size()];
060            keys.toArray(answer);
061            return answer;
062        }
063    
064        public Statistic[] getStatistics() {
065            Collection values = map.values();
066            Statistic[] answer = new Statistic[values.size()];
067            values.toArray(answer);
068            return answer;
069        }
070    
071        protected void addStatistic(String name, StatisticImpl statistic) {
072            map.put(name, statistic);
073        }
074    }