01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.metric;
12:
13: /**
14: * A metric not derived from other metrics.
15: */
16: public class BaseMetric extends Metric {
17:
18: private int index = -1;
19: private final int defaultCalc;
20:
21: public BaseMetric(String name, String displayName, String category,
22: String descr, int decimals, int defaultCalc) {
23: super (name, displayName, category, descr, decimals);
24: this .defaultCalc = defaultCalc;
25: }
26:
27: public final int getIndex() {
28: return index;
29: }
30:
31: public void setIndex(int index) {
32: this .index = index;
33: }
34:
35: public int getDefaultCalc() {
36: return defaultCalc;
37: }
38:
39: /**
40: * Get the value of this metric for the given range of samples in the
41: * data set.
42: * @param dataSet The raw data
43: * @param firstSampleNo The first sample
44: * @param lastSampleNo The last sample (inclusive)
45: * @param calc The duration of the sample range in seconds
46: */
47: public double get(MetricDataSource dataSet, int firstSampleNo,
48: int lastSampleNo, int calc, double seconds) {
49: // these calculations make negative values postive (for int rollover)
50: // by and'ing with 0xFFFFFFFFL
51: int a, b;
52: switch (calc) {
53:
54: case CALC_RAW:
55: return dataSet.getSample(lastSampleNo, index) & 0xFFFFFFFFL;
56:
57: case CALC_DELTA_PER_SECOND:
58: if (seconds > 0.0) {
59: a = dataSet.getSample(firstSampleNo, index);
60: b = dataSet.getSample(lastSampleNo, index);
61: return ((b & 0xFFFFFFFFL) - (a & 0xFFFFFFFFL))
62: / seconds;
63: }
64:
65: case CALC_DELTA:
66: a = dataSet.getSample(firstSampleNo, index);
67: b = dataSet.getSample(lastSampleNo, index);
68: return (b & 0xFFFFFFFFL) - (a & 0xFFFFFFFFL);
69:
70: case CALC_AVERAGE:
71: int n = lastSampleNo - firstSampleNo;
72: if (n <= 1) { // fast exit for simple common case
73: return dataSet.getSample(lastSampleNo, index);
74: }
75: double tot = dataSet.getSample(firstSampleNo + 1, index);
76: for (int i = firstSampleNo + 2; i <= lastSampleNo; i++) {
77: tot += dataSet.getSample(i, index);
78: }
79: return tot / n;
80: }
81: ;
82: throw new IllegalArgumentException("Unknown calc: " + calc);
83: }
84:
85: }
|