KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metric > BaseMetric


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metric;
13
14 /**
15  * A metric not derived from other metrics.
16  */

17 public class BaseMetric extends Metric {
18
19     private int index = -1;
20     private final int defaultCalc;
21
22     public BaseMetric(String JavaDoc name, String JavaDoc displayName, String JavaDoc category,
23             String JavaDoc descr, int decimals, int defaultCalc) {
24         super(name, displayName, category, descr, decimals);
25         this.defaultCalc = defaultCalc;
26     }
27
28     public final int getIndex() {
29         return index;
30     }
31
32     public void setIndex(int index) {
33         this.index = index;
34     }
35
36     public int getDefaultCalc() {
37         return defaultCalc;
38     }
39
40     /**
41      * Get the value of this metric for the given range of samples in the
42      * data set.
43      * @param dataSet The raw data
44      * @param firstSampleNo The first sample
45      * @param lastSampleNo The last sample (inclusive)
46      * @param calc The duration of the sample range in seconds
47      */

48     public double get(MetricDataSource dataSet, int firstSampleNo, int lastSampleNo,
49             int calc, double seconds) {
50         // these calculations make negative values postive (for int rollover)
51
// by and'ing with 0xFFFFFFFFL
52
int a, b;
53         switch (calc) {
54
55             case CALC_RAW:
56                 return dataSet.getSample(lastSampleNo, index) & 0xFFFFFFFFL;
57
58             case CALC_DELTA_PER_SECOND:
59                 if (seconds > 0.0) {
60                     a = dataSet.getSample(firstSampleNo, index);
61                     b = dataSet.getSample(lastSampleNo, index);
62                     return ((b & 0xFFFFFFFFL) - (a & 0xFFFFFFFFL)) / 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         throw new IllegalArgumentException JavaDoc("Unknown calc: " + calc);
82     }
83
84 }
85
Popular Tags