Clover coverage report - Dependency Finder
Coverage timestamp: Mon Nov 29 2010 15:00:50 PST
file stats: LOC: 186   Methods: 10
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AccumulatorMeasurement.java 86.4% 94.6% 90% 92%
coverage coverage
 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.io.*;
 36    import java.util.*;
 37   
 38    import org.apache.log4j.*;
 39   
 40    /**
 41    * <p>Base class that accumulates entries, filtering with regular
 42    * expressions. If no regular expressions are given, matches
 43    * everything for the given measurement, which must implement
 44    * the <code>CollectionMeasurement</code> interface. Regular
 45    * expressions matching using <code>Perl5Util</code> from
 46    * Jakarta-ORO. This measurement will use
 47    * <code>Perl5Util.group(1)</code> if not null, otherwise the
 48    * full string.</p>
 49    *
 50    * <p>This is the syntax for initializing this type of
 51    * measurement:</p>
 52    *
 53    * <pre>
 54    * &lt;init&gt;
 55    * measurement name [perl regular expression]
 56    * ...
 57    * &lt;/init&gt;
 58    * </pre>
 59    */
 60    public abstract class AccumulatorMeasurement extends MeasurementBase implements CollectionMeasurement {
 61    private Map<String, Collection<String>> terms = new HashMap<String, Collection<String>>();
 62    private Collection<String> values = new TreeSet<String>();
 63   
 64  34 public AccumulatorMeasurement(MeasurementDescriptor descriptor, Metrics context, String initText) {
 65  34 super(descriptor, context, initText);
 66   
 67  34 if (initText != null) {
 68  28 try {
 69  28 BufferedReader in = new BufferedReader(new StringReader(initText));
 70  28 String line;
 71   
 72  ? while ((line = in.readLine()) != null) {
 73  42 synchronized (perl()) {
 74  42 if (perl().match("/^\\s*(\\S+)\\s*(.*)/", line)) {
 75  42 String name = perl().group(1);
 76  42 String re = perl().group(2);
 77   
 78  42 Collection<String> res = terms.get(name);
 79  42 if (res == null) {
 80  40 res = new ArrayList<String>();
 81  40 terms.put(name, res);
 82    }
 83   
 84  42 if (re != null && re.length() > 0) {
 85  21 res.add(re);
 86    }
 87    }
 88    }
 89    }
 90   
 91  28 in.close();
 92    } catch (Exception ex) {
 93  0 Logger.getLogger(getClass()).debug("Cannot initialize with \"" + initText + "\"", ex);
 94  0 terms.clear();
 95    }
 96    }
 97   
 98  34 logTerms(initText);
 99    }
 100   
 101  34 private void logTerms(String initText) {
 102  34 Logger.getLogger(getClass()).debug("Initialize with\n" + initText);
 103  34 Logger.getLogger(getClass()).debug("Terms:");
 104   
 105  34 for (Map.Entry<String, Collection<String>> entry : terms.entrySet()) {
 106  40 Logger.getLogger(getClass()).debug("\t" + entry.getKey());
 107   
 108  40 for (String s : entry.getValue()) {
 109  21 Logger.getLogger(getClass()).debug("\t\t" + s);
 110    }
 111    }
 112    }
 113   
 114  46 public Number getValue() {
 115  46 return getValues().size();
 116    }
 117   
 118  7 public boolean isEmpty() {
 119  7 return getValues().isEmpty();
 120    }
 121   
 122  0 protected double compute() {
 123  0 return getValues().size();
 124    }
 125   
 126  117 public Collection<String> getValues() {
 127  117 if (!isCached()) {
 128  98 values.clear();
 129   
 130  98 populateValues();
 131   
 132  98 setCached(true);
 133    }
 134   
 135  117 return Collections.unmodifiableCollection(values);
 136    }
 137   
 138    protected abstract void populateValues();
 139   
 140  153 protected void filterMetrics(Metrics metrics) {
 141  153 for (Map.Entry<String, Collection<String>> entry : terms.entrySet()) {
 142  173 String name = entry.getKey();
 143  173 Collection<String> res = entry.getValue();
 144   
 145  173 Measurement measurement = metrics.getMeasurement(name);
 146  173 if (measurement instanceof CollectionMeasurement) {
 147  126 filterMeasurement((CollectionMeasurement) measurement, res);
 148    }
 149    }
 150    }
 151   
 152  126 private void filterMeasurement(CollectionMeasurement measurement, Collection<String> res) {
 153  126 if (res.isEmpty()) {
 154  41 values.addAll(measurement.getValues());
 155    } else {
 156  85 for (String member : measurement.getValues()) {
 157  171 filterElement(member, res);
 158    }
 159    }
 160    }
 161   
 162  171 private void filterElement(String element, Collection<String> res) {
 163  171 boolean found = false;
 164  171 Iterator<String> i = res.iterator();
 165  171 while (!found && i.hasNext()) {
 166  191 found = evaluateRE(i.next(), element);
 167    }
 168    }
 169   
 170  191 private boolean evaluateRE(String re, String element) {
 171  191 boolean result = false;
 172   
 173  191 synchronized (perl()) {
 174  191 if (perl().match(re, element)) {
 175  85 result = true;
 176  85 if (perl().group(1) != null) {
 177  13 values.add(perl().group(1));
 178    } else {
 179  72 values.add(element);
 180    }
 181    }
 182    }
 183   
 184  191 return result;
 185    }
 186    }