Skip to content

Commit

Permalink
Usage of try-with-resources
Browse files Browse the repository at this point in the history
pom file cleanup
  • Loading branch information
khmarbaise authored and gunnarmorling committed Jan 3, 2024
1 parent 09d3530 commit 7d485d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
24 changes: 14 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
<version>1.0.0-SNAPSHOT</version>

<properties>
<java.version>21</java.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.release>${java.version}</maven.compiler.release>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down Expand Up @@ -100,11 +99,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.12.1</version>
<configuration>
<enablePreview>true</enablePreview>
<parameters>true</parameters>
<compilerArgs>
<compilerArg>--enable-preview</compilerArg>
<compilerArg>--add-modules</compilerArg>
<compilerArg>java.base,jdk.incubator.vector</compilerArg>
</compilerArgs>
Expand All @@ -118,17 +117,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -143,12 +142,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
<version>3.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-wrapper-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down Expand Up @@ -223,7 +227,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>[${java.version},)</version>
<version>${maven.compiler.release}</version>
</requireJavaVersion>
<requirePluginVersions>
<banLatest>true</banLatest>
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/dev/morling/onebrc/CreateMeasurements.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
package dev.morling.onebrc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class CreateMeasurements {

private static final String FILE = "./measurements.txt";
private static final Path MEASUREMENT_FILE = Path.of("./measurements.txt");

private record WeatherStation(String id, double meanTemperature) {
double measurement() {
Expand Down Expand Up @@ -76,7 +74,7 @@ public static void main(String[] args) throws Exception {
// )
// ) TO 'output.csv' (HEADER, DELIMITER ',');
// @formatter:on
List<WeatherStation> stations = Arrays.asList(
List<WeatherStation> stations = List.of(
new WeatherStation("Abha", 18.0),
new WeatherStation("Abidjan", 26.0),
new WeatherStation("Abéché", 29.4),
Expand Down Expand Up @@ -491,20 +489,17 @@ public static void main(String[] args) throws Exception {
new WeatherStation("Zanzibar City", 26.0),
new WeatherStation("Zürich", 9.3));

File measurements = new File(FILE);
try (FileOutputStream fos = new FileOutputStream(measurements); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) {
try (BufferedWriter bw = Files.newBufferedWriter(MEASUREMENT_FILE)) {
for (int i = 0; i < size; i++) {
if (i > 0 && i % 50_000_000 == 0) {
System.out.println("Wrote %,d measurements in %s ms".formatted(i, System.currentTimeMillis() - start));
System.out.printf("Wrote %,d measurements in %s ms%n", i, System.currentTimeMillis() - start);
}
WeatherStation station = stations.get(ThreadLocalRandom.current().nextInt(stations.size()));
bw.write(station.id());
bw.write(";" + station.measurement());
bw.newLine();
}
bw.flush();

System.out.println("Created file with %,d measurements in %s ms".formatted(size, System.currentTimeMillis() - start));
}
System.out.printf("Created file with %,d measurements in %s ms%n", size, System.currentTimeMillis() - start);
}
}

0 comments on commit 7d485d0

Please sign in to comment.