1 | package edu.ucsb.cs156.happiercows.helpers; | |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
4 | import java.io.ByteArrayOutputStream; | |
5 | import java.io.IOException; | |
6 | import java.io.PrintWriter; | |
7 | import java.util.Arrays; | |
8 | import java.util.List; | |
9 | import org.apache.commons.csv.CSVFormat; | |
10 | import org.apache.commons.csv.CSVPrinter; | |
11 | import edu.ucsb.cs156.happiercows.entities.CommonStats; | |
12 | ||
13 | public class CommonStatsCSVHelper { | |
14 | ||
15 | private static final List<String> HEADERS = Arrays.asList( | |
16 | "id", "commonsId", "numCows", "avgHealth", "timestamp" | |
17 | ); | |
18 | ||
19 | // Private constructor to prevent instantiation of utility class. | |
20 | private CommonStatsCSVHelper() {} | |
21 | ||
22 | /** | |
23 | * Flush and close the given output stream and CSV printer. | |
24 | * | |
25 | * @param out stream to close | |
26 | * @param csvPrinter printer to close | |
27 | * @throws IOException if there's an error during flushing or closing | |
28 | */ | |
29 | private static void flushAndClose(ByteArrayOutputStream out, CSVPrinter csvPrinter) throws IOException { | |
30 |
1
1. flushAndClose : removed call to org/apache/commons/csv/CSVPrinter::flush → NO_COVERAGE |
csvPrinter.flush(); |
31 |
1
1. flushAndClose : removed call to org/apache/commons/csv/CSVPrinter::close → NO_COVERAGE |
csvPrinter.close(); |
32 |
1
1. flushAndClose : removed call to java/io/ByteArrayOutputStream::flush → NO_COVERAGE |
out.flush(); |
33 |
1
1. flushAndClose : removed call to java/io/ByteArrayOutputStream::close → NO_COVERAGE |
out.close(); |
34 | } | |
35 | ||
36 | /** | |
37 | * Convert the given Iterable of CommonStats into a ByteArrayInputStream suitable for CSV export. | |
38 | * | |
39 | * @param lines The Iterable collection of CommonStats to be exported. | |
40 | * @return A ByteArrayInputStream representation of the CSV data. | |
41 | * @throws IOException if there's an error during CSV generation. | |
42 | */ | |
43 | public static ByteArrayInputStream toCSV(Iterable<CommonStats> lines) throws IOException { | |
44 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
45 | CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), CSVFormat.DEFAULT); | |
46 | ||
47 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → NO_COVERAGE |
csvPrinter.printRecord(HEADERS); |
48 | ||
49 | for (CommonStats stats : lines) { | |
50 | List<String> data = Arrays.asList( | |
51 | String.valueOf(stats.getId()), | |
52 | String.valueOf(stats.getCommonsId()), | |
53 | String.valueOf(stats.getNumCows()), | |
54 | String.valueOf(stats.getAvgHealth()), | |
55 | String.valueOf(stats.getTimestamp()) | |
56 | ); | |
57 |
1
1. toCSV : removed call to org/apache/commons/csv/CSVPrinter::printRecord → NO_COVERAGE |
csvPrinter.printRecord(data); |
58 | } | |
59 | ||
60 |
1
1. toCSV : removed call to edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::flushAndClose → NO_COVERAGE |
flushAndClose(out, csvPrinter); |
61 | ||
62 |
1
1. toCSV : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/CommonStatsCSVHelper::toCSV → NO_COVERAGE |
return new ByteArrayInputStream(out.toByteArray()); |
63 | } | |
64 | } | |
Mutations | ||
30 |
1.1 |
|
31 |
1.1 |
|
32 |
1.1 |
|
33 |
1.1 |
|
47 |
1.1 |
|
57 |
1.1 |
|
60 |
1.1 |
|
62 |
1.1 |