1 /**
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 package cppncss.measure;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35 * Stores a result measure.
36 *
37 * @author Mathieu Champlon
38 */
39 public final class Measure implements Comparable
40 {
41 private final List<Integer> counts = new ArrayList<Integer>();
42 private final String item;
43 private final String filename;
44 private final int line;
45
46 /**
47 * Create a measure.
48 *
49 * @param item the name of the measured item
50 * @param filename the name of the file containing the item
51 * @param line the location of the item within the file
52 * @param count the value of the measure
53 */
54 public Measure( final String item, final String filename, final int line, final int count )
55 {
56 if( item == null )
57 throw new IllegalArgumentException( "argument 'item' is null" );
58 if( filename == null )
59 throw new IllegalArgumentException( "argument 'filename' is null" );
60 if( line < 0 )
61 throw new IllegalArgumentException( "argument 'line' is < 0" );
62 counts.add( count );
63 this.item = item;
64 this.filename = filename;
65 this.line = line;
66 }
67
68 /**
69 * Add a measure value to the recorded values.
70 * <p>
71 * If the item name does not match the measure the value is not recorded.
72 *
73 * @param item the item name
74 * @param filename the file name of the item
75 * @param line the location of the item
76 * @param count the measure to record
77 */
78 public void update( final String item, final String filename, final int line, final int count )
79 {
80 if( matches( item, filename, line ) )
81 counts.add( count );
82 }
83
84 /**
85 * Accept a visitor.
86 *
87 * @param observer a measure observer
88 */
89 public void accept( final MeasureObserver observer )
90 {
91 for( Integer count : counts )
92 observer.notify( toString(), count );
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 public String toString()
99 {
100 if( filename.equals( item ) )
101 return item;
102 return item + " at " + filename + ":" + line;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public int compareTo( final Object object )
109 {
110 final Measure measure = (Measure)object;
111 if( matches( measure.item, measure.filename, measure.line ) )
112 return 0;
113 final int delta = measure.counts.get( 0 ) - counts.get( 0 );
114 if( delta == 0 )
115 return 1;
116 return delta;
117 }
118
119 private boolean matches( final String item, final String filename, final int line )
120 {
121 return this.item.equals( item ) && this.filename.equals( filename ) && this.line == line;
122 }
123 }