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.ArrayList;
020    import java.util.List;
021    import java.util.Set;
022    import java.util.concurrent.CopyOnWriteArraySet;
023    import javax.management.j2ee.statistics.Statistic;
024    import javax.management.j2ee.statistics.Stats;
025    /**
026     * Base class for a Stats implementation
027     * 
028     * 
029     */
030    public class StatsImpl extends StatisticImpl implements Stats, Resettable {
031        //use a Set instead of a Map - to conserve Space
032        private Set<StatisticImpl> set;
033    
034        public StatsImpl() {
035            this(new CopyOnWriteArraySet<StatisticImpl>());
036        }
037    
038        public StatsImpl(Set<StatisticImpl> set) {
039            super("stats", "many", "Used only as container, not Statistic");
040            this.set = set;
041        }
042    
043        public void reset() {
044            Statistic[] stats = getStatistics();
045            int size = stats.length;
046            for (int i = 0; i < size; 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            for (StatisticImpl stat : this.set) {
057                if (stat.getName() != null && stat.getName().equals(name)) {
058                    return stat;
059                }
060            }
061            return null;
062        }
063    
064        public String[] getStatisticNames() {
065            List<String> names = new ArrayList<String>();
066            for (StatisticImpl stat : this.set) {
067                names.add(stat.getName());
068            }
069            String[] answer = new String[names.size()];
070            names.toArray(answer);
071            return answer;
072        }
073    
074        public Statistic[] getStatistics() {
075            Statistic[] answer = new Statistic[this.set.size()];
076            set.toArray(answer);
077            return answer;
078        }
079    
080        protected void addStatistic(String name, StatisticImpl statistic) {
081            this.set.add(statistic);
082        }
083    }