From 3f6ffea14e55b97242d95cb7f0ea3eeb88f60049 Mon Sep 17 00:00:00 2001 From: KitsuneAlex Date: Sat, 25 Sep 2021 20:23:01 +0200 Subject: [PATCH] Add JetBrains annotations to common base classes --- .../speiger/src/collections/utils/IArray.java | 2 +- .../src/collections/utils/ITrimmable.java | 8 ++--- .../src/collections/utils/SanityChecks.java | 33 ++++++++++--------- .../speiger/src/collections/utils/Stack.java | 16 +++++---- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/main/java/speiger/src/collections/utils/IArray.java b/src/main/java/speiger/src/collections/utils/IArray.java index 826e9baf..a931a5c7 100644 --- a/src/main/java/speiger/src/collections/utils/IArray.java +++ b/src/main/java/speiger/src/collections/utils/IArray.java @@ -16,5 +16,5 @@ public interface IArray extends RandomAccess, ITrimmable * * @param size the desired minimum capacity */ - public void ensureCapacity(int size); + void ensureCapacity(int size); } diff --git a/src/main/java/speiger/src/collections/utils/ITrimmable.java b/src/main/java/speiger/src/collections/utils/ITrimmable.java index 8026ee1b..f1f126f1 100644 --- a/src/main/java/speiger/src/collections/utils/ITrimmable.java +++ b/src/main/java/speiger/src/collections/utils/ITrimmable.java @@ -10,7 +10,7 @@ public interface ITrimmable * Trims the original collection down to the size of the current elements * @return if the internal array has been trimmed. */ - public default boolean trim() { + default boolean trim() { return trim(0); } @@ -19,12 +19,12 @@ public interface ITrimmable * @param size the requested trim size. * @return if the internal array has been trimmed. */ - public boolean trim(int size); + boolean trim(int size); /** * Trims the collection down to the original size and clears all present elements with it. */ - public default void clearAndTrim() { + default void clearAndTrim() { clearAndTrim(0); } @@ -33,5 +33,5 @@ public interface ITrimmable * @param size the amount of elements that should be allowed * @note this will enforce minimum size of the collection itself */ - public void clearAndTrim(int size); + void clearAndTrim(int size); } diff --git a/src/main/java/speiger/src/collections/utils/SanityChecks.java b/src/main/java/speiger/src/collections/utils/SanityChecks.java index 04285086..24488850 100644 --- a/src/main/java/speiger/src/collections/utils/SanityChecks.java +++ b/src/main/java/speiger/src/collections/utils/SanityChecks.java @@ -1,5 +1,8 @@ package speiger.src.collections.utils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.util.Random; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; @@ -8,15 +11,15 @@ import java.util.concurrent.ForkJoinTask; * Helper class that provides functions that are shared within the library. * On top of that it provides some helper functions that allow control of some internals of the Library */ -public class SanityChecks +public final class SanityChecks { /** Max Possible ArraySize without the JVM Crashing */ public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool(); private static boolean FORCE_IGNORE_PARALLELISM = false; - private static ThreadLocal RANDOM = ThreadLocal.withInitial(Random::new); - private static ThreadLocal FORCE_ASYNC = ThreadLocal.withInitial(() -> false); - private static ThreadLocal FORCE_TASK_POOL = ThreadLocal.withInitial(() -> false); + private static final ThreadLocal RANDOM = ThreadLocal.withInitial(Random::new); + private static final ThreadLocal FORCE_ASYNC = ThreadLocal.withInitial(() -> false); + private static final ThreadLocal FORCE_TASK_POOL = ThreadLocal.withInitial(() -> false); /** * Internal function to cast a Integer to a Byte @@ -90,8 +93,8 @@ public class SanityChecks * @param task the Task to invoke * @param the type of the task */ - public static void invokeTask(ForkJoinTask task) { - if(FORCE_ASYNC.get().booleanValue()) invokeAsyncTask(task); + public static void invokeTask(@NotNull ForkJoinTask task) { + if(FORCE_ASYNC.get()) invokeAsyncTask(task); else getPool().invoke(task); } @@ -100,7 +103,7 @@ public class SanityChecks * @param task the Task to invoke * @param the type of the task */ - public static void invokeAsyncTask(ForkJoinTask task) { + public static void invokeAsyncTask(@NotNull ForkJoinTask task) { getPool().execute(task); } @@ -109,12 +112,12 @@ public class SanityChecks * @note this method is not thread-save. It is only there to provide control over how Library specific Threaded tasks are handled. * @param pool The ForkJoinPool that should receive the tasks. If null {@link ForkJoinPool#commonPool()} is set instead */ - public static void setWorkPool(ForkJoinPool pool) { + public static void setWorkPool(@Nullable ForkJoinPool pool) { WORK_POOL = pool == null ? ForkJoinPool.commonPool() : pool; } - private static ForkJoinPool getPool() { - if(FORCE_TASK_POOL.get().booleanValue()) { + private static @NotNull ForkJoinPool getPool() { + if(FORCE_TASK_POOL.get()) { ForkJoinPool pool = ForkJoinTask.getPool(); if(pool != null) return pool; } @@ -126,7 +129,7 @@ public class SanityChecks * @return if the Current Thread has not to await task completion. */ public static boolean isForcedAsync() { - return FORCE_ASYNC.get().booleanValue(); + return FORCE_ASYNC.get(); } /** @@ -134,7 +137,7 @@ public class SanityChecks * @return if the task checks the current pool to be executed in if it is valid. */ public static boolean isForcedTaskPool() { - return FORCE_TASK_POOL.get().booleanValue(); + return FORCE_TASK_POOL.get(); } /** @@ -142,7 +145,7 @@ public class SanityChecks * @param value it tasks should not be awaited for */ public static void setForcedAsync(boolean value) { - FORCE_ASYNC.set(Boolean.valueOf(value)); + FORCE_ASYNC.set(value); } /** @@ -150,7 +153,7 @@ public class SanityChecks * @param value if the current thread should check if it is a ThreadPoolWorker. */ public static void setForcedTaskPool(boolean value) { - FORCE_TASK_POOL.set(Boolean.valueOf(value)); + FORCE_TASK_POOL.set(value); } /** @@ -164,7 +167,7 @@ public class SanityChecks /** * @return Thread Specific Random needed for internal functions in this library. */ - public static Random getRandom() { + public static @NotNull Random getRandom() { return RANDOM.get(); } } diff --git a/src/main/java/speiger/src/collections/utils/Stack.java b/src/main/java/speiger/src/collections/utils/Stack.java index fa878c9c..36b73c0e 100644 --- a/src/main/java/speiger/src/collections/utils/Stack.java +++ b/src/main/java/speiger/src/collections/utils/Stack.java @@ -1,6 +1,8 @@ package speiger.src.collections.utils; +import org.jetbrains.annotations.NotNull; + /** * The Stack Interface represents the Last-In-First-Out layout (LIFO). * It provides a simple {@link #push(Object)}, {@link #pop()} function, @@ -14,40 +16,40 @@ public interface Stack * Inserts a given Object on top of the stack * @param e the Object to insert */ - public void push(T e); + void push(@NotNull T e); /** * Removes the Object on top of the stack. * @return the element that is on top of the stack * @throws ArrayIndexOutOfBoundsException if the stack is empty */ - public T pop(); + @NotNull T pop(); /** * Provides the amount of elements currently in the stack * @return amount of elements in the list */ - public int size(); + int size(); /** * @return if the stack is empty */ - public default boolean isEmpty() { + default boolean isEmpty() { return size() == 0; } /** * Clears the stack */ - public void clear(); + void clear(); /** * Provides the Object on top of the stack * @return the element that is on top of the stack * @throws ArrayIndexOutOfBoundsException if the stack is empty */ - public default T top() { + default @NotNull T top() { return peek(0); } @@ -58,5 +60,5 @@ public interface Stack * @return the element that was requested * @throws ArrayIndexOutOfBoundsException if the index is out of bounds */ - public T peek(int index); + @NotNull T peek(int index); }