001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.j2ee.statistics;
023:
024: import javax.management.j2ee.statistics.Statistic;
025: import javax.management.j2ee.statistics.Stats;
026: import java.io.Serializable;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: /**
032: * The base JSR77.6.10 Stats interface base implementation
033: *
034: * @author Scott.Stark@jboss.org
035: * @version $Revision: 57197 $
036: */
037: public class StatsBase implements Stats, Serializable {
038: // Constants -----------------------------------------------------
039:
040: /** @since 4.0.2 */
041: private static final long serialVersionUID = 384207297746356032L;
042:
043: // Private Data --------------------------------------------------
044:
045: /**
046: * A Map<String,Statistic> of the statistics held by a given
047: * Stats implementation
048: */
049: private Map statistics;
050:
051: // Constructors --------------------------------------------------
052:
053: public StatsBase() {
054: statistics = new HashMap();
055: }
056:
057: public StatsBase(Map statistics) {
058: this .statistics = statistics;
059: }
060:
061: // Begin Stats interface methods
062:
063: /**
064: * Access all the Statistics names
065: *
066: * @return An array of the names of the statistics held in the Stats object
067: */
068: public String[] getStatisticNames() {
069: String[] names = new String[statistics.size()];
070: statistics.keySet().toArray(names);
071: return names;
072: }
073:
074: /**
075: * Access all the Statistics
076: *
077: * @return An array of the Statistic held in the Stats object
078: */
079: public Statistic[] getStatistics() {
080: Statistic[] stats = new Statistic[statistics.size()];
081: statistics.values().toArray(stats);
082: return stats;
083: }
084:
085: /**
086: * Access a Statistic by its name.
087: *
088: * @param name
089: * @return
090: */
091: public Statistic getStatistic(String name) {
092: Statistic stat = (Statistic) statistics.get(name);
093: return stat;
094: }
095:
096: // End Stats interface methods
097:
098: /**
099: * Reset all StatisticImpl objects
100: */
101: public void reset() {
102: Iterator iter = statistics.values().iterator();
103: while (iter.hasNext()) {
104: Object next = iter.next();
105: if (next instanceof StatisticImpl) {
106: StatisticImpl s = (StatisticImpl) next;
107: s.reset();
108: }
109: }
110: }
111:
112: public String toString() {
113: return this .getClass().getName() + " [ " + statistics + " ]";
114: }
115:
116: /**
117: * Add or replace Statistic in the Stats collection.
118: *
119: * @param name Name of the Statistic instance
120: * @param statistic Statistic to be added
121: */
122: public void addStatistic(String name, Statistic statistic) {
123: statistics.put(name, statistic);
124: }
125:
126: }
|