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.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 | * <init> |
49 | * [SET | LIST] |
50 | * </init> |
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<String> values; |
57 | |
58 | public NameListMeasurement(MeasurementDescriptor descriptor, Metrics context, String initText) { |
59 | super(descriptor, context, initText); |
60 | |
61 | if (initText != null) { |
62 | if (initText.trim().equalsIgnoreCase("list")) { |
63 | values = new LinkedList<String>(); |
64 | } else if (initText.trim().equalsIgnoreCase("set")) { |
65 | values = new HashSet<String>(); |
66 | } else { |
67 | Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\", using default value of SET instead"); |
68 | values = new HashSet<String>(); |
69 | } |
70 | } else { |
71 | Logger.getLogger(getClass()).debug("Cannot initialize with null text, using default value of SET instead"); |
72 | values = new HashSet<String>(); |
73 | } |
74 | } |
75 | |
76 | public void add(Object object) { |
77 | if (object instanceof String) { |
78 | values.add((String) object); |
79 | } |
80 | } |
81 | |
82 | public void accept(MeasurementVisitor visitor) { |
83 | visitor.visitNameListMeasurement(this); |
84 | } |
85 | |
86 | public Number getValue() { |
87 | return values.size(); |
88 | } |
89 | |
90 | public boolean isEmpty() { |
91 | return values.isEmpty(); |
92 | } |
93 | |
94 | protected double compute() { |
95 | return values.size(); |
96 | } |
97 | |
98 | public Collection<String> getValues() { |
99 | return Collections.unmodifiableCollection(values); |
100 | } |
101 | } |