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 java.util.*;
036:
037: import org.apache.log4j.*;
038:
039: /**
040: * <p>Accumulates a set of values. Its numerical value is the
041: * cardinality (i.e., size) of the set. <code>OOMetrics</code>
042: * uses it to keep track of dependencies.</p>
043: *
044: * <p>This is the syntax for initializing this type of
045: * measurement:</p>
046: *
047: * <pre>
048: * <init>
049: * [SET | LIST]
050: * </init>
051: * </pre>
052: *
053: * <p>Defaults to SET (i.e., does not count duplicates).</p>
054: */
055: public class NameListMeasurement extends MeasurementBase implements
056: CollectionMeasurement {
057: private Collection<String> values;
058:
059: public NameListMeasurement(MeasurementDescriptor descriptor,
060: Metrics context, String initText) {
061: super (descriptor, context, initText);
062:
063: if (initText != null) {
064: if (initText.trim().equalsIgnoreCase("list")) {
065: values = new LinkedList<String>();
066: } else if (initText.trim().equalsIgnoreCase("set")) {
067: values = new HashSet<String>();
068: } else {
069: Logger
070: .getLogger(getClass())
071: .debug(
072: "Cannot initialize with \""
073: + initText
074: + "\", using default value of SET instead");
075: values = new HashSet<String>();
076: }
077: } else {
078: Logger
079: .getLogger(getClass())
080: .debug(
081: "Cannot initialize with null text, using default value of SET instead");
082: values = new HashSet<String>();
083: }
084: }
085:
086: public void add(Object object) {
087: if (object instanceof String) {
088: values.add((String) object);
089: }
090: }
091:
092: public void accept(MeasurementVisitor visitor) {
093: visitor.visitNameListMeasurement(this );
094: }
095:
096: public Number getValue() {
097: return values.size();
098: }
099:
100: public boolean isEmpty() {
101: return values.isEmpty();
102: }
103:
104: protected double compute() {
105: return values.size();
106: }
107:
108: public Collection<String> getValues() {
109: return Collections.unmodifiableCollection(values);
110: }
111: }
|