diff --git a/calculate_average_ivanklaric.sh b/calculate_average_ivanklaric.sh new file mode 100755 index 0000000..d80d86b --- /dev/null +++ b/calculate_average_ivanklaric.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# +# Copyright 2023 The original authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +JAVA_OPTS="" +java $JAVA_OPTS --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_ivanklaric diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java b/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java new file mode 100644 index 0000000..c5d893d --- /dev/null +++ b/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java @@ -0,0 +1,53 @@ +/* + * Copyright 2023 The original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dev.morling.onebrc; + +import java.io.IOException; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.ConcurrentMap; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +public class CalculateAverage_ivanklaric { + private static final String FILE = "measurements.txt"; + + record CityTemps (double min, double max, double sum, long count) { + public String toString() { + return round(min) + "/" + round(sum / count) + "/" + round(max); + } + + private double round(double num) { + return Math.round(num * 10.0) / 10.0; + } + } + + public static void main(String[] args) throws IOException { + Stream lines = Files.lines(Paths.get(FILE)); + ConcurrentMap cityStats = new ConcurrentSkipListMap<>(); + + lines.parallel().forEach(line -> { + int splitterLoc = line.indexOf(';'); + double temp = Double.parseDouble(line.substring(splitterLoc + 1, line.length())); + + cityStats.merge(line.substring(0, splitterLoc), new CityTemps(temp, temp, temp, 1), + (oldValue, defaultValue) -> { + return new CityTemps(Math.min(oldValue.min, temp), Math.max(oldValue.max, temp), oldValue.sum + temp, oldValue.count + 1); + }); + }); + System.out.println(cityStats); + } +}