forked from StarOpenSource/Engine
Create new Math utility class
This commit is contained in:
parent
e5f7b0e580
commit
e787680e8c
10 changed files with 184 additions and 32 deletions
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class DateDay implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class DateMonth implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class DateYear implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,6 +43,6 @@ public final class TimeEpoch implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class TimeHour implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class TimeMinute implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.internal.placeholders;
|
package de.staropensource.sosengine.base.internal.placeholders;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.classes.Placeholder;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
@ -44,6 +44,6 @@ public final class TimeSecond implements Placeholder {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String replace(@NotNull String text) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,10 +27,7 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.annotations.Range;
|
import org.jetbrains.annotations.Range;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,19 +79,6 @@ public final class Miscellaneous {
|
||||||
return Tristate.toBoolean(getTristatedInteger(integer));
|
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}.
|
* Searches for a value in a {@link Map}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -25,6 +25,7 @@ import de.staropensource.sosengine.base.events.ThrowableCatchEvent;
|
||||||
import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException;
|
import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException;
|
||||||
import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException;
|
import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException;
|
||||||
import de.staropensource.sosengine.base.reflection.Reflect;
|
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.base.utility.Miscellaneous;
|
||||||
import de.staropensource.sosengine.unittests.UnitLogger;
|
import de.staropensource.sosengine.unittests.UnitLogger;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -83,7 +84,7 @@ public class MiscellaneousTest {
|
||||||
})
|
})
|
||||||
void testPadNumbers(int number, int length, String expected) {
|
void testPadNumbers(int number, int length, String expected) {
|
||||||
logger.testCall("testPadNumbers", number, length, 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 + "\"");
|
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue