1 | /* |
2 | * Copyright (c) 2001-2009, 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 java.lang.reflect.*; |
36 | |
37 | public class MeasurementDescriptor { |
38 | private static final Class constructorSignature[] = {MeasurementDescriptor.class, Metrics.class, String.class}; |
39 | |
40 | private String shortName; |
41 | private String longName; |
42 | private Class classFor; |
43 | private String initText; |
44 | private Comparable lowerThreshold; |
45 | private Comparable upperThreshold; |
46 | private boolean visible = true; |
47 | private boolean cached = true; |
48 | |
49 | public String getShortName() { |
50 | return shortName; |
51 | } |
52 | |
53 | public void setShortName(String shortName) { |
54 | this.shortName = shortName; |
55 | } |
56 | |
57 | public String getLongName() { |
58 | return longName; |
59 | } |
60 | |
61 | public void setLongName(String longName) { |
62 | this.longName = longName; |
63 | } |
64 | |
65 | public Class getClassFor() { |
66 | return classFor; |
67 | } |
68 | |
69 | public void setClassFor(Class classFor) { |
70 | if (classFor != null) { |
71 | this.classFor = classFor; |
72 | } else { |
73 | throw new IllegalArgumentException("class cannot be null"); |
74 | } |
75 | } |
76 | |
77 | public void getClassForByName(String className) throws ClassNotFoundException { |
78 | this.classFor = Class.forName(className); |
79 | } |
80 | |
81 | public String getInitText() { |
82 | return initText; |
83 | } |
84 | |
85 | public void setInitText(String initText) { |
86 | this.initText = initText; |
87 | } |
88 | |
89 | public Comparable getLowerThreshold() { |
90 | return lowerThreshold; |
91 | } |
92 | |
93 | public void setLowerThreshold(Comparable lowerThreshold) { |
94 | this.lowerThreshold = lowerThreshold; |
95 | } |
96 | |
97 | public Comparable getUpperThreshold() { |
98 | return upperThreshold; |
99 | } |
100 | |
101 | public void setUpperThreshold(Comparable upperThreshold) { |
102 | this.upperThreshold = upperThreshold; |
103 | } |
104 | |
105 | public boolean isVisible() { |
106 | return visible; |
107 | } |
108 | |
109 | public void setVisible(boolean visible) { |
110 | this.visible = visible; |
111 | } |
112 | |
113 | public boolean isCached() { |
114 | return cached; |
115 | } |
116 | |
117 | public void setCached(boolean cached) { |
118 | this.cached = cached; |
119 | } |
120 | |
121 | public Measurement createMeasurement() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { |
122 | return createMeasurement(null); |
123 | } |
124 | |
125 | public Measurement createMeasurement(Metrics context) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { |
126 | Measurement result = null; |
127 | |
128 | Constructor constructor = getClassFor().getConstructor(constructorSignature); |
129 | Object params[] = new Object[3]; |
130 | params[0] = this; |
131 | params[1] = context; |
132 | params[2] = getInitText(); |
133 | result = (Measurement) constructor.newInstance(params); |
134 | |
135 | return result; |
136 | } |
137 | |
138 | public String getRangeAsString() { |
139 | StringBuffer result = new StringBuffer(); |
140 | |
141 | result.append("["); |
142 | result.append((getLowerThreshold() != null) ? getLowerThreshold().toString() : "*"); |
143 | result.append(", "); |
144 | result.append((getUpperThreshold() != null) ? getUpperThreshold().toString() : "*"); |
145 | result.append("]"); |
146 | |
147 | return result.toString(); |
148 | } |
149 | } |