|
16 | 16 | */ |
17 | 17 | package org.apache.commons.text.jmh; |
18 | 18 |
|
19 | | -import java.io.BufferedReader; |
20 | 19 | import java.io.IOException; |
21 | | -import java.io.InputStream; |
22 | 20 | import java.io.InputStreamReader; |
23 | | -import java.io.UncheckedIOException; |
24 | 21 | import java.util.ArrayList; |
25 | 22 | import java.util.List; |
26 | 23 | import java.util.Objects; |
27 | 24 | import java.util.concurrent.TimeUnit; |
28 | 25 |
|
| 26 | +import org.apache.commons.csv.CSVFormat; |
29 | 27 | import org.apache.commons.lang3.tuple.ImmutablePair; |
30 | 28 | import org.apache.commons.lang3.tuple.Pair; |
31 | 29 | import org.apache.commons.text.similarity.LongestCommonSubsequence; |
@@ -114,22 +112,21 @@ public static class InputData { |
114 | 112 | final List<Pair<CharSequence, CharSequence>> inputs = new ArrayList<>(); |
115 | 113 |
|
116 | 114 | @Setup(Level.Trial) |
117 | | - public void setup() { |
| 115 | + public void setup() throws IOException { |
118 | 116 | final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
119 | | - try (InputStream is = classLoader.getResourceAsStream("org/apache/commons/text/lcs-perf-analysis-inputs.csv"); |
120 | | - InputStreamReader isr = new InputStreamReader(Objects.requireNonNull(is)); |
121 | | - BufferedReader br = new BufferedReader(isr)) { |
122 | | - String line; |
123 | | - while ((line = br.readLine()) != null && !line.trim().isEmpty()) { |
124 | | - line = line.trim(); |
125 | | - final int indexOfComma = line.indexOf(','); |
126 | | - final String inputA = line.substring(0, indexOfComma); |
127 | | - final String inputB = line.substring(1 + indexOfComma); |
128 | | - this.inputs.add(ImmutablePair.of(inputA, inputB)); |
129 | | - } |
130 | | - } catch (final IOException exception) { |
131 | | - throw new UncheckedIOException(exception.getMessage(), exception); |
132 | | - } |
| 117 | + CSVFormat.DEFAULT.builder().setCommentMarker('#').setTrim(true).get() |
| 118 | + .parse(new InputStreamReader( |
| 119 | + Objects.requireNonNull(classLoader.getResourceAsStream("org/apache/commons/text/lcs-perf-analysis-inputs.csv")))) |
| 120 | + .forEach(record -> { |
| 121 | + final String line = record.get(0); |
| 122 | + final int indexOfComma = line.indexOf(','); |
| 123 | + if (indexOfComma < 0) { |
| 124 | + throw new IllegalStateException("Invalid input line: " + line); |
| 125 | + } |
| 126 | + final String inputA = line.substring(0, indexOfComma); |
| 127 | + final String inputB = line.substring(1 + indexOfComma); |
| 128 | + this.inputs.add(ImmutablePair.of(inputA, inputB)); |
| 129 | + }); |
133 | 130 | } |
134 | 131 | } |
135 | 132 |
|
|
0 commit comments