diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateDay.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateDay.java index 27fd5d4..8d361bf 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateDay.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateDay.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class DateDay implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%date_day%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2)); + return text.replace("%date_day%", Math.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateMonth.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateMonth.java index c608e56..76ea9ba 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateMonth.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateMonth.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class DateMonth implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%date_month%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2)); + return text.replace("%date_month%", Math.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateYear.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateYear.java index 7f24b2a..a25705b 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateYear.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/DateYear.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class DateYear implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%date_year%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4)); + return text.replace("%date_year%", Math.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeEpoch.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeEpoch.java index 1302282..0c2a733 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeEpoch.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeEpoch.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; /** @@ -43,6 +43,6 @@ public final class TimeEpoch implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%time_epoch%", Miscellaneous.padNumbers(System.currentTimeMillis(), String.valueOf(Long.MAX_VALUE).length())); + return text.replace("%time_epoch%", Math.padNumbers(System.currentTimeMillis(), String.valueOf(Long.MAX_VALUE).length())); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeHour.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeHour.java index b988803..39e88e9 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeHour.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeHour.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class TimeHour implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%time_hour%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2)); + return text.replace("%time_hour%", Math.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeMinute.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeMinute.java index 3ba1f13..343150e 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeMinute.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeMinute.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class TimeMinute implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%time_minute%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2)); + return text.replace("%time_minute%", Math.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeSecond.java b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeSecond.java index 9fb5e37..40b6dec 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeSecond.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/placeholders/TimeSecond.java @@ -20,7 +20,7 @@ package de.staropensource.sosengine.base.internal.placeholders; import de.staropensource.sosengine.base.classes.Placeholder; -import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.base.utility.Math; import org.jetbrains.annotations.NotNull; import java.util.Calendar; @@ -44,6 +44,6 @@ public final class TimeSecond implements Placeholder { @NotNull @Override public String replace(@NotNull String text) { - return text.replace("%time_second%", Miscellaneous.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2)); + return text.replace("%time_second%", Math.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2)); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/Math.java b/base/src/main/java/de/staropensource/sosengine/base/utility/Math.java new file mode 100644 index 0000000..086f446 --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/Math.java @@ -0,0 +1,167 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Contributors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.sosengine.base.utility; + +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.LinkedList; + +// All mean and double methods have been sourced +// from https://stackoverflow.com/a/4191729, tysm +@SuppressWarnings({ "unused" }) +public class Math { + /** + * Adds padding zeros to a number. + * + * @param number number + * @param length length + * @return the padded number + * @since v1-alpha0 + */ + @NotNull + public static String padNumbers(long number, int length) { + return String.format("%0" + length + "d", number); + } + + /** + * Returns the mean of a collection of numbers. + * + * @param collection collection of {@code double}s + * @return mean + * @since v1-alpha2 + */ + public static double getMeanDouble(@NotNull Collection<@NotNull Double> collection) { + double sum = 0; + + for (double number : collection) + sum += number; + + return sum / collection.size(); + } + + /** + * Returns the mean of a collection of numbers. + * + * @param collection collection of {@code float}s + * @return mean + * @since v1-alpha2 + */ + public static double getMeanFloat(@NotNull Collection<@NotNull Float> collection) { + double sum = 0; + + for (float number : collection) + sum += number; + + return sum / collection.size(); + } + + /** + * Returns the mean of a collection of numbers. + * + * @param collection collection of {@code long}s + * @return mean + * @since v1-alpha2 + */ + public static double getMeanLong(@NotNull Collection<@NotNull Long> collection) { + double sum = 0; + + for (long number : collection) + sum += number; + + return sum / collection.size(); + } + + /** + * Returns the mean of a collection of numbers. + * + * @param collection collection of {@code int}s + * @return mean + * @since v1-alpha2 + */ + public static double getMeanInt(@NotNull Collection<@NotNull Integer> collection) { + double sum = 0; + + for (int number : collection) + sum += number; + + return sum / collection.size(); + } + + /** + * Returns the median of a linked list of numbers. + * + * @param list linked list of {@code double}s + * @return median + * @since v1-alpha2 + */ + public static double getMedianDouble(@NotNull LinkedList<@NotNull Double> list) { + int middle = list.size() / 2; + if (list.size() % 2 == 1) + return list.get(middle); + else + return list.get(middle - 1) / 2.0 + list.get(middle) / 2.0; + } + + /** + * Returns the median of a linked list of numbers. + * + * @param list linked list of {@code float}s + * @return median + * @since v1-alpha2 + */ + public static double getMedianFloat(@NotNull LinkedList<@NotNull Float> list) { + int middle = list.size() / 2; + if (list.size() % 2 == 1) + return list.get(middle); + else + return list.get(middle - 1) / 2.0 + list.get(middle) / 2.0; + } + + /** + * Returns the median of a linked list of numbers. + * + * @param list linked list of {@code long}s + * @return median + * @since v1-alpha2 + */ + public static double getMedianLong(@NotNull LinkedList<@NotNull Long> list) { + int middle = list.size() / 2; + if (list.size() % 2 == 1) + return list.get(middle); + else + return list.get(middle - 1) / 2.0 + list.get(middle) / 2.0; + } + + /** + * Returns the median of a linked list of numbers. + * + * @param list linked list of {@code int}s + * @return median + * @since v1-alpha2 + */ + public static double getMedianInt(@NotNull LinkedList<@NotNull Integer> list) { + int middle = list.size() / 2; + if (list.size() % 2 == 1) + return list.get(middle); + else + return list.get(middle - 1) / 2.0 + list.get(middle) / 2.0; + } +} diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java index bb4a5e4..cb3bdd6 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java @@ -27,10 +27,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Range; import java.lang.ref.WeakReference; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -82,19 +79,6 @@ public final class Miscellaneous { return Tristate.toBoolean(getTristatedInteger(integer)); } - /** - * Adds padding zeros to a number. - * - * @param number number - * @param length length - * @return the padded number - * @since v1-alpha0 - */ - @NotNull - public static String padNumbers(long number, int length) { - return String.format("%0" + length + "d", number); - } - /** * Searches for a value in a {@link Map}. * diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java index a340cc5..784a2bc 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java @@ -25,6 +25,7 @@ import de.staropensource.sosengine.base.events.ThrowableCatchEvent; import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException; import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException; import de.staropensource.sosengine.base.reflection.Reflect; +import de.staropensource.sosengine.base.utility.Math; import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.unittests.UnitLogger; import org.jetbrains.annotations.NotNull; @@ -83,7 +84,7 @@ public class MiscellaneousTest { }) void testPadNumbers(int number, int length, String expected) { logger.testCall("testPadNumbers", number, length, expected); - String result = Miscellaneous.padNumbers(number, length); + String result = Math.padNumbers(number, length); assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\""); }