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.cocoon.profiling.statistics;
018:
019: /**
020: *
021: * @version $Id: ReportImpl.java 485636 2006-12-11 12:20:15Z cziegeler $
022: * @since 2.1.10
023: */
024: public class ReportImpl implements Report {
025:
026: protected int count;
027: protected long min = -1;
028: protected long max = 0;
029: protected long accumulated;
030: protected long last;
031: protected String category;
032: protected StringBuffer buffer = new StringBuffer();
033:
034: public ReportImpl(String category) {
035: this .category = category;
036: }
037:
038: public void add(Statistics stat) {
039: if (stat != null) {
040: long duration = stat.getDuration();
041: if (duration == 0) {
042: duration = 1;
043: }
044: this .count++;
045: this .accumulated += duration;
046: if (this .min == -1 || duration < this .min) {
047: this .min = duration;
048: }
049: if (duration > this .max) {
050: this .max = duration;
051: }
052: this .last = duration;
053: if (buffer.length() > 0) {
054: buffer.append(", ");
055: }
056: buffer.append(this .getTime(duration));
057: }
058: }
059:
060: protected String getTime(long msecs) {
061: long secs = msecs / 1000;
062: StringBuffer b = new StringBuffer();
063: b.append(secs);
064: b.append('.');
065: long rest = (msecs - secs * 1000);
066: if (rest < 100) {
067: b.append('0');
068: }
069: if (rest < 10) {
070: b.append('0');
071: }
072: b.append(rest);
073: b.append('s');
074: return b.toString();
075: }
076:
077: /**
078: * @see org.apache.cocoon.profiling.statistics.Report#getAverage()
079: */
080: public long getAverage() {
081: if (count > 0) {
082: return accumulated / count;
083: }
084: return 0;
085: }
086:
087: /**
088: * @see org.apache.cocoon.profiling.statistics.Report#getCategory()
089: */
090: public String getCategory() {
091: return this .category;
092: }
093:
094: /**
095: * @see org.apache.cocoon.profiling.statistics.Report#getCount()
096: */
097: public int getCount() {
098: return this .count;
099: }
100:
101: /**
102: * @see org.apache.cocoon.profiling.statistics.Report#getMax()
103: */
104: public long getMax() {
105: return this .max;
106: }
107:
108: /**
109: * @see org.apache.cocoon.profiling.statistics.Report#getMin()
110: */
111: public long getMin() {
112: return this .min;
113: }
114:
115: /**
116: * @see org.apache.cocoon.profiling.statistics.Report#getLast()
117: */
118: public long getLast() {
119: return this .last;
120: }
121:
122: /**
123: * @see org.apache.cocoon.profiling.statistics.Report#getAll()
124: */
125: public String getAll() {
126: return buffer.toString();
127: }
128: }
|