Add JetBrains annotations to common base classes

This commit is contained in:
Alexander Hinze 2021-09-25 20:23:01 +02:00
parent 9c09f2ee70
commit 3f6ffea14e
4 changed files with 32 additions and 27 deletions

View File

@ -16,5 +16,5 @@ public interface IArray extends RandomAccess, ITrimmable
* *
* @param size the desired minimum capacity * @param size the desired minimum capacity
*/ */
public void ensureCapacity(int size); void ensureCapacity(int size);
} }

View File

@ -10,7 +10,7 @@ public interface ITrimmable
* Trims the original collection down to the size of the current elements * Trims the original collection down to the size of the current elements
* @return if the internal array has been trimmed. * @return if the internal array has been trimmed.
*/ */
public default boolean trim() { default boolean trim() {
return trim(0); return trim(0);
} }
@ -19,12 +19,12 @@ public interface ITrimmable
* @param size the requested trim size. * @param size the requested trim size.
* @return if the internal array has been trimmed. * @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. * Trims the collection down to the original size and clears all present elements with it.
*/ */
public default void clearAndTrim() { default void clearAndTrim() {
clearAndTrim(0); clearAndTrim(0);
} }
@ -33,5 +33,5 @@ public interface ITrimmable
* @param size the amount of elements that should be allowed * @param size the amount of elements that should be allowed
* @note this will enforce minimum size of the collection itself * @note this will enforce minimum size of the collection itself
*/ */
public void clearAndTrim(int size); void clearAndTrim(int size);
} }

View File

@ -1,5 +1,8 @@
package speiger.src.collections.utils; package speiger.src.collections.utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask; 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. * 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 * 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 */ /** Max Possible ArraySize without the JVM Crashing */
public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool(); private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool();
private static boolean FORCE_IGNORE_PARALLELISM = false; private static boolean FORCE_IGNORE_PARALLELISM = false;
private static ThreadLocal<Random> RANDOM = ThreadLocal.withInitial(Random::new); private static final ThreadLocal<Random> RANDOM = ThreadLocal.withInitial(Random::new);
private static ThreadLocal<Boolean> FORCE_ASYNC = ThreadLocal.withInitial(() -> false); private static final ThreadLocal<Boolean> FORCE_ASYNC = ThreadLocal.withInitial(() -> false);
private static ThreadLocal<Boolean> FORCE_TASK_POOL = ThreadLocal.withInitial(() -> false); private static final ThreadLocal<Boolean> FORCE_TASK_POOL = ThreadLocal.withInitial(() -> false);
/** /**
* Internal function to cast a Integer to a Byte * Internal function to cast a Integer to a Byte
@ -90,8 +93,8 @@ public class SanityChecks
* @param task the Task to invoke * @param task the Task to invoke
* @param <T> the type of the task * @param <T> the type of the task
*/ */
public static <T> void invokeTask(ForkJoinTask<T> task) { public static <T> void invokeTask(@NotNull ForkJoinTask<T> task) {
if(FORCE_ASYNC.get().booleanValue()) invokeAsyncTask(task); if(FORCE_ASYNC.get()) invokeAsyncTask(task);
else getPool().invoke(task); else getPool().invoke(task);
} }
@ -100,7 +103,7 @@ public class SanityChecks
* @param task the Task to invoke * @param task the Task to invoke
* @param <T> the type of the task * @param <T> the type of the task
*/ */
public static <T> void invokeAsyncTask(ForkJoinTask<T> task) { public static <T> void invokeAsyncTask(@NotNull ForkJoinTask<T> task) {
getPool().execute(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. * @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 * @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; WORK_POOL = pool == null ? ForkJoinPool.commonPool() : pool;
} }
private static ForkJoinPool getPool() { private static @NotNull ForkJoinPool getPool() {
if(FORCE_TASK_POOL.get().booleanValue()) { if(FORCE_TASK_POOL.get()) {
ForkJoinPool pool = ForkJoinTask.getPool(); ForkJoinPool pool = ForkJoinTask.getPool();
if(pool != null) return pool; if(pool != null) return pool;
} }
@ -126,7 +129,7 @@ public class SanityChecks
* @return if the Current Thread has not to await task completion. * @return if the Current Thread has not to await task completion.
*/ */
public static boolean isForcedAsync() { 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. * @return if the task checks the current pool to be executed in if it is valid.
*/ */
public static boolean isForcedTaskPool() { 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 * @param value it tasks should not be awaited for
*/ */
public static void setForcedAsync(boolean value) { 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. * @param value if the current thread should check if it is a ThreadPoolWorker.
*/ */
public static void setForcedTaskPool(boolean value) { 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. * @return Thread Specific Random needed for internal functions in this library.
*/ */
public static Random getRandom() { public static @NotNull Random getRandom() {
return RANDOM.get(); return RANDOM.get();
} }
} }

View File

@ -1,6 +1,8 @@
package speiger.src.collections.utils; package speiger.src.collections.utils;
import org.jetbrains.annotations.NotNull;
/** /**
* The Stack Interface represents the Last-In-First-Out layout (LIFO). * The Stack Interface represents the Last-In-First-Out layout (LIFO).
* It provides a simple {@link #push(Object)}, {@link #pop()} function, * It provides a simple {@link #push(Object)}, {@link #pop()} function,
@ -14,40 +16,40 @@ public interface Stack<T>
* Inserts a given Object on top of the stack * Inserts a given Object on top of the stack
* @param e the Object to insert * @param e the Object to insert
*/ */
public void push(T e); void push(@NotNull T e);
/** /**
* Removes the Object on top of the stack. * Removes the Object on top of the stack.
* @return the element that is on top of the stack * @return the element that is on top of the stack
* @throws ArrayIndexOutOfBoundsException if the stack is empty * @throws ArrayIndexOutOfBoundsException if the stack is empty
*/ */
public T pop(); @NotNull T pop();
/** /**
* Provides the amount of elements currently in the stack * Provides the amount of elements currently in the stack
* @return amount of elements in the list * @return amount of elements in the list
*/ */
public int size(); int size();
/** /**
* @return if the stack is empty * @return if the stack is empty
*/ */
public default boolean isEmpty() { default boolean isEmpty() {
return size() == 0; return size() == 0;
} }
/** /**
* Clears the stack * Clears the stack
*/ */
public void clear(); void clear();
/** /**
* Provides the Object on top of the stack * Provides the Object on top of the stack
* @return the element that is on top of the stack * @return the element that is on top of the stack
* @throws ArrayIndexOutOfBoundsException if the stack is empty * @throws ArrayIndexOutOfBoundsException if the stack is empty
*/ */
public default T top() { default @NotNull T top() {
return peek(0); return peek(0);
} }
@ -58,5 +60,5 @@ public interface Stack<T>
* @return the element that was requested * @return the element that was requested
* @throws ArrayIndexOutOfBoundsException if the index is out of bounds * @throws ArrayIndexOutOfBoundsException if the index is out of bounds
*/ */
public T peek(int index); @NotNull T peek(int index);
} }