001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Aggregate.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy.stats;
030:
031: import com.sun.jbi.binding.proxy.ExchangeEntry;
032:
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: public class Aggregate extends StatBase implements java.lang.Cloneable {
038: class Entry {
039: int mCount;
040: StatBase mItem;
041:
042: Entry(StatBase item) {
043: mItem = item;
044: }
045: }
046:
047: private int mField;
048: private HashMap mMap;
049:
050: /** Creates a new instance of Aggregate */
051: public Aggregate(int field) {
052: mField = field;
053: mMap = new HashMap();
054: }
055:
056: public Object clone() {
057: Aggregate a = (Aggregate) super .clone();
058:
059: a.mMap = new HashMap();
060: return (a);
061: }
062:
063: public void apply(ExchangeEntry entry) {
064: String name = (String) entry.getStringField(mField);
065: Entry e;
066: StatBase value;
067:
068: e = (Entry) mMap.get(name);
069: if (e == null) {
070: if (mChild == null) {
071: e = new Entry(null);
072: } else {
073: e = new Entry((StatBase) mChild.clone());
074: }
075: mMap.put(name, e);
076: }
077: e.mCount++;
078: if (e.mItem != null) {
079: e.mItem.apply(entry);
080: }
081: if (mPeer != null) {
082: mPeer.apply(entry);
083: }
084: }
085:
086: public void clear() {
087: mMap = new HashMap();
088: super .clear();
089: }
090:
091: public String report(int depth) {
092: String name;
093: Long value;
094: StringBuffer sb = new StringBuffer();
095: long total = 0;
096:
097: for (Iterator i = mMap.entrySet().iterator(); i.hasNext();) {
098: Map.Entry me = (Map.Entry) i.next();
099: Entry e;
100:
101: for (int j = 0; j < depth; j++) {
102: sb.append(" ");
103: }
104: sb.append(ExchangeEntry.fieldToString(mField));
105: sb.append(" (");
106: sb.append((String) me.getKey());
107: e = (Entry) me.getValue();
108: sb.append(") Count(");
109: sb.append(e.mCount);
110: total += e.mCount;
111: sb.append(")\n");
112: if (e.mItem != null) {
113: sb.append(e.mItem.report(depth + 1));
114: }
115: }
116: for (int j = 0; j < depth; j++) {
117: sb.append(" ");
118: }
119: sb.append("Total (");
120: sb.append(total);
121: sb.append(")\n");
122: if (mPeer != null) {
123: sb.append(mPeer.report(depth));
124: }
125: return (sb.toString());
126: }
127: }
|