001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: /**
036: * <p>A simple counter, it tallies the values that are put in it.
037: * If you try to add a non-number, it simply adds 1.</p>
038: *
039: * <p>This is the syntax for initializing this type of
040: * measurement:</p>
041: *
042: * <pre>
043: * <init>
044: * [initial value]
045: * </init>
046: * </pre>
047: */
048: public class CounterMeasurement extends MeasurementBase {
049: private double value;
050:
051: public CounterMeasurement(MeasurementDescriptor descriptor,
052: Metrics context, String initText) {
053: super (descriptor, context, initText);
054:
055: try {
056: if (initText != null) {
057: value = Double.parseDouble(initText);
058: }
059: } catch (NumberFormatException ex) {
060: value = 0;
061: }
062: }
063:
064: public void add(Object object) {
065: if (object instanceof Number) {
066: value += ((Number) object).doubleValue();
067: } else {
068: value++;
069: }
070:
071: setEmpty(false);
072: }
073:
074: public void add(int i) {
075: value += i;
076: setEmpty(false);
077: }
078:
079: public void add(long l) {
080: value += l;
081: setEmpty(false);
082: }
083:
084: public void add(float f) {
085: value += f;
086: setEmpty(false);
087: }
088:
089: public void add(double d) {
090: value += d;
091: setEmpty(false);
092: }
093:
094: public void accept(MeasurementVisitor visitor) {
095: visitor.visitCounterMeasurement(this );
096: }
097:
098: protected double compute() {
099: return value;
100: }
101: }
|