Source code: com/further/jaudit/EnumeratedTypeMetric.java
1 /*
2 * EnumeratedTypeMetric.java
3 * Copyright (c) 2001, Kristopher Wehner
4 * Created on September 21, 2001, 10:19 PM
5 */
6
7 package com.further.jaudit;
8
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.LinkedList;
12
13 /**
14 * An enumerated type metric is used to measure a source metric that has one
15 * of many values, and whose values are typically ranked from 1 to N. These
16 * values are strings like "Good", "Better", "Best" where a satisfactory
17 * value would be at least "Better". The exact type strings for the enumerated
18 * type are user defined, along with the name and abbreviation.
19 *
20 * @author Kris Wehner <kris@further.com>
21 * @version $Id: EnumeratedTypeMetric.java,v 1.1.1.1 2001/10/11 16:42:06 krisw Exp $
22 * @since 1.0
23 */
24 public class EnumeratedTypeMetric implements SourceMetric {
25 /**
26 * The metric name, like "Perceived Quality"
27 */
28 private String metricName;
29 /**
30 * The metric abbreviation, like "PERQ"
31 */
32 private String metricAbbreviation;
33 /**
34 * The metric description, like "The perceived quality of the source,
35 * according to the viewer"
36 */
37 private String metricDescription;
38 /**
39 * The current string value of the metric, which must appear in the
40 * possible values set
41 */
42 private String currentValue;
43 /**
44 * The possible values for the metric, in order. The element in this
45 * list at position 0 is the highest ranked value for the enumeration,
46 * and the element at position N is the lowest ranked value.
47 */
48 private List possibleValues = new LinkedList();
49 /**
50 * The required value for the enumerated type. If the current value
51 * of the metric is at a position that is numerically less than or
52 * equal to the position of this value in the possible value list,
53 * then the metric has an acceptable value
54 */
55 private String requiredValue;
56
57
58 /**
59 * Create a new EnumeratedTypeMetric with the given name, abbreviation,
60 * list of possible values and required value.
61 *
62 * @param name The metric name
63 * @param abbreviation The metric abbreviation, unique across all metrics
64 * @param values The possible values the metric may take, in order from most
65 * to least desirable.
66 * @param requiredValue The required value that the metric should have in
67 * order for it to be satisfied
68 */
69 public EnumeratedTypeMetric(String name, String abbreviation, String description, List values, String requiredValue) {
70 metricName = name;
71 metricAbbreviation = abbreviation;
72 metricDescription = description;
73 possibleValues = values;
74 currentValue = (String)possibleValues.get(0);
75 this.requiredValue = requiredValue;
76 }
77
78 /**
79 * Get the possible values for the enumerated type metric. These are the
80 * displayable strings that can be set as the metric values.
81 *
82 * @return The list of possible values that can be set for this metric
83 */
84 public List getPossibleValues() {
85 return possibleValues;
86 }
87
88 /**
89 * Does the given metric meet the minimum value for the given metric type?
90 * This allows a fast evaluation of whether or not the metric "passes",
91 * and can be used to flag a file that needs modification.
92 *
93 * @return true If the metric value is at least the minimum value, false otherwise
94 */
95 public boolean hasSatisfactoryValue() {
96 boolean foundRequiredValue = false;
97 for (Iterator i = possibleValues.iterator(); i.hasNext(); ) {
98 String value = (String)i.next();
99 if (value.equals(currentValue) && !foundRequiredValue)
100 return true;
101 if (value.equals(requiredValue))
102 foundRequiredValue = true;
103 }
104 return false;
105 }
106
107 /**
108 * Set the metric value to the given value. This checks to ensure that the
109 * specified value is a value enumerated type value.
110 *
111 * @param value The value to set the metric to, which must be a value
112 * enumerated type value
113 * @throws IllegalArgumentException If the value is not a valid enumerated
114 * type value
115 */
116 public void setMetricValue(String value) {
117 if (!possibleValues.contains(value))
118 throw new IllegalArgumentException(value + " is not a valid metric value");
119 currentValue = value;
120 }
121
122 /**
123 * Get the current metric value, which will be one of the possible metric
124 * values for the enumerated type.
125 *
126 * @return The current value of the metric
127 */
128 public String getMetricValue() {
129 return currentValue;
130 }
131
132 /**
133 * Retreive the metric name for this particular metric. This
134 * is a descriptive tag, like "Coherance" or "Code Quality".
135 *
136 * @return The descriptive name of the source metric.
137 */
138 public String getMetricName() {
139 return metricName;
140 }
141
142 /**
143 * Get the description for the metric, in a verbose fashion suitable
144 * to explain to a new user what the metric is rating.
145 *
146 * @return The description of the source metric
147 */
148 public String getDescription() {
149 return metricDescription;
150 }
151
152 /**
153 * Fetch the metric abbreviation for the current metric. This
154 * is something like "COH" or "COQ". This should be recognizable
155 * by the user, and is used when screen real estate is at a premium,
156 * such as in history lists.
157 *
158 * @return The abbreviated name of the source metric.
159 */
160 public String getMetricAbbreviation() {
161 return metricAbbreviation;
162 }
163
164 /**
165 * Accept the given SourceMetricVisitor, calling back to the appropriate
166 * visitMetric() method on the visitor.
167 *
168 * @param visitor The SourceMetricVisitor to accept
169 */
170 public void accept(SourceMetricVisitor visitor) {
171 visitor.visitMetric(this);
172 }
173
174 }