|
3 | 3 | import au.com.bytecode.opencsv.CSVWriter; |
4 | 4 | import com.google.common.base.Function; |
5 | 5 | import com.google.common.base.Objects; |
6 | | -import com.google.common.collect.FluentIterable; |
| 6 | +import com.google.common.collect.*; |
7 | 7 | import nl.rug.jbi.jsm.core.calculator.MetricScope; |
8 | 8 |
|
9 | 9 | import java.io.Closeable; |
10 | 10 | import java.io.File; |
11 | 11 | import java.io.FileWriter; |
12 | 12 | import java.io.IOException; |
13 | 13 | import java.text.NumberFormat; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.List; |
14 | 16 | import java.util.Map; |
15 | 17 |
|
16 | 18 | import static com.google.common.base.Preconditions.checkArgument; |
@@ -96,7 +98,8 @@ private File getFileForName(final String identifier, final boolean strict) throw |
96 | 98 | * @param results Map with results for the given metric and scope. |
97 | 99 | * @throws IOException If it gets thrown by the underlying CSVWriter |
98 | 100 | */ |
99 | | - public void exportData(final Class metricClass, final MetricScope scope, final Map<String, Object> results) throws IOException { |
| 101 | + public void exportData(final Class metricClass, final MetricScope scope, final Map<String, Object> results) |
| 102 | + throws IOException { |
100 | 103 | checkArgument(metricClass != null); |
101 | 104 | assert metricClass != null; |
102 | 105 | checkArgument(scope != null); |
@@ -132,10 +135,84 @@ public String[] apply(Map.Entry<String, Object> entry) { |
132 | 135 | .toList() |
133 | 136 | ); |
134 | 137 | } finally { |
135 | | - writer.close(); |
| 138 | + try { |
| 139 | + writer.close(); |
| 140 | + } catch (IOException ignored) { |
| 141 | + //Prevent finally block from overriding exceptions |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + /** |
| 148 | + * Exports a data set collection, creates a file to contain the results, writes the filename to the mapping file |
| 149 | + * and then write all the data from the given table to that file. |
| 150 | + * |
| 151 | + * @param scope Scope of the results getting exported. |
| 152 | + * @param results Map with results for the given scope. |
| 153 | + * @throws IOException If it gets thrown by the underlying CSVWriter |
| 154 | + */ |
| 155 | + public void exportDataCollection(final MetricScope scope, final Table<String, Class, Object> results) |
| 156 | + throws IOException { |
| 157 | + checkArgument(scope != null); |
| 158 | + assert scope != null; |
| 159 | + checkArgument(results != null); |
| 160 | + assert results != null; |
| 161 | + |
| 162 | + final File metricOutput = getFileForName(scope.toString(), false); |
| 163 | + |
| 164 | + //Output mappings |
| 165 | + final String outputFileName = metricOutput.toString(); |
| 166 | + for (final Class c : results.columnKeySet()) { |
| 167 | + this.mappingWriter.writeNext(new String[]{c.getName(), scope.toString(), outputFileName}); |
| 168 | + } |
| 169 | + this.mappingWriter.flush(); |
| 170 | + |
| 171 | + //Write results |
| 172 | + final CSVWriter writer = new CSVWriter(new FileWriter(metricOutput)); |
| 173 | + try { |
| 174 | + //Output headers |
| 175 | + final List<String> headers = Lists.newLinkedList(); |
| 176 | + headers.add("Identifier"); |
| 177 | + headers.addAll(Collections2.transform(results.columnKeySet(), new Function<Class, String>() { |
| 178 | + @Override |
| 179 | + public String apply(Class aClass) { |
| 180 | + return aClass.getSimpleName(); |
| 181 | + } |
| 182 | + })); |
| 183 | + writer.writeNext(headers.toArray(new String[headers.size()])); |
| 184 | + |
| 185 | + //Output data |
| 186 | + final ImmutableList<String[]> mappedDataSet = FluentIterable.from(results.rowMap().entrySet()) |
| 187 | + .transform(new Function<Map.Entry<String, Map<Class, Object>>, String[]>() { |
| 188 | + @Override |
| 189 | + public String[] apply(Map.Entry<String, Map<Class, Object>> entry) { |
| 190 | + final String[] ret = new String[1 + entry.getValue().size()]; |
| 191 | + ret[0] = entry.getKey(); |
| 192 | + |
| 193 | + final Iterator<Object> it = entry.getValue().values().iterator(); |
| 194 | + int count = 1; |
| 195 | + while (it.hasNext()) { |
| 196 | + ret[count++] = printObject(it.next(), ResultsExporter.this.nf); |
| 197 | + } |
| 198 | + |
| 199 | + return ret; |
| 200 | + } |
| 201 | + }) |
| 202 | + .toList(); |
| 203 | + writer.writeAll(mappedDataSet); |
| 204 | + } finally { |
| 205 | + try { |
| 206 | + writer.close(); |
| 207 | + } catch (IOException ignored) { |
| 208 | + //Prevent finally block from overriding exceptions |
| 209 | + } |
136 | 210 | } |
137 | 211 | } |
138 | 212 |
|
| 213 | + /** |
| 214 | + * @return Relative location of the 'mapping' file |
| 215 | + */ |
139 | 216 | public String getMappingFileName() { |
140 | 217 | return this.mappingFileName; |
141 | 218 | } |
|
0 commit comments