From dd4ebcd88a163ffa4754512d73fe7542c26603de Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 28 Sep 2024 16:59:45 +0200 Subject: [PATCH] Mark all arrays as @NotNull --- .../reflection/IncompatibleTypeException.java | 2 +- .../implementation/EventListenerMethod.java | 2 +- .../base/reflection/ReflectionClass.java | 2 +- .../base/reflection/ReflectionField.java | 2 +- .../base/reflection/ReflectionMethod.java | 2 +- .../engine/base/type/InternalAccessArea.java | 7 +- .../engine/base/type/vector/Vec2d.java | 2 +- .../engine/base/type/vector/Vec2f.java | 2 +- .../engine/base/type/vector/Vec2i.java | 2 +- .../engine/base/type/vector/Vec3d.java | 4 +- .../engine/base/type/vector/Vec3f.java | 4 +- .../engine/base/type/vector/Vec3i.java | 4 +- .../base/utility/DependencyResolver.java | 2 +- .../engine/base/utility/ListFormatter.java | 2 +- .../engine/base/utility/Math.java | 69 +++++++++++++++++++ .../engine/base/utility/Miscellaneous.java | 4 +- .../staropensource/engine/testapp/Main.java | 2 +- 17 files changed, 92 insertions(+), 22 deletions(-) diff --git a/base/src/main/java/de/staropensource/engine/base/exception/reflection/IncompatibleTypeException.java b/base/src/main/java/de/staropensource/engine/base/exception/reflection/IncompatibleTypeException.java index f9e44bc49..926186105 100644 --- a/base/src/main/java/de/staropensource/engine/base/exception/reflection/IncompatibleTypeException.java +++ b/base/src/main/java/de/staropensource/engine/base/exception/reflection/IncompatibleTypeException.java @@ -36,7 +36,7 @@ public class IncompatibleTypeException extends RuntimeException { * @param requiredClassType class type received by the method * @param compatibleTypes class types the method is compatible with */ - public IncompatibleTypeException(@NotNull String methodName, @NotNull ClassType requiredClassType, @NotNull ClassType[] compatibleTypes) { + public IncompatibleTypeException(@NotNull String methodName, @NotNull ClassType requiredClassType, @NotNull ClassType @NotNull [] compatibleTypes) { super("The method ReflectionClass#" + methodName + " only applies to type(s) " + ListFormatter.formatArray(compatibleTypes) + ", not " + requiredClassType.name()); } diff --git a/base/src/main/java/de/staropensource/engine/base/internal/implementation/EventListenerMethod.java b/base/src/main/java/de/staropensource/engine/base/internal/implementation/EventListenerMethod.java index 3a3fe47eb..7682d196f 100644 --- a/base/src/main/java/de/staropensource/engine/base/internal/implementation/EventListenerMethod.java +++ b/base/src/main/java/de/staropensource/engine/base/internal/implementation/EventListenerMethod.java @@ -53,7 +53,7 @@ public final class EventListenerMethod extends EventListenerCode { /** {@inheritDoc} */ @Override - public void run(Object[] arguments) throws Exception { + public void run(@Nullable Object @NotNull [] arguments) throws Exception { method.invoke(arguments); } diff --git a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionClass.java b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionClass.java index edc930293..60a4c721c 100644 --- a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionClass.java +++ b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionClass.java @@ -152,7 +152,7 @@ public record ReflectionClass(Class clazz) { * @return array of all annotations * @since v1-alpha2 */ - public Annotation[] getAnnotations() { + public Annotation @NotNull [] getAnnotations() { return clazz.getAnnotations(); } diff --git a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionField.java b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionField.java index 13116f82c..dcbc1b9ae 100644 --- a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionField.java +++ b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionField.java @@ -299,7 +299,7 @@ public final class ReflectionField { * @return array of all annotations * @since v1-alpha2 */ - public Annotation[] getAnnotations() { + public Annotation @NotNull [] getAnnotations() { return field.getAnnotations(); } diff --git a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionMethod.java b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionMethod.java index d4ef25234..fd7510cdc 100644 --- a/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionMethod.java +++ b/base/src/main/java/de/staropensource/engine/base/reflection/ReflectionMethod.java @@ -303,7 +303,7 @@ public final class ReflectionMethod { * @return array of all annotations * @since v1-alpha2 */ - public Annotation[] getAnnotations() { + public Annotation @NotNull [] getAnnotations() { return method.getAnnotations(); } diff --git a/base/src/main/java/de/staropensource/engine/base/type/InternalAccessArea.java b/base/src/main/java/de/staropensource/engine/base/type/InternalAccessArea.java index 42f277602..2f9ae3ac9 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/InternalAccessArea.java +++ b/base/src/main/java/de/staropensource/engine/base/type/InternalAccessArea.java @@ -22,6 +22,7 @@ package de.staropensource.engine.base.type; import de.staropensource.engine.base.EngineInternals; import de.staropensource.engine.base.implementable.Event; import de.staropensource.engine.base.implementable.ShutdownHandler; +import org.jetbrains.annotations.NotNull; /** * Specifies multiple areas of internal engine access. @@ -110,7 +111,7 @@ public enum InternalAccessArea { * @return array containing all read-only areas * @since v1-alpha4 */ - public static InternalAccessArea[] valuesReadOnly() { + public static @NotNull InternalAccessArea @NotNull [] valuesReadOnly() { return new InternalAccessArea[]{ SHUTDOWN_HANDLER_GET, }; @@ -122,7 +123,7 @@ public enum InternalAccessArea { * @return array containing all essential read-only areas * @since v1-alpha5 */ - public static InternalAccessArea[] valuesEssentialReadOnly() { + public static @NotNull InternalAccessArea @NotNull [] valuesEssentialReadOnly() { return new InternalAccessArea[]{ REFLECTIVE_CLASSPATH_SCANNING_GET, }; @@ -134,7 +135,7 @@ public enum InternalAccessArea { * @return array containing all write-only areas * @since v1-alpha4 */ - public static InternalAccessArea[] valuesWriteOnly() { + public static @NotNull InternalAccessArea @NotNull [] valuesWriteOnly() { return new InternalAccessArea[]{ SAFETY_SHUTDOWN_HOOK_UPDATE, SHUTDOWN_HANDLER_UPDATE, diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2d.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2d.java index ac41bd29f..65500cba8 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2d.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2d.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec2d { +public final class Vec2d { /** * The X axis value. * diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2f.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2f.java index ab007ed38..d304fa694 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2f.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2f.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec2f { +public final class Vec2f { /** * The X axis value. * diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2i.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2i.java index c062dd651..5ae1217b7 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2i.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec2i.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec2i implements Cloneable { +public final class Vec2i implements Cloneable { /** * The X axis value. * diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3d.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3d.java index 11ed0e0aa..901d1887a 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3d.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3d.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec3d { +public final class Vec3d { /** * The X axis value. * @@ -121,6 +121,6 @@ public class Vec3d { return (EngineConfiguration.getInstance().isHideFullTypePath() ? getClass().getName().replace(getClass().getPackage() + ".", "") : getClass().getName()) - + "(x=" + x + " y=" + y + ")"; + + "(x=" + x + " y=" + y + " z=" + z + ")"; } } diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3f.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3f.java index 911d774a8..91846926c 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3f.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3f.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec3f { +public final class Vec3f { /** * The X axis value. * @@ -121,6 +121,6 @@ public class Vec3f { return (EngineConfiguration.getInstance().isHideFullTypePath() ? getClass().getName().replace(getClass().getPackage() + ".", "") : getClass().getName()) - + "(x=" + x + " y=" + y + ")"; + + "(x=" + x + " y=" + y + " z=" + z + ")"; } } diff --git a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3i.java b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3i.java index 61cd78822..bc0f39cef 100644 --- a/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3i.java +++ b/base/src/main/java/de/staropensource/engine/base/type/vector/Vec3i.java @@ -33,7 +33,7 @@ import org.jetbrains.annotations.NotNull; @Getter @Setter @SuppressWarnings({ "JavadocDeclaration" }) -public class Vec3i { +public final class Vec3i { /** * The X axis value. * @@ -121,6 +121,6 @@ public class Vec3i { return (EngineConfiguration.getInstance().isHideFullTypePath() ? getClass().getName().replace(getClass().getPackage() + ".", "") : getClass().getName()) - + "(x=" + x + " y=" + y + ")"; + + "(x=" + x + " y=" + y + " z=" + z + ")"; } } diff --git a/base/src/main/java/de/staropensource/engine/base/utility/DependencyResolver.java b/base/src/main/java/de/staropensource/engine/base/utility/DependencyResolver.java index 93d2b4871..82d367a69 100644 --- a/base/src/main/java/de/staropensource/engine/base/utility/DependencyResolver.java +++ b/base/src/main/java/de/staropensource/engine/base/utility/DependencyResolver.java @@ -100,7 +100,7 @@ public final class DependencyResolver { * @return itself * @since v1-alpha1 */ - public @NotNull DependencyResolver addVectors(@NotNull DependencyVector[] vectors) { + public @NotNull DependencyResolver addVectors(@NotNull DependencyVector @NotNull [] vectors) { return addVectors(Arrays.stream(vectors).toList()); } diff --git a/base/src/main/java/de/staropensource/engine/base/utility/ListFormatter.java b/base/src/main/java/de/staropensource/engine/base/utility/ListFormatter.java index 22ae4a46e..bf2a63310 100644 --- a/base/src/main/java/de/staropensource/engine/base/utility/ListFormatter.java +++ b/base/src/main/java/de/staropensource/engine/base/utility/ListFormatter.java @@ -45,7 +45,7 @@ public final class ListFormatter { * @return formatted string * @since v1-alpha4 */ - public static @NotNull String formatArray(@NotNull Object[] array) { + public static @NotNull String formatArray(@NotNull Object @NotNull [] array) { StringBuilder output = new StringBuilder(); for (int index = 0; index < array.length; index++) { diff --git a/base/src/main/java/de/staropensource/engine/base/utility/Math.java b/base/src/main/java/de/staropensource/engine/base/utility/Math.java index cba34819e..e28500188 100644 --- a/base/src/main/java/de/staropensource/engine/base/utility/Math.java +++ b/base/src/main/java/de/staropensource/engine/base/utility/Math.java @@ -52,6 +52,75 @@ public final class Math { return String.format("%0" + length + "d", number); } + /** + * Ensures the integer is inside the + * specified bounds. If not, the + * integer will be corrected. + * + * @param min minimum value + * @param max maximum value + * @param value value + * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value + * @since v1-alpha6 + */ + public static int boundNumber(int min, int max, int value) throws IndexOutOfBoundsException { + if (min > max) + throw new IndexOutOfBoundsException(); + + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; + } + + /** + * Ensures the float is inside the + * specified bounds. If not, the + * float will be corrected. + * + * @param min minimum value + * @param max maximum value + * @param value value + * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value + * @since v1-alpha6 + */ + public static float boundNumber(float min, float max, float value) throws IndexOutOfBoundsException { + if (min > max) + throw new IndexOutOfBoundsException(); + + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; + } + + /** + * Ensures the double is inside the + * specified bounds. If not, the + * double will be corrected. + * + * @param min minimum value + * @param max maximum value + * @param value value + * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value + * @since v1-alpha6 + */ + public static double boundNumber(double min, double max, double value) throws IndexOutOfBoundsException { + if (min > max) + throw new IndexOutOfBoundsException(); + + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; + } + /** * Returns the mean of a collection of numbers. * diff --git a/base/src/main/java/de/staropensource/engine/base/utility/Miscellaneous.java b/base/src/main/java/de/staropensource/engine/base/utility/Miscellaneous.java index b17404e51..e0995a7ee 100644 --- a/base/src/main/java/de/staropensource/engine/base/utility/Miscellaneous.java +++ b/base/src/main/java/de/staropensource/engine/base/utility/Miscellaneous.java @@ -119,7 +119,7 @@ public final class Miscellaneous { * @return separator to use or {@code null} * @since v1-alpha1 */ - public static @Nullable String getSeparator(@NotNull String string, String[] separators, int requiredOccurrences) { + public static @Nullable String getSeparator(@NotNull String string, @NotNull String @NotNull [] separators, int requiredOccurrences) { if (string.isBlank() || separators.length == 0 || requiredOccurrences == 0) return null; @@ -225,7 +225,7 @@ public final class Miscellaneous { * @return converted stacktrace string * @since v1-alpha4 */ - public static @NotNull String getStackTraceAsString(@NotNull StackTraceElement[] stacktrace, boolean indent) { + public static @NotNull String getStackTraceAsString(@NotNull StackTraceElement @NotNull [] stacktrace, boolean indent) { StringBuilder output = new StringBuilder(); for (StackTraceElement element : stacktrace) { diff --git a/testapp/src/main/java/de/staropensource/engine/testapp/Main.java b/testapp/src/main/java/de/staropensource/engine/testapp/Main.java index 1a5263027..455191ef8 100644 --- a/testapp/src/main/java/de/staropensource/engine/testapp/Main.java +++ b/testapp/src/main/java/de/staropensource/engine/testapp/Main.java @@ -101,7 +101,7 @@ public final class Main { * @param args program's command line arguments * @since v1-alpha0 */ - public static void main(String[] args) { + public static void main(String @NotNull [] args) { getInstance().run(); }