/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.activemq.management;

import java.util.*;
import javax.management.j2ee.statistics.Statistic;
import javax.management.j2ee.statistics.Stats;

/**
 * Base class for a Stats implementation
 * 
 * @version $Revision: 1.2 $
 */
public class StatsImpl extends StatisticImpl implements Stats, Resettable {
    private Map<String, StatisticImpl> map;

    public StatsImpl() {
        this(new HashMap<String, StatisticImpl>());
    }

    public StatsImpl(Map<String, StatisticImpl> map) {
        super("stats", "many", "Used only as container, not Statistic");
        this.map = map;
    }

    public void reset() {
        Statistic[] stats = getStatistics();
        int size = stats.length;
        for (int i = 0; i < size; i++) {
            Statistic stat = stats[i];
            if (stat instanceof Resettable) {
                Resettable r = (Resettable)stat;
                r.reset();
            }
        }
    }

    public Statistic getStatistic(String name) {
        return map.get(name);
    }

    public String[] getStatisticNames() {
        Set<String> keys = map.keySet();
        String[] answer = new String[keys.size()];
        keys.toArray(answer);
        return answer;
    }

    public Statistic[] getStatistics() {
        Collection<StatisticImpl> values = map.values();
        Statistic[] answer = new Statistic[values.size()];
        values.toArray(answer);
        return answer;
    }

    protected void addStatistic(String name, StatisticImpl statistic) {
        map.put(name, statistic);
    }
}

@@ -855,17 +855,119 @@
 va.util.
-*
+ArrayList;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport java.util.concurrent.CopyOnWriteArraySet
 ;%0Aimport
@@ -1058,17 +1058,16 @@
 .Stats;%0A
-%0A
 /**%0A * B
@@ -1224,28 +1224,73 @@
     
-private Map%3CString, 
+//use a Set instead of a Map - to conserve Space%0A    private Set%3C
 Stat
@@ -1300,19 +1300,19 @@
 icImpl%3E 
-map
+set
 ;%0A%0A    p
@@ -1352,24 +1352,28 @@
 new 
-HashMap%3CString, 
+CopyOnWriteArraySet%3C
 Stat
@@ -1419,20 +1419,12 @@
 mpl(
-Map%3CString, 
+Set%3C
 Stat
@@ -1434,19 +1434,19 @@
 icImpl%3E 
-map
+set
 ) %7B%0A    
@@ -1531,17 +1531,17 @@
 his.
-map = map
+set = set
 ;%0A  
@@ -1818,16 +1818,17 @@
 ettable)
+ 
 stat;%0A  
@@ -1944,28 +1944,191 @@
     
-return map.get(name)
+for (StatisticImpl stat : this.set) %7B%0A            if (stat.getName() != null && stat.getName().equals(name)) %7B%0A                return stat;%0A            %7D%0A        %7D%0A        return null
 ;%0A  
@@ -2182,18 +2182,19 @@
         
-Se
+Lis
 t%3CString
@@ -2199,29 +2199,136 @@
 ng%3E 
-keys = map.keySet(
+names = new ArrayList%3CString%3E();%0A        for (StatisticImpl stat : this.set) %7B%0A            names.add(stat.getName()
 );%0A
+        %7D%0A
     
@@ -2360,19 +2360,20 @@
  String%5B
-key
+name
 s.size()
@@ -2387,11 +2387,12 @@
     
-key
+name
 s.to
@@ -2481,65 +2481,8 @@
 ) %7B%0A
-        Collection%3CStatisticImpl%3E values = map.values();%0A
     
@@ -2520,22 +2520,24 @@
 atistic%5B
-values
+this.set
 .size()%5D
@@ -2550,14 +2550,11 @@
     
-values
+set
 .toA
@@ -2681,22 +2681,21 @@
     
-map.put(name, 
+this.set.add(
 stat

use a CopyOnWriteSet instead of a Map - to reduce memory footprint