KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
36
37 import org.apache.log4j.*;
38
39 /**
40  * <p>Accumulates a set of values. Its numerical value is the
41  * cardinality (i.e., size) of the set. <code>OOMetrics</code>
42  * uses it to keep track of dependencies.</p>
43  *
44  * <p>This is the syntax for initializing this type of
45  * measurement:</p>
46  *
47  * <pre>
48  * &lt;init&gt;
49  * [SET | LIST]
50  * &lt;/init&gt;
51  * </pre>
52  *
53  * <p>Defaults to SET (i.e., does not count duplicates).</p>
54  */

55 public class NameListMeasurement extends MeasurementBase implements CollectionMeasurement {
56     private Collection values;
57
58     public NameListMeasurement(MeasurementDescriptor descriptor, Metrics context, String JavaDoc initText) {
59         super(descriptor, context, initText);
60
61         if (initText != null) {
62             if (initText.trim().equalsIgnoreCase("list")) {
63                 values = new LinkedList();
64             } else if (initText.trim().equalsIgnoreCase("set")) {
65                 values = new HashSet();
66             } else {
67                 Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\", using default value of SET instead");
68                 values = new HashSet();
69             }
70         } else {
71             Logger.getLogger(getClass()).debug("Cannot initialize with null text, using default value of SET instead");
72             values = new HashSet();
73         }
74     }
75
76     public void add(Object JavaDoc object) {
77         values.add(object);
78     }
79
80     public void accept(MeasurementVisitor visitor) {
81         visitor.visitNameListMeasurement(this);
82     }
83
84     public Number JavaDoc getValue() {
85         return new Integer JavaDoc(values.size());
86     }
87
88     public boolean isEmpty() {
89         return values.isEmpty();
90     }
91
92     protected double compute() {
93         return values.size();
94     }
95
96     public Collection getValues() {
97         return Collections.unmodifiableCollection(values);
98     }
99 }
100
Popular Tags