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.servicemix.jbi.monitoring.stats;
018    
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import javax.management.j2ee.statistics.Statistic;
025    import javax.management.j2ee.statistics.Stats;
026    
027    /**
028     * Base class for a Stats implementation
029     *
030     * @version $Revision: 564607 $
031     */
032    public class StatsImpl extends StatisticImpl implements Stats, Resettable {
033        private Map<String, StatisticImpl> map;
034    
035        public StatsImpl() {
036            this(new HashMap<String, StatisticImpl>());
037        }
038    
039        public StatsImpl(Map<String, StatisticImpl> map) {
040            super("stats", "many", "Used only as container, not Statistic");
041            this.map = map;
042        }
043    
044        public void reset() {
045            Statistic[] stats = getStatistics();
046            for (int i = 0; i < stats.length; i++) {
047                Statistic stat = stats[i];
048                if (stat instanceof Resettable) {
049                    Resettable r = (Resettable) stat;
050                    r.reset();
051                }
052            }
053        }
054    
055        public Statistic getStatistic(String name) {
056            return map.get(name);
057        }
058    
059        public String[] getStatisticNames() {
060            Set<String> keys = map.keySet();
061            String[] answer = new String[keys.size()];
062            keys.toArray(answer);
063            return answer;
064        }
065    
066        public Statistic[] getStatistics() {
067            Collection<StatisticImpl> values = map.values();
068            Statistic[] answer = new Statistic[values.size()];
069            values.toArray(answer);
070            return answer;
071        }
072    
073        protected void addStatistic(String name, StatisticImpl statistic) {
074            map.put(name, statistic);
075        }
076    }