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: import org.apache.oro.text.perl.*;
036:
037: import com.jeantessier.text.*;
038:
039: public abstract class MeasurementBase implements Measurement {
040: private static final Perl5Util perl = new Perl5Util(
041: new MaximumCapacityPatternCache());
042:
043: protected static Perl5Util perl() {
044: return perl;
045: }
046:
047: private MeasurementDescriptor descriptor = null;
048: private Metrics context = null;
049:
050: private boolean cached = false;
051: private boolean empty = true;
052:
053: public MeasurementBase(MeasurementDescriptor descriptor,
054: Metrics context, String initText) {
055: this .descriptor = descriptor;
056: this .context = context;
057: }
058:
059: public MeasurementDescriptor getDescriptor() {
060: return descriptor;
061: }
062:
063: public Metrics getContext() {
064: return context;
065: }
066:
067: /**
068: * Tells this instance if it should reuse a previously
069: * computed value or if it should regenerate it.
070: */
071: protected boolean isCached() {
072: return cached;
073: }
074:
075: /**
076: * Sets the caching flag, telling this instance if
077: * its value has been computed. This flag is
078: * conditional to caching being enabled in the
079: * corresponding descriptor.
080: */
081: protected void setCached(boolean cached) {
082: this .cached = cached && getDescriptor().isCached();
083: }
084:
085: public boolean isEmpty() {
086: return empty;
087: }
088:
089: protected void setEmpty(boolean empty) {
090: this .empty = empty;
091: }
092:
093: public String getShortName() {
094: return getDescriptor().getShortName();
095: }
096:
097: public String getLongName() {
098: return getDescriptor().getLongName();
099: }
100:
101: public Number getValue() {
102: return compute();
103: }
104:
105: public int intValue() {
106: return (int) compute();
107: }
108:
109: public long longValue() {
110: return (long) compute();
111: }
112:
113: public float floatValue() {
114: return (float) compute();
115: }
116:
117: public double doubleValue() {
118: return compute();
119: }
120:
121: public boolean isInRange() {
122: boolean result = true;
123:
124: if (getDescriptor() != null) {
125: Comparable lowerThreshold = getDescriptor()
126: .getLowerThreshold();
127: Comparable upperThreshold = getDescriptor()
128: .getUpperThreshold();
129:
130: if (result && lowerThreshold != null) {
131: if (lowerThreshold instanceof String) {
132: try {
133: result = Double
134: .parseDouble((String) lowerThreshold) <= compute();
135: } catch (NumberFormatException ex) {
136: // Ignore
137: }
138: } else if (lowerThreshold instanceof Number) {
139: result = ((Number) lowerThreshold).doubleValue() <= compute();
140: } else {
141: result = lowerThreshold.compareTo(getValue()) <= 0;
142: }
143: }
144:
145: if (result && upperThreshold != null) {
146: if (upperThreshold instanceof String) {
147: try {
148: result = Double
149: .parseDouble((String) upperThreshold) >= compute();
150: } catch (NumberFormatException ex) {
151: // Ignore
152: }
153: } else if (upperThreshold instanceof Number) {
154: result = ((Number) upperThreshold).doubleValue() >= compute();
155: } else {
156: result = upperThreshold.compareTo(getValue()) >= 0;
157: }
158: }
159: }
160:
161: return result;
162: }
163:
164: public void add(Object object) {
165: // Do nothing
166: }
167:
168: public void add(int i) {
169: // Do nothing
170: }
171:
172: public void add(long l) {
173: // Do nothing
174: }
175:
176: public void add(float f) {
177: // Do nothing
178: }
179:
180: public void add(double d) {
181: // Do nothing
182: }
183:
184: protected abstract double compute();
185:
186: public String toString() {
187: return getValue().toString();
188: }
189: }
|