1   package com.stateofflow.eclipse.metrics.export.csv;
2   
3   import java.io.PrintWriter;
4   
5   import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
6   import com.stateofflow.eclipse.metrics.location.MetricLocation;
7   import com.stateofflow.eclipse.metrics.location.NamedLineNumber;
8   import com.stateofflow.eclipse.metrics.metric.MetricId;
9   
10  final class RowWriter {
11      private final MetricLocation location;
12      private final PrintWriter writer;
13  
14      public RowWriter(final PrintWriter writer, final MetricLocation location) {
15          this.writer = writer;
16          this.location = location;
17      }
18  
19      public void write(final MetricId[] metricIds, final MetricsCollator collator) {
20          writePreamble();
21          writeMetrics(metricIds, collator);
22          writer.println();
23      }
24  
25      private void writeMethodInfo(final NamedLineNumber methodInfo) {
26          writer.print(',');
27          writer.print(methodInfo.getName().substring(0, methodInfo.getName().indexOf('(')));
28          writer.print(',');
29          writer.print(methodInfo.getLineNumber());
30      }
31  
32      private void writeMetrics(final MetricId[] metricIds, final MetricsCollator collator) {
33          for (int i = 0; i < metricIds.length; i++) {
34              writer.print(',');
35              if (collator.hasMetric(location, metricIds[i])) {
36                  writer.print(collator.getMetric(location, metricIds[i]).getValue());
37              }
38          }
39      }
40  
41      private void writePackageAndType(final MetricLocation location) {
42          writer.print(location.getPackageName());
43          writer.print(',');
44          writer.print(location.getTypeInfo().getName());
45          if (!location.hasMethod()) {
46              writer.print(',');
47              writer.print(location.getTypeInfo().getLineNumber());
48          }
49      }
50  
51      private void writePreamble() {
52          writePackageAndType(location);
53          if (location.hasMethod()) {
54              writeMethodInfo(location.getMethodInfo());
55          }
56      }
57  }
58