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: * @(#)Calculate.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: public class Calculate extends StatBase implements java.lang.Cloneable {
034: private int mField;
035: private int mDisplay;
036: private long count;
037: private long min;
038: private long max;
039: private double sum;
040: private double squaredsum;
041:
042: public static final int CNT = 1;
043: public static final int MIN = 2;
044: public static final int MAX = 4;
045: public static final int SUM = 8;
046: public static final int AVG = 16;
047: public static final int STD = 32;
048:
049: /** Creates a new instance of Calculate */
050: public Calculate(int field) {
051: mField = field;
052: mDisplay = MIN | MAX | SUM | AVG;
053: }
054:
055: public Calculate(int field, int display) {
056: mField = field;
057: mDisplay = display;
058: }
059:
060: public void apply(ExchangeEntry entry) {
061: long value;
062:
063: count++;
064:
065: if (mField != 0) {
066: value = entry.getLongField(mField);
067:
068: sum += value;
069: if ((mDisplay & STD) != 0) {
070: squaredsum += (double) value * (double) value;
071: }
072: if (count == 1) {
073: min = max = value;
074: } else {
075: if (value < min) {
076: min = value;
077: } else if (value > max) {
078: max = value;
079: }
080: }
081: }
082: super .apply(entry);
083: }
084:
085: public void clear() {
086: count = 0;
087: min = 0;
088: max = 0;
089: sum = 0.0;
090: squaredsum = 0.0;
091: super .clear();
092: }
093:
094: public String report(int depth) {
095: StringBuffer sb = new StringBuffer();
096:
097: for (int j = 0; j < depth; j++) {
098: sb.append(" ");
099: }
100: if (mField != 0) {
101: sb.append(ExchangeEntry.fieldToString(mField));
102: }
103: if ((mDisplay & CNT) != 0) {
104: sb.append(" C(");
105: sb.append(count);
106: sb.append(")");
107: }
108: if ((mDisplay & MIN) != 0) {
109: sb.append(" M(");
110: sb.append(min);
111: sb.append(")");
112: }
113: if ((mDisplay & MAX) != 0) {
114: sb.append(" X(");
115: sb.append(max);
116: sb.append(")");
117: }
118: if ((mDisplay & SUM) != 0) {
119: sb.append(" T(");
120: sb.append(sum);
121: sb.append(")");
122: }
123: if ((mDisplay & AVG) != 0) {
124: sb.append(" V(");
125: sb.append((double) ((long) (sum / count * 100)) / 100.);
126: sb.append(")");
127: }
128: if ((mDisplay & STD) != 0) {
129: sb.append(") S(");
130: sb
131: .append((double) ((long) (Math
132: .sqrt((1.0 / (count - 1))
133: * (squaredsum - (sum * sum / count))) * 100)) / 100.);
134: sb.append(")");
135: }
136: sb.append("\n");
137: if (mChild != null) {
138: sb.append(mChild.report(depth + 1));
139: }
140: if (mPeer != null) {
141: sb.append(mPeer.report(depth));
142: }
143: return (sb.toString());
144: }
145:
146: }
|