KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MeasurementBase


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import org.apache.oro.text.perl.*;
36
37 import com.jeantessier.text.*;
38
39 public abstract class MeasurementBase implements Measurement {
40     private static final Perl5Util perl = new Perl5Util(new MaximumCapacityPatternCache());
41
42     protected static Perl5Util perl() {
43         return perl;
44     }
45     
46     private MeasurementDescriptor descriptor = null;
47     private Metrics context = null;
48
49     private boolean cached = false;
50     private boolean empty = true;
51
52     public MeasurementBase(MeasurementDescriptor descriptor, Metrics context, String JavaDoc initText) {
53         this.descriptor = descriptor;
54         this.context = context;
55     }
56     
57     public MeasurementDescriptor getDescriptor() {
58         return descriptor;
59     }
60     
61     public Metrics getContext() {
62         return context;
63     }
64
65     /**
66      * Tells this instance if it should reuse a previously
67      * computed value or if it should regenerate it.
68      */

69     protected boolean isCached() {
70         return cached;
71     }
72
73     /**
74      * Sets the caching flag, telling this instance if
75      * its value has been computed. This flag is
76      * conditional to caching being enabled in the
77      * corresponding descriptor.
78      */

79     protected void setCached(boolean cached) {
80         this.cached = cached && getDescriptor().isCached();
81     }
82
83     public boolean isEmpty() {
84         return empty;
85     }
86
87     protected void setEmpty(boolean empty) {
88         this.empty = empty;
89     }
90     
91     public String JavaDoc getShortName() {
92         return getDescriptor().getShortName();
93     }
94     
95     public String JavaDoc getLongName() {
96         return getDescriptor().getLongName();
97     }
98     
99     public Number JavaDoc getValue() {
100         return new Double JavaDoc(compute());
101     }
102
103     public int intValue() {
104         return (int) compute();
105     }
106
107     public long longValue() {
108         return (long) compute();
109     }
110
111     public float floatValue() {
112         return (float) compute();
113     }
114
115     public double doubleValue() {
116         return compute();
117     }
118     
119     public boolean isInRange() {
120         boolean result = true;
121
122         if (getDescriptor() != null) {
123             Comparable JavaDoc lowerThreshold = getDescriptor().getLowerThreshold();
124             Comparable JavaDoc upperThreshold = getDescriptor().getUpperThreshold();
125
126             if (result && lowerThreshold != null) {
127                 if (lowerThreshold instanceof String JavaDoc) {
128                     try {
129                         result = Double.parseDouble((String JavaDoc) lowerThreshold) <= compute();
130                     } catch (NumberFormatException JavaDoc ex) {
131                         // Ignore
132
}
133                 } else if (lowerThreshold instanceof Number JavaDoc) {
134                     result = ((Number JavaDoc) lowerThreshold).doubleValue() <= compute();
135                 } else {
136                     result = lowerThreshold.compareTo(getValue()) <= 0;
137                 }
138             }
139             
140             if (result && upperThreshold != null) {
141                 if (upperThreshold instanceof String JavaDoc) {
142                     try {
143                         result = Double.parseDouble((String JavaDoc) upperThreshold) >= compute();
144                     } catch (NumberFormatException JavaDoc ex) {
145                         // Ignore
146
}
147                 } else if (upperThreshold instanceof Number JavaDoc) {
148                     result = ((Number JavaDoc) upperThreshold).doubleValue() >= compute();
149                 } else {
150                     result = upperThreshold.compareTo(getValue()) >= 0;
151                 }
152             }
153         }
154         
155         return result;
156     }
157     
158     public void add(Object JavaDoc object) {
159         // Do nothing
160
}
161
162     public void add(int i) {
163         // Do nothing
164
}
165     
166     public void add(long l) {
167         // Do nothing
168
}
169     
170     public void add(float f) {
171         // Do nothing
172
}
173     
174     public void add(double d) {
175         // Do nothing
176
}
177     
178     protected abstract double compute();
179
180     public String JavaDoc toString() {
181         return getValue().toString();
182     }
183 }
184
Popular Tags