001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.management;
018    
019    import java.util.*;
020    import javax.management.j2ee.statistics.Statistic;
021    import javax.management.j2ee.statistics.Stats;
022    
023    /**
024     * Base class for a Stats implementation
025     * 
026     * @version $Revision: 1.2 $
027     */
028    public class StatsImpl extends StatisticImpl implements Stats, Resettable {
029        private Map<String, StatisticImpl> map;
030    
031        public StatsImpl() {
032            this(new HashMap<String, StatisticImpl>());
033        }
034    
035        public StatsImpl(Map<String, StatisticImpl> map) {
036            super("stats", "many", "Used only as container, not Statistic");
037            this.map = map;
038        }
039    
040        public void reset() {
041            Statistic[] stats = getStatistics();
042            int size = stats.length;
043            for (int i = 0; i < size; i++) {
044                Statistic stat = stats[i];
045                if (stat instanceof Resettable) {
046                    Resettable r = (Resettable)stat;
047                    r.reset();
048                }
049            }
050        }
051    
052        public Statistic getStatistic(String name) {
053            return map.get(name);
054        }
055    
056        public String[] getStatisticNames() {
057            Set<String> keys = map.keySet();
058            String[] answer = new String[keys.size()];
059            keys.toArray(answer);
060            return answer;
061        }
062    
063        public Statistic[] getStatistics() {
064            Collection<StatisticImpl> values = map.values();
065            Statistic[] answer = new Statistic[values.size()];
066            values.toArray(answer);
067            return answer;
068        }
069    
070        protected void addStatistic(String name, StatisticImpl statistic) {
071            map.put(name, statistic);
072        }
073    }