From a6c99ea1bc1bc0f4d21da7f2a52e1f3739a2f04f Mon Sep 17 00:00:00 2001 From: javasabr Date: Fri, 6 Feb 2026 20:25:33 +0100 Subject: [PATCH 1/4] update javadoc for collection module --- .../rlib/collections/array/Array.java | 285 ++++++++++++++---- .../rlib/collections/array/ArrayBuilder.java | 46 +++ .../collections/array/ArrayCollectors.java | 21 ++ .../rlib/collections/array/ArrayFactory.java | 50 +++ .../array/ArrayIterationFunctions.java | 68 +++++ .../rlib/collections/array/IntArray.java | 148 ++++++++- .../rlib/collections/array/LockableArray.java | 12 + .../rlib/collections/array/LongArray.java | 149 ++++++++- .../rlib/collections/array/MutableArray.java | 67 +++- .../collections/array/MutableIntArray.java | 69 +++++ .../collections/array/MutableLongArray.java | 69 +++++ .../ReversedArgsArrayIterationFunctions.java | 44 +++ .../rlib/collections/array/UnsafeArray.java | 21 ++ .../collections/array/UnsafeIntArray.java | 21 ++ .../collections/array/UnsafeLongArray.java | 21 ++ .../collections/array/UnsafeMutableArray.java | 42 +++ .../array/UnsafeMutableIntArray.java | 41 +++ .../array/UnsafeMutableLongArray.java | 41 +++ .../rlib/collections/deque/DequeFactory.java | 27 ++ .../collections/dictionary/Dictionary.java | 97 ++++++ .../dictionary/DictionaryCollectors.java | 26 ++ .../dictionary/DictionaryFactory.java | 63 ++++ .../collections/dictionary/HashEntry.java | 12 + .../dictionary/IntToRefDictionary.java | 98 ++++++ .../collections/dictionary/IntToRefEntry.java | 19 ++ .../collections/dictionary/LinkedEntry.java | 20 ++ .../dictionary/LinkedHashEntry.java | 8 + .../dictionary/LinkedHashIntToRefEntry.java | 7 + .../dictionary/LinkedHashLongToRefEntry.java | 7 + .../LockableRefToRefDictionary.java | 13 + .../dictionary/LongToRefDictionary.java | 98 ++++++ .../dictionary/LongToRefEntry.java | 20 ++ .../dictionary/MutableIntToRefDictionary.java | 108 ++++++- .../MutableLongToRefDictionary.java | 108 ++++++- .../dictionary/MutableRefToRefDictionary.java | 111 ++++++- .../rlib/collections/dictionary/RefEntry.java | 20 ++ .../dictionary/RefToRefDictionary.java | 122 +++++++- .../dictionary/RefToRefDictionaryBuilder.java | 44 ++- .../collections/dictionary/RefToRefEntry.java | 21 ++ .../dictionary/UnsafeIntToRefDictionary.java | 13 + .../dictionary/UnsafeLongToRefDictionary.java | 13 + .../UnsafeMutableIntToRefDictionary.java | 7 + .../UnsafeMutableLongToRefDictionary.java | 7 + .../UnsafeMutableRefToRefDictionary.java | 8 + .../dictionary/UnsafeRefToRefDictionary.java | 14 + .../operation/LockableOperations.java | 108 +++++++ .../collections/operation/LockableSource.java | 45 +++ 47 files changed, 2393 insertions(+), 86 deletions(-) diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/Array.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/Array.java index a40377d4..a1a0d465 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/Array.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/Array.java @@ -15,49 +15,140 @@ import org.jspecify.annotations.Nullable; /** + * An immutable array interface that provides type-safe, indexed access to elements. + * + * @param the type of elements in this array * @author JavaSaBr + * @since 10.0.0 */ public interface Array extends Iterable, Serializable, Cloneable { + /** + * Creates an empty immutable array of the specified type. + * + * @param the type of elements + * @param type the component type of the array + * @return an empty immutable array + * @since 10.0.0 + */ static Array empty(Class type) { return new ImmutableArray<>(ClassUtils.unsafeCast(type)); } + /** + * Creates a new builder for constructing an immutable array. + * + * @param the type of elements + * @param type the component type of the array + * @return a new array builder + * @since 10.0.0 + */ static ArrayBuilder builder(Class type) { return new ArrayBuilder<>(type); } - + + /** + * Creates an immutable array containing a single element. + * + * @param the type of elements + * @param single the single element + * @return an immutable array containing the element + * @since 10.0.0 + */ static Array of(E single) { @SuppressWarnings("unchecked") Class type = (Class) single.getClass(); return new ImmutableArray<>(type, single); } + /** + * Creates an immutable array containing two elements. + * + * @param the type of elements + * @param e1 the first element + * @param e2 the second element + * @return an immutable array containing the elements + * @since 10.0.0 + */ static Array of(E e1, E e2) { Class commonType = ClassUtils.commonType(e1, e2); return new ImmutableArray<>(commonType, e1, e2); } + /** + * Creates an immutable array containing three elements. + * + * @param the type of elements + * @param e1 the first element + * @param e2 the second element + * @param e3 the third element + * @return an immutable array containing the elements + * @since 10.0.0 + */ static Array of(E e1, E e2, E e3) { Class commonType = ClassUtils.commonType(e1, e2, e3); return new ImmutableArray<>(commonType, e1, e2, e3); } + /** + * Creates an immutable array containing four elements. + * + * @param the type of elements + * @param e1 the first element + * @param e2 the second element + * @param e3 the third element + * @param e4 the fourth element + * @return an immutable array containing the elements + * @since 10.0.0 + */ static Array of(E e1, E e2, E e3, E e4) { Class commonType = ClassUtils.commonType(e1, e2, e3, e4); return new ImmutableArray<>(commonType, e1, e2, e3, e4); } + /** + * Creates an immutable array containing five elements. + * + * @param the type of elements + * @param e1 the first element + * @param e2 the second element + * @param e3 the third element + * @param e4 the fourth element + * @param e5 the fifth element + * @return an immutable array containing the elements + * @since 10.0.0 + */ static Array of(E e1, E e2, E e3, E e4, E e5) { Class commonType = ClassUtils.commonType(e1, e2, e3, e4, e5); return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5); } + /** + * Creates an immutable array containing six elements. + * + * @param the type of elements + * @param e1 the first element + * @param e2 the second element + * @param e3 the third element + * @param e4 the fourth element + * @param e5 the fifth element + * @param e6 the sixth element + * @return an immutable array containing the elements + * @since 10.0.0 + */ static Array of(E e1, E e2, E e3, E e4, E e5, E e6) { Class commonType = ClassUtils.commonType(e1, e2, e3, e4, e5, e6); return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5, e6); } + /** + * Creates an immutable array containing the specified elements. + * + * @param the type of elements + * @param elements the elements to include + * @return an immutable array containing the elements + * @since 10.0.0 + */ @SafeVarargs static Array of(E... elements) { //noinspection unchecked @@ -67,11 +158,29 @@ static Array of(E... elements) { return new ImmutableArray<>(type, elements); } + /** + * Creates an immutable array with explicit type containing the specified elements. + * + * @param the type of elements + * @param type the component type of the array + * @param elements the elements to include + * @return an immutable array containing the elements + * @since 10.0.0 + */ @SafeVarargs static Array typed(Class type, E... elements) { return new ImmutableArray<>(type, elements); } + /** + * Creates an immutable array from present optional values. + * + * @param the type of elements + * @param type the component type of the array + * @param elements the optional elements + * @return an immutable array containing present values + * @since 10.0.0 + */ @SafeVarargs static Array optionals(Class type, Optional... elements) { return Stream @@ -82,7 +191,13 @@ static Array optionals(Class type, Optional... elements) { } /** - * Creates array with the same element repeated 'count' times + * Creates an immutable array with the same element repeated specified times. + * + * @param the type of elements + * @param element the element to repeat + * @param count the number of times to repeat + * @return an immutable array with repeated elements + * @since 10.0.0 */ static Array repeated(E element, int count) { @SuppressWarnings("unchecked") @@ -92,6 +207,14 @@ static Array repeated(E element, int count) { return ImmutableArray.trustWrap(array); } + /** + * Creates an immutable copy of the specified array. + * + * @param the type of elements + * @param array the array to copy + * @return an immutable copy of the array + * @since 10.0.0 + */ static Array copyOf(Array array) { if (array instanceof ImmutableArray) { return array; @@ -99,6 +222,15 @@ static Array copyOf(Array array) { return new ImmutableArray<>(array.type(), array.toArray()); } + /** + * Creates an immutable array from the specified collection. + * + * @param the type of elements + * @param type the component type of the array + * @param collection the collection to copy + * @return an immutable array containing the collection elements + * @since 10.0.0 + */ static Array copyOf(Class type, Collection collection) { if (collection instanceof MutableArray mutableArray) { return copyOf(mutableArray); @@ -107,14 +239,21 @@ static Array copyOf(Class type, Collection collection) { return ImmutableArray.trustWrap(array); } + /** + * Returns the component type of this array. + * + * @return the component type + * @since 10.0.0 + */ Class type(); /** - * Returns the number of elements in this array. If this array + * Returns the number of elements in this array. If this array * contains more than {@code Integer.MAX_VALUE} elements, returns * {@code Integer.MAX_VALUE}. * * @return the number of elements in this array + * @since 10.0.0 */ int size(); @@ -125,14 +264,8 @@ static Array copyOf(Class type, Collection collection) { * {@code Objects.equals(object, e)}. * * @param object element whose presence in this array is to be tested - * @return {@code true} if this array contains the specified - * element - * @throws ClassCastException if the type of the specified element - * is incompatible with this array - * ({@linkplain Collection##optional-restrictions optional}) - * @throws NullPointerException if the specified element is null and this - * array does not permit null elements - * ({@linkplain Collection##optional-restrictions optional}) + * @return {@code true} if this array contains the specified element + * @since 10.0.0 */ boolean contains(@Nullable Object object); @@ -140,19 +273,11 @@ static Array copyOf(Class type, Collection collection) { * Returns {@code true} if this array contains all of the elements * in the specified array. * - * @param array array to be checked for containment in this collection + * @param array array to be checked for containment in this array * @return {@code true} if this array contains all of the elements * in the specified array - * @throws ClassCastException if the types of one or more elements - * in the specified array are incompatible with this - * array - * ({@linkplain Collection##optional-restrictions optional}) - * @throws NullPointerException if the specified array contains one - * or more null elements and this array does not permit null - * elements - * ({@linkplain Collection##optional-restrictions optional}) - * or if the specified collection is null. * @see #contains(Object) + * @since 10.0.0 */ boolean containsAll(Array array); @@ -163,16 +288,8 @@ static Array copyOf(Class type, Collection collection) { * @param collection collection to be checked for containment in this array * @return {@code true} if this array contains all of the elements * in the specified collection - * @throws ClassCastException if the types of one or more elements - * in the specified collection are incompatible with this - * collection - * ({@linkplain Collection##optional-restrictions optional}) - * @throws NullPointerException if the specified collection contains one - * or more null elements and this array does not permit null - * elements - * ({@linkplain Collection##optional-restrictions optional}) - * or if the specified collection is null. * @see #contains(Object) + * @since 10.0.0 */ boolean containsAll(Collection collection); @@ -180,36 +297,38 @@ static Array copyOf(Class type, Collection collection) { * Returns {@code true} if this array contains all of the elements * in the specified array. * - * @param array array to be checked for containment in this collection + * @param array array to be checked for containment in this array * @return {@code true} if this array contains all of the elements * in the specified array - * @throws ClassCastException if the types of one or more elements - * in the specified array are incompatible with this - * array - * ({@linkplain Collection##optional-restrictions optional}) - * @throws NullPointerException if the specified array contains one - * or more null elements and this array does not permit null - * elements - * ({@linkplain Collection##optional-restrictions optional}) - * or if the specified collection is null. * @see #contains(Object) + * @since 10.0.0 */ boolean containsAll(Object[] array); /** - * Gets the first element of this array. - - * @return the retrieved element or null + * Returns the first element of this array, or null if empty. + * + * @return the first element or null + * @since 10.0.0 */ @Nullable E first(); + /** + * Returns the element at the specified index. + * + * @param index the index of the element to return + * @return the element at the specified index + * @throws IndexOutOfBoundsException if the index is out of range + * @since 10.0.0 + */ E get(int index); /** - * Gets the last element of this array. - - * @return the retrieved element or null + * Returns the last element of this array, or null if empty. + * + * @return the last element or null + * @since 10.0.0 */ @Nullable E last(); @@ -220,44 +339,108 @@ default Iterator iterator() { } /** - * @return the index of the object or -1. + * Returns the index of the first occurrence of the specified object. + * + * @param object the object to search for + * @return the index of the object or -1 if not found + * @since 10.0.0 */ int indexOf(@Nullable Object object); /** - * @return the index of the object or -1. + * Returns the index of the first element whose property matches the specified object. + * + * @param the type of the property + * @param getter the function to extract the property + * @param object the object to match + * @return the index of the object or -1 if not found + * @since 10.0.0 */ int indexOf(Function getter, @Nullable Object object); + /** + * Returns the index of the last occurrence of the specified object. + * + * @param object the object to search for + * @return the index of the object or -1 if not found + * @since 10.0.0 + */ int lastIndexOf(@Nullable Object object); + /** + * Copies elements into the provided array. + * + * @param the type of the array elements + * @param newArray the array to copy into + * @return the array with copied elements + * @since 10.0.0 + */ T[] toArray(T[] newArray); /** - * Copy this array to the new array. + * Copies this array to a new array of the specified component type. * * @param the type parameter - * @param componentType the type of the new array. - * @return the copied array. + * @param componentType the type of the new array + * @return the copied array + * @since 10.0.0 */ T[] toArray(Class componentType); + /** + * Returns {@code true} if this array contains no elements. + * + * @return {@code true} if this array is empty + * @since 10.0.0 + */ boolean isEmpty(); + /** + * Returns a new array containing all elements. + * + * @return an array containing all elements + * @since 10.0.0 + */ E[] toArray(); + /** + * Returns a string representation using the specified formatter. + * + * @param toString the function to convert elements to strings + * @return a string representation of this array + * @since 10.0.0 + */ String toString(Function toString); /** * Returns a sequential {@code Stream} with this array as its source. * * @return a sequential {@code Stream} over the elements in this array + * @since 10.0.0 */ Stream stream(); + /** + * Returns iteration functions for this array. + * + * @return the iteration functions + * @since 10.0.0 + */ ArrayIterationFunctions iterations(); + /** + * Returns an unsafe view of this array providing direct access to internals. + * + * @return an unsafe view of this array + * @since 10.0.0 + */ UnsafeArray asUnsafe(); + /** + * Returns an unmodifiable list view of this array. + * + * @return an unmodifiable list view + * @since 10.0.0 + */ List toList(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayBuilder.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayBuilder.java index 06be898d..aa527f88 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayBuilder.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayBuilder.java @@ -4,36 +4,82 @@ import lombok.AccessLevel; import lombok.experimental.FieldDefaults; +/** + * A builder for constructing immutable {@link Array} instances. + * + * @param the type of elements in the array being built + * @since 10.0.0 + */ @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public final class ArrayBuilder { MutableArray elements; + /** + * Creates a new array builder with the specified component type. + * + * @param type the component type of the array + * @since 10.0.0 + */ public ArrayBuilder(Class type) { this.elements = ArrayFactory.mutableArray(type); } + /** + * Adds an element to the array being built. + * + * @param element the element to add + * @return this builder for method chaining + * @since 10.0.0 + */ public ArrayBuilder add(E element) { elements.add(element); return this; } + /** + * Adds multiple elements to the array being built. + * + * @param other the elements to add + * @return this builder for method chaining + * @since 10.0.0 + */ @SafeVarargs public final ArrayBuilder add(E... other) { elements.addAll(other); return this; } + /** + * Adds all elements from a collection to the array being built. + * + * @param other the collection of elements to add + * @return this builder for method chaining + * @since 10.0.0 + */ public ArrayBuilder add(Collection other) { elements.addAll(other); return this; } + /** + * Adds all elements from an array to the array being built. + * + * @param other the array of elements to add + * @return this builder for method chaining + * @since 10.0.0 + */ public ArrayBuilder add(Array other) { elements.addAll(other); return this; } + /** + * Builds and returns an immutable array containing all added elements. + * + * @return an immutable array containing all added elements + * @since 10.0.0 + */ public Array build() { return Array.copyOf(elements); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayCollectors.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayCollectors.java index ce87c751..b4db250b 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayCollectors.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayCollectors.java @@ -12,6 +12,11 @@ import java.util.stream.Collector.Characteristics; import lombok.experimental.UtilityClass; +/** + * Provides {@link Collector} implementations for collecting stream elements into {@link Array} instances. + * + * @since 10.0.0 + */ @UtilityClass public class ArrayCollectors { @@ -96,10 +101,26 @@ public Set characteristics() { }; } + /** + * Returns a collector that accumulates elements into a mutable array. + * + * @param the type of elements + * @param type the component type of the array + * @return a collector that collects elements into a mutable array + * @since 10.0.0 + */ public static Collector, MutableArray> toMutableArray(Class type) { return mutableCollector(type, ArrayFactory::mutableArray); } + /** + * Returns a collector that accumulates elements into an immutable array. + * + * @param the type of elements + * @param type the component type of the array + * @return a collector that collects elements into an immutable array + * @since 10.0.0 + */ public static Collector, Array> toArray(Class type) { return collector(type, ArrayFactory::mutableArray); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayFactory.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayFactory.java index 1960983f..e7cd1af5 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayFactory.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayFactory.java @@ -8,30 +8,80 @@ import javasabr.rlib.common.util.ClassUtils; import lombok.experimental.UtilityClass; +/** + * Factory for creating various array implementations. + * + * @since 10.0.0 + */ @UtilityClass public class ArrayFactory { + /** + * Creates a new mutable array of the specified type. + * + * @param the type of elements + * @param type the component type of the array + * @return a new mutable array + * @since 10.0.0 + */ public static MutableArray mutableArray(Class type) { return new DefaultMutableArray<>(ClassUtils.unsafeCast(type)); } + /** + * Creates a new mutable int array. + * + * @return a new mutable int array + * @since 10.0.0 + */ public static MutableIntArray mutableIntArray() { return new DefaultMutableIntArray(); } + /** + * Creates a new mutable long array. + * + * @return a new mutable long array + * @since 10.0.0 + */ public static MutableLongArray mutableLongArray() { return new DefaultMutableLongArray(); } + /** + * Creates a new mutable array of the specified type with initial capacity. + * + * @param the type of elements + * @param type the component type of the array + * @param capacity the initial capacity + * @return a new mutable array + * @since 10.0.0 + */ public static MutableArray mutableArray(Class type, int capacity) { return new DefaultMutableArray<>(ClassUtils.unsafeCast(type), capacity); } + /** + * Creates a new copy-on-modify array of the specified type. + * + * @param the type of elements + * @param type the component type of the array + * @return a new copy-on-modify array + * @since 10.0.0 + */ public static MutableArray copyOnModifyArray(Class type) { return new CopyOnWriteMutableArray<>(type); } + /** + * Creates a new thread-safe array backed by a stamped lock. + * + * @param the type of elements + * @param type the component type of the array + * @return a new lockable array + * @since 10.0.0 + */ public static LockableArray stampedLockBasedArray(Class type) { return new StampedLockBasedArray<>(type); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java index 985d3af8..fdd7943a 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java @@ -7,21 +7,89 @@ import javasabr.rlib.functions.TriConsumer; import org.jspecify.annotations.Nullable; +/** + * Provides iteration functions for arrays with support for additional arguments. + * + * @param the type of elements in the array + * @since 10.0.0 + */ public interface ArrayIterationFunctions { + /** + * Returns iteration functions with reversed argument order. + * + * @return reversed argument iteration functions + * @since 10.0.0 + */ ReversedArgsArrayIterationFunctions reversedArgs(); + /** + * Finds any element matching the filter predicate. + * + * @param the type of the argument + * @param arg1 the argument to pass to the filter + * @param filter the predicate to match elements + * @return the matching element or null + * @since 10.0.0 + */ @Nullable E findAny(A arg1, BiPredicate filter); + /** + * Finds any element matching the filter predicate with an int argument. + * + * @param arg1 the int argument to pass to the filter + * @param filter the predicate to match elements + * @return the matching element or null + * @since 10.0.0 + */ @Nullable E findAny(int arg1, ObjIntPredicate filter); + /** + * Performs an action for each element with an additional argument. + * + * @param the type of the argument + * @param arg1 the argument to pass to the consumer + * @param consumer the action to perform + * @return this for method chaining + * @since 10.0.0 + */ ArrayIterationFunctions forEach(A arg1, BiConsumer consumer); + /** + * Performs an action for each element with two additional arguments. + * + * @param the type of the first argument + * @param the type of the second argument + * @param arg1 the first argument to pass to the consumer + * @param arg2 the second argument to pass to the consumer + * @param consumer the action to perform + * @return this for method chaining + * @since 10.0.0 + */ ArrayIterationFunctions forEach(A arg1, B arg2, TriConsumer consumer); + /** + * Performs an action for each element with an object and long argument. + * + * @param the type of the first argument + * @param arg1 the first argument to pass to the consumer + * @param arg2 the long argument to pass to the consumer + * @param consumer the action to perform + * @return this for method chaining + * @since 10.0.0 + */ ArrayIterationFunctions forEach(A arg1, long arg2, ObjObjLongConsumer consumer); + /** + * Returns whether any element matches the filter predicate. + * + * @param the type of the argument + * @param arg1 the argument to pass to the filter + * @param filter the predicate to match elements + * @return true if any element matches + * @since 10.0.0 + */ boolean anyMatch(A arg1, BiPredicate filter); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/IntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/IntArray.java index 268f2381..12e468e3 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/IntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/IntArray.java @@ -7,78 +7,220 @@ import javasabr.rlib.collections.array.impl.ImmutableIntArray; /** + * An immutable array interface for primitive int values. + * * @author JavaSaBr + * @since 10.0.0 */ public interface IntArray extends Iterable, Serializable, Cloneable, RandomAccess { ImmutableIntArray EMPTY = new ImmutableIntArray(); + /** + * Returns an empty immutable int array. + * + * @return an empty int array + * @since 10.0.0 + */ static IntArray empty() { return EMPTY; } + /** + * Creates an immutable int array containing a single value. + * + * @param e1 the value + * @return an immutable int array + * @since 10.0.0 + */ static IntArray of(int e1) { return new ImmutableIntArray(e1); } + /** + * Creates an immutable int array containing two values. + * + * @param e1 the first value + * @param e2 the second value + * @return an immutable int array + * @since 10.0.0 + */ static IntArray of(int e1, int e2) { return new ImmutableIntArray(e1, e2); } + /** + * Creates an immutable int array containing three values. + * + * @param e1 the first value + * @param e2 the second value + * @param e3 the third value + * @return an immutable int array + * @since 10.0.0 + */ static IntArray of(int e1, int e2, int e3) { return new ImmutableIntArray(e1, e2, e3); } + /** + * Creates an immutable int array containing four values. + * + * @param e1 the first value + * @param e2 the second value + * @param e3 the third value + * @param e4 the fourth value + * @return an immutable int array + * @since 10.0.0 + */ static IntArray of(int e1, int e2, int e3, int e4) { return new ImmutableIntArray(e1, e2, e3, e4); } + /** + * Creates an immutable int array containing the specified values. + * + * @param elements the values to include + * @return an immutable int array + * @since 10.0.0 + */ static IntArray of(int... elements) { return new ImmutableIntArray(elements); } + /** + * Creates an immutable copy of the specified int array. + * + * @param intArray the array to copy + * @return an immutable copy + * @since 10.0.0 + */ static IntArray copyOf(IntArray intArray) { return new ImmutableIntArray(intArray.toArray()); } + /** + * Creates an immutable int array with the same value repeated. + * + * @param value the value to repeat + * @param count the number of times to repeat + * @return an immutable int array with repeated values + * @since 10.0.0 + */ static IntArray repeated(int value, int count) { int[] values = new int[count]; Arrays.fill(values, value); return new ImmutableIntArray(values); } + /** + * Returns the number of elements in this array. + * + * @return the number of elements + * @since 10.0.0 + */ int size(); + /** + * Returns whether this array contains the specified value. + * + * @param value the value to search for + * @return true if the array contains the value + * @since 10.0.0 + */ boolean contains(int value); + /** + * Returns whether this array contains all values from the specified array. + * + * @param array the array of values to check + * @return true if all values are contained + * @since 10.0.0 + */ boolean containsAll(IntArray array); + /** + * Returns whether this array contains all values from the specified array. + * + * @param array the array of values to check + * @return true if all values are contained + * @since 10.0.0 + */ boolean containsAll(int[] array); /** - * @return the first element or {@link java.util.NoSuchElementException} + * Returns the first element. + * + * @return the first element + * @throws java.util.NoSuchElementException if the array is empty + * @since 10.0.0 */ int first(); + /** + * Returns the element at the specified index. + * + * @param index the index of the element + * @return the element at the index + * @since 10.0.0 + */ int get(int index); /** - * @return the last element or {@link java.util.NoSuchElementException} + * Returns the last element. + * + * @return the last element + * @throws java.util.NoSuchElementException if the array is empty + * @since 10.0.0 */ int last(); /** - * @return the index of the value or -1. + * Returns the index of the first occurrence of the specified value. + * + * @param value the value to search for + * @return the index of the value or -1 if not found + * @since 10.0.0 */ int indexOf(int value); + /** + * Returns the index of the last occurrence of the specified value. + * + * @param value the value to search for + * @return the index of the value or -1 if not found + * @since 10.0.0 + */ int lastIndexOf(int value); + /** + * Returns whether this array is empty. + * + * @return true if empty + * @since 10.0.0 + */ boolean isEmpty(); + /** + * Returns a new primitive int array containing all elements. + * + * @return an int array + * @since 10.0.0 + */ int[] toArray(); + /** + * Returns a sequential stream of int values. + * + * @return an IntStream + * @since 10.0.0 + */ IntStream stream(); + /** + * Returns an unsafe view providing direct access to internals. + * + * @return an unsafe view + * @since 10.0.0 + */ UnsafeIntArray asUnsafe(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LockableArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LockableArray.java index 735d6977..e170d559 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LockableArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LockableArray.java @@ -4,7 +4,19 @@ import javasabr.rlib.collections.operation.LockableSource; import javasabr.rlib.common.ThreadSafe; +/** + * A thread-safe mutable array that provides locking operations for concurrent access. + * + * @param the type of elements in this array + * @since 10.0.0 + */ public interface LockableArray extends MutableArray, LockableSource, ThreadSafe { + /** + * Returns lockable operations for performing thread-safe operations on this array. + * + * @return lockable operations + * @since 10.0.0 + */ LockableOperations> operations(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java index 10db626e..f7aa7467 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java @@ -1,3 +1,4 @@ + package javasabr.rlib.collections.array; import java.io.Serializable; @@ -7,78 +8,220 @@ import javasabr.rlib.collections.array.impl.ImmutableLongArray; /** + * An immutable array interface for primitive long values. + * * @author JavaSaBr + * @since 10.0.0 */ public interface LongArray extends Iterable, Serializable, Cloneable, RandomAccess { ImmutableLongArray EMPTY = new ImmutableLongArray(); + /** + * Returns an empty immutable long array. + * + * @return an empty long array + * @since 10.0.0 + */ static LongArray empty() { return EMPTY; } + /** + * Creates an immutable long array containing a single value. + * + * @param e1 the value + * @return an immutable long array + * @since 10.0.0 + */ static LongArray of(long e1) { return new ImmutableLongArray(e1); } + /** + * Creates an immutable long array containing two values. + * + * @param e1 the first value + * @param e2 the second value + * @return an immutable long array + * @since 10.0.0 + */ static LongArray of(long e1, long e2) { return new ImmutableLongArray(e1, e2); } + /** + * Creates an immutable long array containing three values. + * + * @param e1 the first value + * @param e2 the second value + * @param e3 the third value + * @return an immutable long array + * @since 10.0.0 + */ static LongArray of(long e1, long e2, long e3) { return new ImmutableLongArray(e1, e2, e3); } + /** + * Creates an immutable long array containing four values. + * + * @param e1 the first value + * @param e2 the second value + * @param e3 the third value + * @param e4 the fourth value + * @return an immutable long array + * @since 10.0.0 + */ static LongArray of(long e1, long e2, long e3, long e4) { return new ImmutableLongArray(e1, e2, e3, e4); } + /** + * Creates an immutable long array containing the specified values. + * + * @param elements the values to include + * @return an immutable long array + * @since 10.0.0 + */ static LongArray of(long... elements) { return new ImmutableLongArray(elements); } + /** + * Creates an immutable copy of the specified long array. + * + * @param intArray the array to copy + * @return an immutable copy + * @since 10.0.0 + */ static LongArray copyOf(LongArray intArray) { return new ImmutableLongArray(intArray.toArray()); } + /** + * Creates an immutable long array with the same value repeated. + * + * @param value the value to repeat + * @param count the number of times to repeat + * @return an immutable long array with repeated values + * @since 10.0.0 + */ static LongArray repeated(long value, int count) { long[] values = new long[count]; Arrays.fill(values, value); return new ImmutableLongArray(values); } + /** + * Returns the number of elements in this array. + * + * @return the number of elements + * @since 10.0.0 + */ int size(); + /** + * Returns whether this array contains the specified value. + * + * @param value the value to search for + * @return true if the array contains the value + * @since 10.0.0 + */ boolean contains(long value); + /** + * Returns whether this array contains all values from the specified array. + * + * @param array the array of values to check + * @return true if all values are contained + * @since 10.0.0 + */ boolean containsAll(LongArray array); + /** + * Returns whether this array contains all values from the specified array. + * + * @param array the array of values to check + * @return true if all values are contained + * @since 10.0.0 + */ boolean containsAll(long[] array); /** - * @return the first element or {@link java.util.NoSuchElementException} + * Returns the first element. + * + * @return the first element + * @throws java.util.NoSuchElementException if the array is empty + * @since 10.0.0 */ long first(); + /** + * Returns the element at the specified index. + * + * @param index the index of the element + * @return the element at the index + * @since 10.0.0 + */ long get(int index); /** - * @return the last element or {@link java.util.NoSuchElementException} + * Returns the last element. + * + * @return the last element + * @throws java.util.NoSuchElementException if the array is empty + * @since 10.0.0 */ long last(); /** - * @return the index of the value or -1. + * Returns the index of the first occurrence of the specified value. + * + * @param value the value to search for + * @return the index of the value or -1 if not found + * @since 10.0.0 */ int indexOf(long value); + /** + * Returns the index of the last occurrence of the specified value. + * + * @param value the value to search for + * @return the index of the value or -1 if not found + * @since 10.0.0 + */ int lastIndexOf(long value); + /** + * Returns whether this array is empty. + * + * @return true if empty + * @since 10.0.0 + */ boolean isEmpty(); + /** + * Returns a new primitive long array containing all elements. + * + * @return a long array + * @since 10.0.0 + */ long[] toArray(); + /** + * Returns a sequential stream of long values. + * + * @return a LongStream + * @since 10.0.0 + */ LongStream stream(); + /** + * Returns an unsafe view providing direct access to internals. + * + * @return an unsafe view + * @since 10.0.0 + */ UnsafeLongArray asUnsafe(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableArray.java index 66d3d1c0..8ef26de2 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableArray.java @@ -8,25 +8,71 @@ import java.util.stream.Stream; import javasabr.rlib.collections.array.impl.DefaultMutableArray; +/** + * A mutable array interface that extends {@link Array} with modification operations. + * + * @param the type of elements in this array + * @since 10.0.0 + */ public interface MutableArray extends Array, Collection { + /** + * Creates a new mutable array of the specified type. + * + * @param the type of elements + * @param type the component type of the array + * @return a new mutable array + * @since 10.0.0 + */ static MutableArray ofType(Class type) { return new DefaultMutableArray<>(type); } + /** + * Adds all elements from the specified array. + * + * @param array the array to add elements from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(Array array); + /** + * Adds all elements from the specified mutable array. + * + * @param array the array to add elements from + * @return true if this array changed as a result + * @since 10.0.0 + */ default boolean addAll(MutableArray array) { return addAll((Array) array); } + /** + * Adds all elements from the specified Java array. + * + * @param array the array to add elements from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(E[] array); /** + * Removes the element at the specified index. + * + * @param index the index of the element to remove * @return the element previously at the specified position + * @since 10.0.0 */ E remove(int index); + /** + * Replaces the element at the specified index. + * + * @param index the index of the element to replace + * @param element the new element + * @since 10.0.0 + */ void replace(int index, E element); @Override @@ -47,10 +93,27 @@ default void forEach(Consumer action) { Array.super.forEach(action); } + /** + * Returns an unsafe mutable view of this array. + * + * @return an unsafe mutable view + * @since 10.0.0 + */ @Override UnsafeMutableArray asUnsafe(); - + + /** + * Sorts this array using natural ordering. + * + * @since 10.0.0 + */ void sort(); - + + /** + * Sorts this array using the specified comparator. + * + * @param comparator the comparator to determine element order + * @since 10.0.0 + */ void sort(Comparator comparator); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableIntArray.java index d119cd65..f7a3c371 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableIntArray.java @@ -1,28 +1,97 @@ package javasabr.rlib.collections.array; +/** + * A mutable array interface for primitive int values. + * + * @since 10.0.0 + */ public interface MutableIntArray extends IntArray { + /** + * Adds a value to this array. + * + * @param value the value to add + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean add(int value); + /** + * Adds all values from the specified int array. + * + * @param array the array to add values from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(IntArray array); + /** + * Adds all values from the specified primitive int array. + * + * @param array the array to add values from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(int[] array); /** + * Removes the element at the specified index. + * + * @param index the index of the element to remove * @return the element previously at the specified position + * @since 10.0.0 */ int removeByIndex(int index); + /** + * Removes the first occurrence of the specified value. + * + * @param value the value to remove + * @return true if this array contained the value + * @since 10.0.0 + */ boolean remove(int value); + /** + * Removes all values that are contained in the specified array. + * + * @param array the array of values to remove + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean removeAll(IntArray array); + /** + * Removes all values that are contained in the specified array. + * + * @param array the array of values to remove + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean removeAll(int[] array); + /** + * Replaces the value at the specified index. + * + * @param index the index of the value to replace + * @param value the new value + * @since 10.0.0 + */ void replace(int index, int value); + /** + * Removes all values from this array. + * + * @since 10.0.0 + */ void clear(); + /** + * Returns an unsafe mutable view of this array. + * + * @return an unsafe mutable view + * @since 10.0.0 + */ @Override UnsafeMutableIntArray asUnsafe(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableLongArray.java index dfea7d28..8da91528 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableLongArray.java @@ -1,28 +1,97 @@ package javasabr.rlib.collections.array; +/** + * A mutable array interface for primitive long values. + * + * @since 10.0.0 + */ public interface MutableLongArray extends LongArray { + /** + * Adds a value to this array. + * + * @param value the value to add + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean add(long value); + /** + * Adds all values from the specified long array. + * + * @param array the array to add values from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(LongArray array); + /** + * Adds all values from the specified primitive long array. + * + * @param array the array to add values from + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean addAll(long[] array); /** + * Removes the element at the specified index. + * + * @param index the index of the element to remove * @return the element previously at the specified position + * @since 10.0.0 */ long removeByIndex(int index); + /** + * Removes the first occurrence of the specified value. + * + * @param value the value to remove + * @return true if this array contained the value + * @since 10.0.0 + */ boolean remove(long value); + /** + * Removes all values that are contained in the specified array. + * + * @param array the array of values to remove + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean removeAll(LongArray array); + /** + * Removes all values that are contained in the specified array. + * + * @param array the array of values to remove + * @return true if this array changed as a result + * @since 10.0.0 + */ boolean removeAll(long[] array); + /** + * Replaces the value at the specified index. + * + * @param index the index of the value to replace + * @param value the new value + * @since 10.0.0 + */ void replace(int index, long value); + /** + * Removes all values from this array. + * + * @since 10.0.0 + */ void clear(); + /** + * Returns an unsafe mutable view of this array. + * + * @return an unsafe mutable view + * @since 10.0.0 + */ @Override UnsafeMutableLongArray asUnsafe(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ReversedArgsArrayIterationFunctions.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ReversedArgsArrayIterationFunctions.java index b01e44af..30f6564c 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/ReversedArgsArrayIterationFunctions.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/ReversedArgsArrayIterationFunctions.java @@ -5,14 +5,58 @@ import javasabr.rlib.functions.TriConsumer; import org.jspecify.annotations.Nullable; +/** + * Provides iteration functions for arrays with reversed argument order in callbacks. + * + * @param the type of elements in the array + * @since 10.0.0 + */ public interface ReversedArgsArrayIterationFunctions { + /** + * Finds any element matching the filter predicate with reversed argument order. + * + * @param the type of the argument + * @param arg1 the argument to pass to the filter + * @param filter the predicate to match elements (argument first, then element) + * @return the matching element or null + * @since 10.0.0 + */ @Nullable E findAny(A arg1, BiPredicate filter); + /** + * Performs an action for each element with reversed argument order. + * + * @param the type of the argument + * @param arg1 the argument to pass to the consumer + * @param consumer the action to perform (argument first, then element) + * @return this for method chaining + * @since 10.0.0 + */ ReversedArgsArrayIterationFunctions forEach(A arg1, BiConsumer consumer); + /** + * Performs an action for each element with two additional arguments in reversed order. + * + * @param the type of the first argument + * @param the type of the second argument + * @param arg1 the first argument to pass to the consumer + * @param arg2 the second argument to pass to the consumer + * @param consumer the action to perform (arguments first, then element) + * @return this for method chaining + * @since 10.0.0 + */ ReversedArgsArrayIterationFunctions forEach(A arg1, B arg2, TriConsumer consumer); + /** + * Returns whether any element matches the filter predicate with reversed argument order. + * + * @param the type of the argument + * @param arg1 the argument to pass to the filter + * @param filter the predicate to match elements (argument first, then element) + * @return true if any element matches + * @since 10.0.0 + */ boolean anyMatch(A arg1, BiPredicate filter); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeArray.java index eea2c7df..2e90748b 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeArray.java @@ -2,8 +2,29 @@ import org.jspecify.annotations.Nullable; +/** + * An unsafe view of an array providing direct access to internal data structures. + * Use with caution as modifications may violate array invariants. + * + * @param the type of elements in this array + * @since 10.0.0 + */ public interface UnsafeArray extends Array { + + /** + * Returns the internal backing array. + * + * @return the backing array or null + * @since 10.0.0 + */ @Nullable E[] wrapped(); + /** + * Returns the element at the specified index without bounds checking. + * + * @param index the index of the element + * @return the element at the index + * @since 10.0.0 + */ E unsafeGet(int index); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeIntArray.java index d456511c..26479328 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeIntArray.java @@ -1,6 +1,27 @@ package javasabr.rlib.collections.array; +/** + * An unsafe view of an int array providing direct access to internal data structures. + * Use with caution as modifications may violate array invariants. + * + * @since 10.0.0 + */ public interface UnsafeIntArray extends IntArray { + + /** + * Returns the internal backing array. + * + * @return the backing array + * @since 10.0.0 + */ int[] wrapped(); + + /** + * Returns the element at the specified index without bounds checking. + * + * @param index the index of the element + * @return the element at the index + * @since 10.0.0 + */ int unsafeGet(int index); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeLongArray.java index 35668235..ac7c1a13 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeLongArray.java @@ -1,6 +1,27 @@ package javasabr.rlib.collections.array; +/** + * An unsafe view of a long array providing direct access to internal data structures. + * Use with caution as modifications may violate array invariants. + * + * @since 10.0.0 + */ public interface UnsafeLongArray extends LongArray { + + /** + * Returns the internal backing array. + * + * @return the backing array + * @since 10.0.0 + */ long[] wrapped(); + + /** + * Returns the element at the specified index without bounds checking. + * + * @param index the index of the element + * @return the element at the index + * @since 10.0.0 + */ long unsafeGet(int index); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableArray.java index 7da5a8b7..ff2297c7 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableArray.java @@ -1,14 +1,56 @@ package javasabr.rlib.collections.array; +/** + * An unsafe mutable view of an array providing direct modification operations. + * Use with caution as modifications may violate array invariants. + * + * @param the type of elements in this array + * @since 10.0.0 + */ public interface UnsafeMutableArray extends UnsafeArray, MutableArray { + /** + * Prepares internal storage for the expected size to avoid reallocations. + * + * @param expectedSize the expected number of elements + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableArray prepareForSize(int expectedSize); + /** + * Adds an element without triggering copy-on-write or other safety mechanisms. + * + * @param element the element to add + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableArray unsafeAdd(E element); + /** + * Removes the element at the specified index without bounds checking. + * + * @param index the index of the element to remove + * @return the removed element + * @since 10.0.0 + */ E unsafeRemove(int index); + /** + * Sets the element at the specified index without bounds checking. + * + * @param index the index of the element to set + * @param element the new element + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableArray unsafeSet(int index, E element); + /** + * Trims the internal storage to match the current size. + * + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableArray trimToSize(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java index 0274df9e..4a74dd1c 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java @@ -1,14 +1,55 @@ package javasabr.rlib.collections.array; +/** + * An unsafe mutable view of an int array providing direct modification operations. + * Use with caution as modifications may violate array invariants. + * + * @since 10.0.0 + */ public interface UnsafeMutableIntArray extends UnsafeIntArray, MutableIntArray { + /** + * Prepares internal storage for the expected size to avoid reallocations. + * + * @param expectedSize the expected number of elements + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableIntArray prepareForSize(int expectedSize); + /** + * Adds a value without triggering safety mechanisms. + * + * @param value the value to add + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableIntArray unsafeAdd(int value); + /** + * Removes the element at the specified index without bounds checking. + * + * @param index the index of the element to remove + * @return the removed value + * @since 10.0.0 + */ int unsafeRemoveByInex(int index); + /** + * Sets the value at the specified index without bounds checking. + * + * @param index the index of the value to set + * @param value the new value + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableIntArray unsafeSet(int index, int value); + /** + * Trims the internal storage to match the current size. + * + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableIntArray trimToSize(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java index a78f81d0..9f26bd1c 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java @@ -1,14 +1,55 @@ package javasabr.rlib.collections.array; +/** + * An unsafe mutable view of a long array providing direct modification operations. + * Use with caution as modifications may violate array invariants. + * + * @since 10.0.0 + */ public interface UnsafeMutableLongArray extends UnsafeLongArray, MutableLongArray { + /** + * Prepares internal storage for the expected size to avoid reallocations. + * + * @param expectedSize the expected number of elements + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableLongArray prepareForSize(int expectedSize); + /** + * Adds a value without triggering safety mechanisms. + * + * @param value the value to add + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableLongArray unsafeAdd(long value); + /** + * Removes the element at the specified index without bounds checking. + * + * @param index the index of the element to remove + * @return the removed value + * @since 10.0.0 + */ long unsafeRemoveByInex(int index); + /** + * Sets the value at the specified index without bounds checking. + * + * @param index the index of the value to set + * @param value the new value + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableLongArray unsafeSet(int index, long value); + /** + * Trims the internal storage to match the current size. + * + * @return this for method chaining + * @since 10.0.0 + */ UnsafeMutableLongArray trimToSize(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java b/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java index f07a3c79..c713797f 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java @@ -6,19 +6,46 @@ import lombok.experimental.UtilityClass; /** + * Factory for creating various deque implementations. + * * @author JavaSaBr + * @since 10.0.0 */ @UtilityClass public class DequeFactory { + /** + * Creates a new deque backed by a linked list. + * + * @param the type of elements + * @return a new linked list based deque + * @since 10.0.0 + */ public static Deque linkedListBased() { return new DefaultLinkedListBasedDeque<>(); } + /** + * Creates a new deque backed by an array. + * + * @param the type of elements + * @param type the component type of the array + * @return a new array based deque + * @since 10.0.0 + */ public static Deque arrayBasedBased(Class type) { return new DefaultArrayBasedDeque<>(type); } + /** + * Creates a new deque backed by an array with initial capacity. + * + * @param the type of elements + * @param type the component type of the array + * @param capacity the initial capacity + * @return a new array based deque + * @since 10.0.0 + */ public static Deque arrayBasedBased(Class type, int capacity) { return new DefaultArrayBasedDeque<>(type, capacity); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/Dictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/Dictionary.java index f6a12b91..1ce49315 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/Dictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/Dictionary.java @@ -7,34 +7,131 @@ import org.jspecify.annotations.Nullable; /** + * A base interface for dictionary (map-like) data structures. + * + * @param the type of keys + * @param the type of values * @author JavaSaBr + * @since 10.0.0 */ public interface Dictionary extends Iterable { + /** + * Returns whether this dictionary contains the specified key. + * + * @param key the key to check + * @return true if the key is present + * @since 10.0.0 + */ boolean containsKey(K key); + /** + * Returns whether this dictionary contains the specified value. + * + * @param value the value to check + * @return true if the value is present + * @since 10.0.0 + */ boolean containsValue(V value); + /** + * Returns whether this dictionary is empty. + * + * @return true if empty + * @since 10.0.0 + */ boolean isEmpty(); + /** + * Returns the number of key-value mappings. + * + * @return the number of mappings + * @since 10.0.0 + */ int size(); + /** + * Returns the value associated with the specified key. + * + * @param key the key + * @return the value or null if not found + * @since 10.0.0 + */ @Nullable V get(K key); + /** + * Returns an optional containing the value for the specified key. + * + * @param key the key + * @return an optional with the value + * @since 10.0.0 + */ Optional getOptional(K key); + /** + * Returns the value for the key, or a default value if not found. + * + * @param key the key + * @param def the default value + * @return the value or the default + * @since 10.0.0 + */ V getOrDefault(K key, V def); + /** + * Collects all keys into the provided collection. + * + * @param the collection type + * @param container the collection to add keys to + * @return the container with added keys + * @since 10.0.0 + */ > C keys(C container); + /** + * Collects all keys into the provided mutable array. + * + * @param container the array to add keys to + * @return the container with added keys + * @since 10.0.0 + */ MutableArray keys(MutableArray container); + /** + * Returns all keys as an immutable array. + * + * @param type the type of keys + * @return an array of keys + * @since 10.0.0 + */ Array keys(Class type); + /** + * Collects all values into the provided collection. + * + * @param the collection type + * @param container the collection to add values to + * @return the container with added values + * @since 10.0.0 + */ > C values(C container); + /** + * Collects all values into the provided mutable array. + * + * @param container the array to add values to + * @return the container with added values + * @since 10.0.0 + */ MutableArray values(MutableArray container); + /** + * Returns all values as an immutable array. + * + * @param type the type of values + * @return an array of values + * @since 10.0.0 + */ Array values(Class type); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryCollectors.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryCollectors.java index d93d13a2..ef8f8998 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryCollectors.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryCollectors.java @@ -18,6 +18,11 @@ import lombok.experimental.FieldDefaults; import lombok.experimental.UtilityClass; +/** + * Provides {@link Collector} implementations for collecting stream elements into {@link RefToRefDictionary} instances. + * + * @since 10.0.0 + */ @UtilityClass public class DictionaryCollectors { @@ -44,6 +49,16 @@ static class CollectorImpl implements Collector { } } + /** + * Returns a collector that accumulates elements into a dictionary using the element as the value. + * + * @param the type of input elements + * @param the type of keys + * @param unused type parameter + * @param keyMapper the function to extract keys + * @return a collector that collects elements into a dictionary + * @since 10.0.0 + */ public static Collector, RefToRefDictionary> toRefToRefDictionary( Function keyMapper) { return new CollectorImpl<>( @@ -54,6 +69,17 @@ public static Collector, RefToRefDi CH_ID); } + /** + * Returns a collector that accumulates elements into a dictionary. + * + * @param the type of input elements + * @param the type of keys + * @param the type of values + * @param keyMapper the function to extract keys + * @param valueMapper the function to extract values + * @return a collector that collects elements into a dictionary + * @since 10.0.0 + */ public static Collector, RefToRefDictionary> toRefToRefDictionary( Function keyMapper, Function valueMapper) { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryFactory.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryFactory.java index e18b13ae..e6321315 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryFactory.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/DictionaryFactory.java @@ -7,34 +7,97 @@ import javasabr.rlib.collections.dictionary.impl.gc.optimized.GcOptimizedMutableHashBasedIntToRefDictionary; import lombok.experimental.UtilityClass; +/** + * Factory for creating various dictionary implementations. + * + * @since 10.0.0 + */ @UtilityClass public class DictionaryFactory { + + /** + * Creates a new mutable reference-to-reference dictionary. + * + * @param the type of keys + * @param the type of values + * @return a new mutable dictionary + * @since 10.0.0 + */ public static MutableRefToRefDictionary mutableRefToRefDictionary() { return new DefaultMutableHashBasedRefToRefDictionary<>(); } + /** + * Creates a new mutable int-to-reference dictionary. + * + * @param the type of values + * @return a new mutable dictionary + * @since 10.0.0 + */ public static MutableIntToRefDictionary mutableIntToRefDictionary() { return new DefaultMutableHashBasedIntToRefDictionary<>(); } + /** + * Creates a new GC-optimized mutable int-to-reference dictionary. + * + * @param the type of values + * @return a new GC-optimized mutable dictionary + * @since 10.0.0 + */ public static MutableIntToRefDictionary gcOptimizedIntToRefDictionary() { return new GcOptimizedMutableHashBasedIntToRefDictionary<>(); } + /** + * Creates a new mutable long-to-reference dictionary. + * + * @param the type of values + * @return a new mutable dictionary + * @since 10.0.0 + */ public static MutableLongToRefDictionary mutableLongToRefDictionary() { return new DefaultMutableHashBasedLongToRefDictionary<>(); } + /** + * Creates a new mutable reference-to-reference dictionary with explicit types. + * + * @param the type of keys + * @param the type of values + * @param keyType the key type (unused, for type inference) + * @param valueType the value type (unused, for type inference) + * @return a new mutable dictionary + * @since 10.0.0 + */ public static MutableRefToRefDictionary mutableRefToRefDictionary( Class keyType, Class valueType) { return new DefaultMutableHashBasedRefToRefDictionary<>(); } + /** + * Creates a new thread-safe dictionary backed by a stamped lock. + * + * @param the type of keys + * @param the type of values + * @return a new lockable dictionary + * @since 10.0.0 + */ public static LockableRefToRefDictionary stampedLockBasedRefToRefDictionary() { return new StampedLockBasedHashBasedRefToRefDictionary<>(); } + /** + * Creates a new thread-safe dictionary backed by a stamped lock with explicit types. + * + * @param the type of keys + * @param the type of values + * @param keyType the key type (unused, for type inference) + * @param valueType the value type (unused, for type inference) + * @return a new lockable dictionary + * @since 10.0.0 + */ public static LockableRefToRefDictionary stampedLockBasedRefToRefDictionary( Class keyType, Class valueType) { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/HashEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/HashEntry.java index 885f43f0..a27fad32 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/HashEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/HashEntry.java @@ -1,5 +1,17 @@ package javasabr.rlib.collections.dictionary; +/** + * An entry that stores its hash code for hash-based data structures. + * + * @since 10.0.0 + */ public interface HashEntry { + + /** + * Returns the hash code of this entry. + * + * @return the hash code + * @since 10.0.0 + */ int hash(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefDictionary.java index 71a6ea65..4a198619 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefDictionary.java @@ -8,24 +8,74 @@ import javasabr.rlib.functions.IntObjConsumer; import org.jspecify.annotations.Nullable; +/** + * A dictionary that maps int keys to reference values. + * + * @param the type of values + * @since 10.0.0 + */ public interface IntToRefDictionary extends Dictionary { + /** + * Creates a new entry with the specified key and value. + * + * @param the type of the value + * @param key the key + * @param value the value + * @return a new entry + * @since 10.0.0 + */ static IntToRefEntry entry(int key, V value) { return new SimpleIntToRefEntry<>(key, value); } + /** + * Returns an empty immutable int-to-reference dictionary. + * + * @param the type of values + * @return an empty dictionary + * @since 10.0.0 + */ static IntToRefDictionary empty() { return ImmutableHashBasedIntToRefDictionary.empty(); } + /** + * Creates an immutable dictionary with a single mapping. + * + * @param the type of values + * @param key the key + * @param value the value + * @return an immutable dictionary + * @since 10.0.0 + */ static IntToRefDictionary of(int key, V value) { return ofEntries(entry(key, value)); } + /** + * Creates an immutable dictionary with two mappings. + * + * @param the type of values + * @param k1 the first key + * @param v1 the first value + * @param k2 the second key + * @param v2 the second value + * @return an immutable dictionary + * @since 10.0.0 + */ static IntToRefDictionary of(int k1, V v1, int k2, V v2) { return ofEntries(entry(k1, v1), entry(k2, v2)); } + /** + * Creates an immutable dictionary from entries. + * + * @param the type of values + * @param entries the entries + * @return an immutable dictionary + * @since 10.0.0 + */ @SafeVarargs static IntToRefDictionary ofEntries(IntToRefEntry... entries) { MutableIntToRefDictionary mutable = DictionaryFactory.mutableIntToRefDictionary(); @@ -35,18 +85,66 @@ static IntToRefDictionary ofEntries(IntToRefEntry... entries) { return mutable.toReadOnly(); } + /** + * Returns whether this dictionary contains the specified key. + * + * @param key the key to check + * @return true if the key is present + * @since 10.0.0 + */ boolean containsKey(int key); + /** + * Returns the value for the specified key. + * + * @param key the key + * @return the value or null if not found + * @since 10.0.0 + */ @Nullable V get(int key); + /** + * Returns an optional containing the value for the specified key. + * + * @param key the key + * @return an optional with the value + * @since 10.0.0 + */ Optional getOptional(int key); + /** + * Returns the value for the key, or a default value if not found. + * + * @param key the key + * @param def the default value + * @return the value or the default + * @since 10.0.0 + */ V getOrDefault(int key, V def); + /** + * Collects all keys into the provided mutable int array. + * + * @param container the array to add keys to + * @return the container with added keys + * @since 10.0.0 + */ MutableIntArray keys(MutableIntArray container); + /** + * Returns all keys as an immutable int array. + * + * @return an array of keys + * @since 10.0.0 + */ IntArray keys(); + /** + * Performs the given action for each key-value pair. + * + * @param consumer the action to perform + * @since 10.0.0 + */ void forEach(IntObjConsumer consumer); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefEntry.java index 2aa7c172..6c3757a6 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/IntToRefEntry.java @@ -1,7 +1,26 @@ package javasabr.rlib.collections.dictionary; +/** + * An entry that maps an int key to a reference value. + * + * @param the type of the value + * @since 10.0.0 + */ public interface IntToRefEntry extends RefEntry { + /** + * Returns the int key of this entry. + * + * @return the key + * @since 10.0.0 + */ int key(); + + /** + * Sets the int key of this entry. + * + * @param key the new key + * @since 10.0.0 + */ void key(int key); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedEntry.java index c3312877..db9b32ab 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedEntry.java @@ -2,8 +2,28 @@ import org.jspecify.annotations.Nullable; +/** + * An entry that maintains a link to the next entry in a chain. + * + * @param the type of the next entry + * @since 10.0.0 + */ public interface LinkedEntry { + + /** + * Returns the next entry in the chain. + * + * @return the next entry or null + * @since 10.0.0 + */ @Nullable N next(); + + /** + * Sets the next entry in the chain. + * + * @param next the next entry or null + * @since 10.0.0 + */ void next(@Nullable N next); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashEntry.java index 7d472f8c..f3ab21b7 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashEntry.java @@ -1,5 +1,13 @@ package javasabr.rlib.collections.dictionary; +/** + * A linked hash entry for reference-to-reference dictionaries. + * + * @param the type of keys + * @param the type of values + * @param the type of the next entry + * @since 10.0.0 + */ public interface LinkedHashEntry> extends LinkedEntry, HashEntry, RefToRefEntry { } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashIntToRefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashIntToRefEntry.java index a2b74e79..3980a27d 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashIntToRefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashIntToRefEntry.java @@ -1,5 +1,12 @@ package javasabr.rlib.collections.dictionary; +/** + * A linked hash entry for int-to-reference dictionaries. + * + * @param the type of values + * @param the type of the next entry + * @since 10.0.0 + */ public interface LinkedHashIntToRefEntry> extends LinkedEntry, HashEntry, IntToRefEntry { } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashLongToRefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashLongToRefEntry.java index 21ad18db..4008447d 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashLongToRefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LinkedHashLongToRefEntry.java @@ -1,5 +1,12 @@ package javasabr.rlib.collections.dictionary; +/** + * A linked hash entry for long-to-reference dictionaries. + * + * @param the type of values + * @param the type of the next entry + * @since 10.0.0 + */ public interface LinkedHashLongToRefEntry> extends LinkedEntry, HashEntry, LongToRefEntry { } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LockableRefToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LockableRefToRefDictionary.java index 07d16c71..5c1bda07 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LockableRefToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LockableRefToRefDictionary.java @@ -4,8 +4,21 @@ import javasabr.rlib.collections.operation.LockableSource; import javasabr.rlib.common.ThreadSafe; +/** + * A thread-safe mutable dictionary that provides locking operations for concurrent access. + * + * @param the type of keys + * @param the type of values + * @since 10.0.0 + */ public interface LockableRefToRefDictionary extends MutableRefToRefDictionary, LockableSource, ThreadSafe { + /** + * Returns lockable operations for performing thread-safe operations on this dictionary. + * + * @return lockable operations + * @since 10.0.0 + */ LockableOperations> operations(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefDictionary.java index bb1d7fd7..09964c0c 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefDictionary.java @@ -8,24 +8,74 @@ import javasabr.rlib.functions.LongObjConsumer; import org.jspecify.annotations.Nullable; +/** + * A dictionary that maps long keys to reference values. + * + * @param the type of values + * @since 10.0.0 + */ public interface LongToRefDictionary extends Dictionary { + /** + * Creates a new entry with the specified key and value. + * + * @param the type of the value + * @param key the key + * @param value the value + * @return a new entry + * @since 10.0.0 + */ static LongToRefEntry entry(long key, V value) { return new SimpleLongToRefEntry<>(key, value); } + /** + * Returns an empty immutable long-to-reference dictionary. + * + * @param the type of values + * @return an empty dictionary + * @since 10.0.0 + */ static LongToRefDictionary empty() { return ImmutableHashBasedLongToRefDictionary.empty(); } + /** + * Creates an immutable dictionary with a single mapping. + * + * @param the type of values + * @param key the key + * @param value the value + * @return an immutable dictionary + * @since 10.0.0 + */ static LongToRefDictionary of(long key, V value) { return ofEntries(entry(key, value)); } + /** + * Creates an immutable dictionary with two mappings. + * + * @param the type of values + * @param k1 the first key + * @param v1 the first value + * @param k2 the second key + * @param v2 the second value + * @return an immutable dictionary + * @since 10.0.0 + */ static LongToRefDictionary of(long k1, V v1, long k2, V v2) { return ofEntries(entry(k1, v1), entry(k2, v2)); } + /** + * Creates an immutable dictionary from entries. + * + * @param the type of values + * @param entries the entries + * @return an immutable dictionary + * @since 10.0.0 + */ @SafeVarargs static LongToRefDictionary ofEntries(LongToRefEntry... entries) { MutableLongToRefDictionary mutable = DictionaryFactory.mutableLongToRefDictionary(); @@ -35,18 +85,66 @@ static LongToRefDictionary ofEntries(LongToRefEntry... entries) { return mutable.toReadOnly(); } + /** + * Returns whether this dictionary contains the specified key. + * + * @param key the key to check + * @return true if the key is present + * @since 10.0.0 + */ boolean containsKey(long key); + /** + * Returns the value for the specified key. + * + * @param key the key + * @return the value or null if not found + * @since 10.0.0 + */ @Nullable V get(long key); + /** + * Returns an optional containing the value for the specified key. + * + * @param key the key + * @return an optional with the value + * @since 10.0.0 + */ Optional getOptional(long key); + /** + * Returns the value for the key, or a default value if not found. + * + * @param key the key + * @param def the default value + * @return the value or the default + * @since 10.0.0 + */ V getOrDefault(long key, V def); + /** + * Collects all keys into the provided mutable long array. + * + * @param container the array to add keys to + * @return the container with added keys + * @since 10.0.0 + */ MutableLongArray keys(MutableLongArray container); + /** + * Returns all keys as an immutable long array. + * + * @return an array of keys + * @since 10.0.0 + */ LongArray keys(); + /** + * Performs the given action for each key-value pair. + * + * @param consumer the action to perform + * @since 10.0.0 + */ void forEach(LongObjConsumer consumer); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefEntry.java index 142feced..7cfbebb0 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/LongToRefEntry.java @@ -1,6 +1,26 @@ package javasabr.rlib.collections.dictionary; +/** + * An entry that maps a long key to a reference value. + * + * @param the type of the value + * @since 10.0.0 + */ public interface LongToRefEntry extends RefEntry { + + /** + * Returns the long key of this entry. + * + * @return the key + * @since 10.0.0 + */ long key(); + + /** + * Sets the long key of this entry. + * + * @param key the new key + * @since 10.0.0 + */ void key(long key); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableIntToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableIntToRefDictionary.java index 6516ed43..da605f08 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableIntToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableIntToRefDictionary.java @@ -7,56 +7,148 @@ import javasabr.rlib.collections.dictionary.impl.DefaultMutableHashBasedIntToRefDictionary; import org.jspecify.annotations.Nullable; +/** + * A mutable dictionary that maps int keys to reference values. + * + * @param the type of values + * @since 10.0.0 + */ public interface MutableIntToRefDictionary extends IntToRefDictionary { + /** + * Creates a new mutable int-to-reference dictionary. + * + * @param the type of values + * @param valueType the value type (unused, for type inference) + * @return a new mutable dictionary + * @since 10.0.0 + */ static MutableIntToRefDictionary ofTypes(Class valueType) { return new DefaultMutableHashBasedIntToRefDictionary<>(); } + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(int key, Supplier factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value from the key + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(int key, IntFunction factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param the type of the factory argument + * @param key the key + * @param arg1 the argument to pass to the factory + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(int key, T arg1, Function factory); /** - * @return the previous value for the key or null. + * Associates the value with the key. + * + * @param key the key + * @param value the value + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V put(int key, V value); /** - * @return the existing value if the key is already present, or null if the key was absent and the new mapping was added. + * Associates the value with the key if not already present. + * + * @param key the key + * @param value the value + * @return the existing value if present, or null if added + * @since 10.0.0 */ @Nullable V putIfAbsent(int key, V value); - + + /** + * Copies all mappings from the specified dictionary. + * + * @param dictionary the dictionary to copy from + * @since 10.0.0 + */ void putAll(IntToRefDictionary dictionary); + /** + * Copies all mappings from the specified dictionary and returns this. + * + * @param dictionary the dictionary to copy from + * @return this dictionary + * @since 10.0.0 + */ MutableIntToRefDictionary append(IntToRefDictionary dictionary); /** - * @return the optional value of the previous value for the key. + * Associates the value with the key and returns an optional of the previous value. + * + * @param key the key + * @param value the value + * @return an optional of the previous value + * @since 10.0.0 */ Optional putOptional(int key, V value); /** - * @return the previous value for the key or null. + * Removes the mapping for the key. + * + * @param key the key + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V remove(int key); /** - * @return true if the expectedValue was removed + * Removes the mapping for the key only if it maps to the expected value. + * + * @param key the key + * @param expectedValue the expected value + * @return true if the expected value was removed + * @since 10.0.0 */ boolean remove(int key, V expectedValue); - + /** - * @return the optional value of the previous value for the key. + * Removes the mapping for the key and returns an optional of the previous value. + * + * @param key the key + * @return an optional of the previous value + * @since 10.0.0 */ Optional removeOptional(int key); + /** + * Removes all mappings from this dictionary. + * + * @since 10.0.0 + */ void clear(); + /** + * Returns an immutable copy of this dictionary. + * + * @return an immutable dictionary + * @since 10.0.0 + */ IntToRefDictionary toReadOnly(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableLongToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableLongToRefDictionary.java index 15866783..ecb973be 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableLongToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableLongToRefDictionary.java @@ -7,56 +7,148 @@ import javasabr.rlib.collections.dictionary.impl.DefaultMutableHashBasedLongToRefDictionary; import org.jspecify.annotations.Nullable; +/** + * A mutable dictionary that maps long keys to reference values. + * + * @param the type of values + * @since 10.0.0 + */ public interface MutableLongToRefDictionary extends LongToRefDictionary { + /** + * Creates a new mutable long-to-reference dictionary. + * + * @param the type of values + * @param valueType the value type (unused, for type inference) + * @return a new mutable dictionary + * @since 10.0.0 + */ static MutableLongToRefDictionary ofTypes(Class valueType) { return new DefaultMutableHashBasedLongToRefDictionary<>(); } + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(long key, Supplier factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value from the key + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(long key, LongFunction factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param the type of the factory argument + * @param key the key + * @param arg1 the argument to pass to the factory + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(long key, T arg1, Function factory); /** - * @return the previous value for the key or null. + * Associates the value with the key. + * + * @param key the key + * @param value the value + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V put(long key, V value); /** - * @return the existing value if the key is already present, or null if the key was absent and the new mapping was added. + * Associates the value with the key if not already present. + * + * @param key the key + * @param value the value + * @return the existing value if present, or null if added + * @since 10.0.0 */ @Nullable V putIfAbsent(long key, V value); - + + /** + * Copies all mappings from the specified dictionary. + * + * @param dictionary the dictionary to copy from + * @since 10.0.0 + */ void putAll(LongToRefDictionary dictionary); + /** + * Copies all mappings from the specified dictionary and returns this. + * + * @param dictionary the dictionary to copy from + * @return this dictionary + * @since 10.0.0 + */ MutableLongToRefDictionary append(LongToRefDictionary dictionary); /** - * @return the optional value of the previous value for the key. + * Associates the value with the key and returns an optional of the previous value. + * + * @param key the key + * @param value the value + * @return an optional of the previous value + * @since 10.0.0 */ Optional putOptional(long key, V value); /** - * @return the previous value for the key or null. + * Removes the mapping for the key. + * + * @param key the key + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V remove(long key); /** - * @return true if the expectedValue was removed + * Removes the mapping for the key only if it maps to the expected value. + * + * @param key the key + * @param expectedValue the expected value + * @return true if the expected value was removed + * @since 10.0.0 */ boolean remove(long key, V expectedValue); - + /** - * @return the optional value of the previous value for the key. + * Removes the mapping for the key and returns an optional of the previous value. + * + * @param key the key + * @return an optional of the previous value + * @since 10.0.0 */ Optional removeOptional(long key); + /** + * Removes all mappings from this dictionary. + * + * @since 10.0.0 + */ void clear(); + /** + * Returns an immutable copy of this dictionary. + * + * @return an immutable dictionary + * @since 10.0.0 + */ LongToRefDictionary toReadOnly(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionary.java index ff3f1437..bf017599 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/MutableRefToRefDictionary.java @@ -6,58 +6,153 @@ import javasabr.rlib.collections.dictionary.impl.DefaultMutableHashBasedRefToRefDictionary; import org.jspecify.annotations.Nullable; +/** + * A mutable dictionary that maps reference keys to reference values. + * + * @param the type of keys + * @param the type of values + * @since 10.0.0 + */ public interface MutableRefToRefDictionary extends RefToRefDictionary { + /** + * Creates a new mutable reference-to-reference dictionary. + * + * @param the type of keys + * @param the type of values + * @param keyType the key type (unused, for type inference) + * @param valueType the value type (unused, for type inference) + * @return a new mutable dictionary + * @since 10.0.0 + */ static MutableRefToRefDictionary ofTypes( Class keyType, Class valueType) { return new DefaultMutableHashBasedRefToRefDictionary<>(); } + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(K key, Supplier factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param key the key + * @param factory the factory to compute the value from the key + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(K key, Function factory); + /** + * Returns the value for the key, computing it if absent. + * + * @param the type of the factory argument + * @param key the key + * @param arg1 the argument to pass to the factory + * @param factory the factory to compute the value + * @return the existing or computed value + * @since 10.0.0 + */ V getOrCompute(K key, T arg1, Function factory); /** - * @return the previous value for the key or null. + * Associates the value with the key. + * + * @param key the key + * @param value the value + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V put(K key, V value); /** - * @return the existing value if the key is already present, or null if the key was absent and the new mapping was added. + * Associates the value with the key if not already present. + * + * @param key the key + * @param value the value + * @return the existing value if present, or null if added + * @since 10.0.0 */ @Nullable V putIfAbsent(K key, V value); - + + /** + * Copies all mappings from the specified dictionary. + * + * @param dictionary the dictionary to copy from + * @since 10.0.0 + */ void putAll(RefToRefDictionary dictionary); + /** + * Copies all mappings from the specified dictionary and returns this. + * + * @param dictionary the dictionary to copy from + * @return this dictionary + * @since 10.0.0 + */ MutableRefToRefDictionary append(RefToRefDictionary dictionary); /** - * @return the optional value of the previous value for the key. + * Associates the value with the key and returns an optional of the previous value. + * + * @param key the key + * @param value the value + * @return an optional of the previous value + * @since 10.0.0 */ Optional putOptional(K key, V value); /** - * @return the previous value for the key or null. + * Removes the mapping for the key. + * + * @param key the key + * @return the previous value for the key or null + * @since 10.0.0 */ @Nullable V remove(K key); /** - * @return true if the expectedValue was removed + * Removes the mapping for the key only if it maps to the expected value. + * + * @param key the key + * @param expectedValue the expected value + * @return true if the expected value was removed + * @since 10.0.0 */ boolean remove(K key, V expectedValue); - + /** - * @return the optional value of the previous value for the key. + * Removes the mapping for the key and returns an optional of the previous value. + * + * @param key the key + * @return an optional of the previous value + * @since 10.0.0 */ Optional removeOptional(K key); + /** + * Removes all mappings from this dictionary. + * + * @since 10.0.0 + */ void clear(); + /** + * Returns an immutable copy of this dictionary. + * + * @return an immutable dictionary + * @since 10.0.0 + */ RefToRefDictionary toReadOnly(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefEntry.java index dd2f75b0..f9b14316 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefEntry.java @@ -2,7 +2,27 @@ import org.jspecify.annotations.Nullable; +/** + * An entry that holds a reference value. + * + * @param the type of the value + * @since 10.0.0 + */ public interface RefEntry { + + /** + * Returns the value of this entry. + * + * @return the value or null + * @since 10.0.0 + */ @Nullable V value(); + + /** + * Sets the value of this entry. + * + * @param value the new value or null + * @since 10.0.0 + */ void value(@Nullable V value); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionary.java index cb489e04..5c80f614 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionary.java @@ -4,47 +4,161 @@ import javasabr.rlib.collections.dictionary.impl.ImmutableHashBasedRefToRefDictionary; import javasabr.rlib.collections.dictionary.impl.SimpleRefToRefEntry; +/** + * A dictionary that maps reference keys to reference values. + * + * @param the type of keys + * @param the type of values + * @since 10.0.0 + */ public interface RefToRefDictionary extends Dictionary { + /** + * Creates a new builder for constructing an immutable dictionary. + * + * @param the type of keys + * @param the type of values + * @return a new builder + * @since 10.0.0 + */ static RefToRefDictionaryBuilder builder() { return new RefToRefDictionaryBuilder<>(); } + /** + * Creates a new builder for constructing an immutable dictionary with explicit types. + * + * @param the type of keys + * @param the type of values + * @param keyType the key type (unused, for type inference) + * @param valueType the value type (unused, for type inference) + * @return a new builder + * @since 10.0.0 + */ static RefToRefDictionaryBuilder builder( Class keyType, Class valueType) { return new RefToRefDictionaryBuilder<>(); } + /** + * Creates a builder and adds the first key-value pair. + * + * @param the type of keys + * @param the type of values + * @param key the first key + * @param value the first value + * @return a builder with the entry added + * @since 10.0.0 + */ static RefToRefDictionaryBuilder startWith(K key, V value) { return new RefToRefDictionaryBuilder() .put(key, value); } - + + /** + * Creates a new entry with the specified key and value. + * + * @param the type of the key + * @param the type of the value + * @param key the key + * @param value the value + * @return a new entry + * @since 10.0.0 + */ static RefToRefEntry entry(K key, V value) { return new SimpleRefToRefEntry<>(key, value); } + /** + * Returns an empty immutable reference-to-reference dictionary. + * + * @param the type of keys + * @param the type of values + * @return an empty dictionary + * @since 10.0.0 + */ static RefToRefDictionary empty() { return ImmutableHashBasedRefToRefDictionary.empty(); } + /** + * Creates an immutable dictionary with a single mapping. + * + * @param the type of keys + * @param the type of values + * @param key the key + * @param value the value + * @return an immutable dictionary + * @since 10.0.0 + */ static RefToRefDictionary of(K key, V value) { return ofEntries(entry(key, value)); } + /** + * Creates an immutable dictionary with two mappings. + * + * @param the type of keys + * @param the type of values + * @param k1 the first key + * @param v1 the first value + * @param k2 the second key + * @param v2 the second value + * @return an immutable dictionary + * @since 10.0.0 + */ static RefToRefDictionary of(K k1, V v1, K k2, V v2) { return ofEntries(entry(k1, v1), entry(k2, v2)); } + /** + * Creates an immutable dictionary with three mappings. + * + * @param the type of keys + * @param the type of values + * @param k1 the first key + * @param v1 the first value + * @param k2 the second key + * @param v2 the second value + * @param k3 the third key + * @param v3 the third value + * @return an immutable dictionary + * @since 10.0.0 + */ static RefToRefDictionary of(K k1, V v1, K k2, V v2, K k3, V v3) { return ofEntries(entry(k1, v1), entry(k2, v2), entry(k3, v3)); } + /** + * Creates an immutable dictionary with four mappings. + * + * @param the type of keys + * @param the type of values + * @param k1 the first key + * @param v1 the first value + * @param k2 the second key + * @param v2 the second value + * @param k3 the third key + * @param v3 the third value + * @param k4 the fourth key + * @param v4 the fourth value + * @return an immutable dictionary + * @since 10.0.0 + */ static RefToRefDictionary of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return ofEntries(entry(k1, v1), entry(k2, v2), entry(k3, v3), entry(k4, v4)); } + /** + * Creates an immutable dictionary from entries. + * + * @param the type of keys + * @param the type of values + * @param entries the entries + * @return an immutable dictionary + * @since 10.0.0 + */ @SafeVarargs static RefToRefDictionary ofEntries(RefToRefEntry... entries) { MutableRefToRefDictionary mutable = DictionaryFactory.mutableRefToRefDictionary(); @@ -54,5 +168,11 @@ static RefToRefDictionary ofEntries(RefToRefEntry... entries) return mutable.toReadOnly(); } + /** + * Performs the given action for each key-value pair. + * + * @param consumer the action to perform + * @since 10.0.0 + */ void forEach(BiConsumer consumer); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionaryBuilder.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionaryBuilder.java index 2c269c67..769b8d94 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionaryBuilder.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefDictionaryBuilder.java @@ -4,32 +4,72 @@ import lombok.AccessLevel; import lombok.experimental.FieldDefaults; +/** + * A builder for constructing immutable {@link RefToRefDictionary} instances. + * + * @param the type of keys + * @param the type of values + * @since 10.0.0 + */ @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class RefToRefDictionaryBuilder { MutableRefToRefDictionary elements; + /** + * Creates a new dictionary builder. + * + * @since 10.0.0 + */ public RefToRefDictionaryBuilder() { this.elements = DictionaryFactory.mutableRefToRefDictionary(); } - + + /** + * Adds a key-value mapping to the dictionary being built. + * + * @param key the key + * @param value the value + * @return this builder for method chaining + * @since 10.0.0 + */ public RefToRefDictionaryBuilder put(K key, V value) { this.elements.put(key, value); return this; } + /** + * Adds all mappings from another dictionary to the dictionary being built. + * + * @param other the dictionary to add mappings from + * @return this builder for method chaining + * @since 10.0.0 + */ public RefToRefDictionaryBuilder put(RefToRefDictionary other) { this.elements.putAll(other); return this; } + /** + * Adds all mappings from a map to the dictionary being built. + * + * @param other the map to add mappings from + * @return this builder for method chaining + * @since 10.0.0 + */ public RefToRefDictionaryBuilder put(Map other) { for (Map.Entry entry : other.entrySet()) { elements.put(entry.getKey(), entry.getValue()); } return this; } - + + /** + * Builds and returns an immutable dictionary containing all added mappings. + * + * @return an immutable dictionary + * @since 10.0.0 + */ public RefToRefDictionary build() { return elements.toReadOnly(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefEntry.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefEntry.java index ea14f74e..8f91b47d 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefEntry.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/RefToRefEntry.java @@ -1,6 +1,27 @@ package javasabr.rlib.collections.dictionary; +/** + * An entry that maps a reference key to a reference value. + * + * @param the type of the key + * @param the type of the value + * @since 10.0.0 + */ public interface RefToRefEntry extends RefEntry { + + /** + * Returns the key of this entry. + * + * @return the key + * @since 10.0.0 + */ K key(); + + /** + * Sets the key of this entry. + * + * @param key the new key + * @since 10.0.0 + */ void key(K key); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeIntToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeIntToRefDictionary.java index b80b63e9..dfcdd5ec 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeIntToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeIntToRefDictionary.java @@ -2,7 +2,20 @@ import org.jspecify.annotations.Nullable; +/** + * An unsafe view of an int-to-reference dictionary providing direct access to internal entries. + * + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeIntToRefDictionary> extends IntToRefDictionary { + /** + * Returns the internal entry array. + * + * @return the entry array or null + * @since 10.0.0 + */ @Nullable E[] entries(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeLongToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeLongToRefDictionary.java index bd52c2c3..e3bdec6f 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeLongToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeLongToRefDictionary.java @@ -2,7 +2,20 @@ import org.jspecify.annotations.Nullable; +/** + * An unsafe view of a long-to-reference dictionary providing direct access to internal entries. + * + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeLongToRefDictionary> extends LongToRefDictionary { + /** + * Returns the internal entry array. + * + * @return the entry array or null + * @since 10.0.0 + */ @Nullable E[] entries(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableIntToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableIntToRefDictionary.java index 6a1fecb8..33465829 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableIntToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableIntToRefDictionary.java @@ -1,5 +1,12 @@ package javasabr.rlib.collections.dictionary; +/** + * An unsafe mutable view of an int-to-reference dictionary providing direct access to internal entries. + * + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeMutableIntToRefDictionary> extends MutableIntToRefDictionary, UnsafeIntToRefDictionary { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableLongToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableLongToRefDictionary.java index 1ed37350..a901881e 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableLongToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableLongToRefDictionary.java @@ -1,5 +1,12 @@ package javasabr.rlib.collections.dictionary; +/** + * An unsafe mutable view of a long-to-reference dictionary providing direct access to internal entries. + * + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeMutableLongToRefDictionary> extends MutableLongToRefDictionary, UnsafeLongToRefDictionary { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableRefToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableRefToRefDictionary.java index c4c98de2..7f69eac5 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableRefToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeMutableRefToRefDictionary.java @@ -1,5 +1,13 @@ package javasabr.rlib.collections.dictionary; +/** + * An unsafe mutable view of a reference-to-reference dictionary providing direct access to internal entries. + * + * @param the type of keys + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeMutableRefToRefDictionary> extends MutableRefToRefDictionary, UnsafeRefToRefDictionary { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeRefToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeRefToRefDictionary.java index 97e8bca9..a7834d02 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeRefToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/UnsafeRefToRefDictionary.java @@ -2,8 +2,22 @@ import org.jspecify.annotations.Nullable; +/** + * An unsafe view of a reference-to-reference dictionary providing direct access to internal entries. + * + * @param the type of keys + * @param the type of values + * @param the type of entries + * @since 10.0.0 + */ public interface UnsafeRefToRefDictionary> extends RefToRefDictionary { + /** + * Returns the internal entry array. + * + * @return the entry array or null + * @since 10.0.0 + */ @Nullable E[] entries(); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableOperations.java b/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableOperations.java index ea1b74f1..1733bb1a 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableOperations.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableOperations.java @@ -9,28 +9,136 @@ import javasabr.rlib.functions.TriConsumer; import javasabr.rlib.functions.TriFunction; +/** + * Provides thread-safe operations on a lockable source with automatic lock management. + * + * @param the type of the lockable source + * @since 10.0.0 + */ public interface LockableOperations { + /** + * Executes a function within a read lock and returns the result. + * + * @param the type of the result + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInReadLock(Function function); + /** + * Executes a function within a read lock with one argument and returns the result. + * + * @param the type of the result + * @param the type of the argument + * @param arg1 the argument + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInReadLock(A arg1, BiFunction function); + /** + * Executes a function within a read lock with an int argument and returns the result. + * + * @param the type of the result + * @param arg1 the int argument + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInReadLock(int arg1, ObjIntFunction function); + /** + * Executes a function within a read lock with two arguments and returns the result. + * + * @param the type of the result + * @param the type of the first argument + * @param the type of the second argument + * @param arg1 the first argument + * @param arg2 the second argument + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInReadLock(A arg1, B arg2, TriFunction function); + /** + * Executes a boolean function within a read lock with one argument. + * + * @param the type of the argument + * @param arg1 the argument + * @param function the function to execute + * @return the boolean result + * @since 10.0.0 + */ boolean getBooleanInReadLock(A arg1, BiObjToBooleanFunction function); + /** + * Executes a consumer within a read lock with one argument. + * + * @param the type of the argument + * @param arg1 the argument + * @param function the consumer to execute + * @since 10.0.0 + */ void inReadLock(A arg1, BiConsumer function); + /** + * Executes a function within a write lock with one argument and returns the result. + * + * @param the type of the result + * @param the type of the argument + * @param arg1 the argument + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInWriteLock(A arg1, BiFunction function); + /** + * Executes a function within a write lock with two arguments and returns the result. + * + * @param the type of the result + * @param the type of the first argument + * @param the type of the second argument + * @param arg1 the first argument + * @param arg2 the second argument + * @param function the function to execute + * @return the result of the function + * @since 10.0.0 + */ R getInWriteLock(A arg1, B arg2, TriFunction function); + /** + * Executes a consumer within a write lock. + * + * @param function the consumer to execute + * @since 10.0.0 + */ void inWriteLock(Consumer function); + /** + * Executes a consumer within a write lock with one argument. + * + * @param the type of the argument + * @param arg1 the argument + * @param function the consumer to execute + * @since 10.0.0 + */ void inWriteLock(A arg1, BiConsumer function); + /** + * Executes a consumer within a write lock with two arguments. + * + * @param the type of the first argument + * @param the type of the second argument + * @param arg1 the first argument + * @param arg2 the second argument + * @param function the consumer to execute + * @since 10.0.0 + */ void inWriteLock(A arg1, B arg2, TriConsumer function); } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableSource.java b/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableSource.java index 0d4eb78d..9b01486b 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableSource.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/operation/LockableSource.java @@ -1,13 +1,58 @@ package javasabr.rlib.collections.operation; +/** + * A source that provides locking primitives for thread-safe access. + * + * @since 10.0.0 + */ public interface LockableSource { + /** + * Acquires a read lock and returns a stamp for later unlock. + * + * @return a stamp representing the acquired lock + * @since 10.0.0 + */ long readLock(); + + /** + * Releases a read lock using the provided stamp. + * + * @param stamp the stamp from readLock() + * @since 10.0.0 + */ void readUnlock(long stamp); + + /** + * Attempts an optimistic read, returning a stamp for validation. + * + * @return a stamp for validation + * @since 10.0.0 + */ long tryOptimisticRead(); + /** + * Validates that a lock stamp is still valid. + * + * @param stamp the stamp to validate + * @return true if the stamp is still valid + * @since 10.0.0 + */ boolean validateLock(long stamp); + /** + * Acquires a write lock and returns a stamp for later unlock. + * + * @return a stamp representing the acquired lock + * @since 10.0.0 + */ long writeLock(); + + /** + * Releases a write lock using the provided stamp. + * + * @param stamp the stamp from writeLock() + * @since 10.0.0 + */ void writeUnlock(long stamp); } From c8a26133fcaa285ee24c052b3735e6ddcca9f773 Mon Sep 17 00:00:00 2001 From: javasabr Date: Fri, 6 Feb 2026 20:51:17 +0100 Subject: [PATCH 2/4] update javadoc for network module --- .../rlib/collections/array/LongArray.java | 7 +- .../array/UnsafeMutableIntArray.java | 2 +- .../array/UnsafeMutableLongArray.java | 2 +- .../array/impl/AbstractIntArray.java | 4 +- .../array/impl/AbstractLongArray.java | 4 +- .../array/impl/AbstractMutableIntArray.java | 6 +- .../array/impl/AbstractMutableLongArray.java | 6 +- .../rlib/collections/deque/DequeFactory.java | 4 +- ...zedMutableHashBasedIntToRefDictionary.java | 2 +- .../rlib/collections/deque/DequeTest.java | 6 +- .../rlib/network/BufferAllocator.java | 48 +++++-- .../javasabr/rlib/network/Connection.java | 90 +++++++++--- .../java/javasabr/rlib/network/Network.java | 24 +++- .../javasabr/rlib/network/NetworkConfig.java | 73 ++++++++-- .../javasabr/rlib/network/NetworkCryptor.java | 29 ++-- .../javasabr/rlib/network/NetworkFactory.java | 100 +++++++++++-- .../rlib/network/ServerNetworkConfig.java | 23 ++- .../rlib/network/UnsafeConnection.java | 30 ++++ .../annotation/NetworkPacketDescription.java | 10 +- .../rlib/network/client/ClientNetwork.java | 32 ++++- .../exception/MalformedProtocolException.java | 12 ++ .../network/exception/NetworkException.java | 24 ++++ .../UserDefinedNetworkException.java | 25 ++++ .../rlib/network/impl/AbstractConnection.java | 2 +- .../network/packet/IdBasedNetworkPacket.java | 9 +- .../packet/IdBasedReadableNetworkPacket.java | 9 +- .../packet/IdBasedWritableNetworkPacket.java | 4 + .../network/packet/MarkerNetworkPacket.java | 4 +- .../rlib/network/packet/NetworkPacket.java | 9 +- .../network/packet/NetworkPacketReader.java | 11 +- .../network/packet/NetworkPacketWriter.java | 12 +- .../network/packet/ReadableNetworkPacket.java | 14 +- .../packet/ReusableWritablePacket.java | 40 ++++-- .../network/packet/WritableNetworkPacket.java | 22 ++- .../ReadableNetworkPacketRegistry.java | 80 +++++++++-- .../rlib/network/server/ServerNetwork.java | 26 +++- .../rlib/network/util/NetworkUtils.java | 135 ++++++++++++++---- .../javasabr/rlib/network/util/SslUtils.java | 26 ++++ 38 files changed, 793 insertions(+), 173 deletions(-) diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java index f7aa7467..6757451d 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java @@ -1,4 +1,3 @@ - package javasabr.rlib.collections.array; import java.io.Serializable; @@ -91,12 +90,12 @@ static LongArray of(long... elements) { /** * Creates an immutable copy of the specified long array. * - * @param intArray the array to copy + * @param array the array to copy * @return an immutable copy * @since 10.0.0 */ - static LongArray copyOf(LongArray intArray) { - return new ImmutableLongArray(intArray.toArray()); + static LongArray copyOf(LongArray array) { + return new ImmutableLongArray(array.toArray()); } /** diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java index 4a74dd1c..5e6b750f 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableIntArray.java @@ -33,7 +33,7 @@ public interface UnsafeMutableIntArray extends UnsafeIntArray, MutableIntArray { * @return the removed value * @since 10.0.0 */ - int unsafeRemoveByInex(int index); + int unsafeRemoveByIndex(int index); /** * Sets the value at the specified index without bounds checking. diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java index 9f26bd1c..e4a68aac 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/UnsafeMutableLongArray.java @@ -33,7 +33,7 @@ public interface UnsafeMutableLongArray extends UnsafeLongArray, MutableLongArra * @return the removed value * @since 10.0.0 */ - long unsafeRemoveByInex(int index); + long unsafeRemoveByIndex(int index); /** * Sets the value at the specified index without bounds checking. diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractIntArray.java index e493b9d9..9b69d3de 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractIntArray.java @@ -61,7 +61,7 @@ public boolean contains(int value) { @Override public boolean containsAll(IntArray array) { if (array.isEmpty()) { - return false; + return true; } int[] wrapped = array.asUnsafe().wrapped(); @@ -77,7 +77,7 @@ public boolean containsAll(IntArray array) { @Override public boolean containsAll(int[] array) { if (array.length < 1) { - return false; + return true; } for (int value : array) { if (!contains(value)) { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractLongArray.java index 5b340d94..9005e9ae 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractLongArray.java @@ -61,7 +61,7 @@ public boolean contains(long value) { @Override public boolean containsAll(LongArray array) { if (array.isEmpty()) { - return false; + return true; } long[] wrapped = array.asUnsafe().wrapped(); for (int i = 0, length = array.size(); i < length; i++) { @@ -75,7 +75,7 @@ public boolean containsAll(LongArray array) { @Override public boolean containsAll(long[] array) { if (array.length < 1) { - return false; + return true; } for (long value : array) { if (!contains(value)) { diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableIntArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableIntArray.java index 05b76381..62a69aca 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableIntArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableIntArray.java @@ -66,7 +66,7 @@ public UnsafeMutableIntArray unsafeSet(int index, int value) { @Override public int removeByIndex(int index) { checkIndex(index); - return unsafeRemoveByInex(index); + return unsafeRemoveByIndex(index); } @Override @@ -75,7 +75,7 @@ public boolean remove(int value) { if (index < 0) { return false; } - unsafeRemoveByInex(index); + unsafeRemoveByIndex(index); return true; } @@ -108,7 +108,7 @@ public boolean removeAll(int[] array) { } @Override - public int unsafeRemoveByInex(int index) { + public int unsafeRemoveByIndex(int index) { int numMoved = size() - index - 1; int[] wrapped = wrapped(); int value = wrapped[index]; diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableLongArray.java b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableLongArray.java index 30b53043..960018cf 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableLongArray.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractMutableLongArray.java @@ -66,7 +66,7 @@ public UnsafeMutableLongArray unsafeSet(int index, long value) { @Override public long removeByIndex(int index) { checkIndex(index); - return unsafeRemoveByInex(index); + return unsafeRemoveByIndex(index); } @Override @@ -75,7 +75,7 @@ public boolean remove(long value) { if (index < 0) { return false; } - unsafeRemoveByInex(index); + unsafeRemoveByIndex(index); return true; } @@ -108,7 +108,7 @@ public boolean removeAll(long[] array) { } @Override - public long unsafeRemoveByInex(int index) { + public long unsafeRemoveByIndex(int index) { int numMoved = size() - index - 1; long[] wrapped = wrapped(); long value = wrapped[index]; diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java b/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java index c713797f..114347db 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/deque/DequeFactory.java @@ -33,7 +33,7 @@ public static Deque linkedListBased() { * @return a new array based deque * @since 10.0.0 */ - public static Deque arrayBasedBased(Class type) { + public static Deque arrayBased(Class type) { return new DefaultArrayBasedDeque<>(type); } @@ -46,7 +46,7 @@ public static Deque arrayBasedBased(Class type) { * @return a new array based deque * @since 10.0.0 */ - public static Deque arrayBasedBased(Class type, int capacity) { + public static Deque arrayBased(Class type, int capacity) { return new DefaultArrayBasedDeque<>(type, capacity); } } diff --git a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/gc/optimized/GcOptimizedMutableHashBasedIntToRefDictionary.java b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/gc/optimized/GcOptimizedMutableHashBasedIntToRefDictionary.java index 945f4efd..950e1c74 100644 --- a/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/gc/optimized/GcOptimizedMutableHashBasedIntToRefDictionary.java +++ b/rlib-collections/src/main/java/javasabr/rlib/collections/dictionary/impl/gc/optimized/GcOptimizedMutableHashBasedIntToRefDictionary.java @@ -34,7 +34,7 @@ public GcOptimizedMutableHashBasedIntToRefDictionary(int initCapacity, float loa //noinspection unchecked this.entries = new ReusableLinkedHashIntToRefEntry[initCapacity]; this.threshold = (int) (initCapacity * loadFactor); - this.entryPool = DequeFactory.arrayBasedBased(ReusableLinkedHashIntToRefEntry.class); + this.entryPool = DequeFactory.arrayBased(ReusableLinkedHashIntToRefEntry.class); } @Override diff --git a/rlib-collections/src/test/java/javasabr/rlib/collections/deque/DequeTest.java b/rlib-collections/src/test/java/javasabr/rlib/collections/deque/DequeTest.java index 40310619..01a2c533 100644 --- a/rlib-collections/src/test/java/javasabr/rlib/collections/deque/DequeTest.java +++ b/rlib-collections/src/test/java/javasabr/rlib/collections/deque/DequeTest.java @@ -420,7 +420,7 @@ void shouldCheckContains(Deque deque) { @Test void shouldRebalanceIndexesAddLastRemoveFirst() { // given: - Deque deque = DequeFactory.arrayBasedBased(String.class, 15); + Deque deque = DequeFactory.arrayBased(String.class, 15); Field head = ReflectionUtils.getUnsafeField(deque, "head"); Field tail = ReflectionUtils.getUnsafeField(deque, "tail"); var generator = new AtomicInteger(); @@ -541,7 +541,7 @@ void shouldRebalanceIndexesAddLastRemoveFirst() { @Test void shouldRebalanceIndexesAddFirstRemoveLast() { // given: - Deque deque = DequeFactory.arrayBasedBased(String.class, 15); + Deque deque = DequeFactory.arrayBased(String.class, 15); Field head = ReflectionUtils.getUnsafeField(deque, "head"); Field tail = ReflectionUtils.getUnsafeField(deque, "tail"); var generator = new AtomicInteger(); @@ -707,6 +707,6 @@ void shouldCorrectlyIterate2Deque(Deque deque) { private static Stream generateDeque() { return Stream.of( Arguments.of(DequeFactory.linkedListBased()), - Arguments.of(DequeFactory.arrayBasedBased(String.class, 15))); + Arguments.of(DequeFactory.arrayBased(String.class, 15))); } } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/BufferAllocator.java b/rlib-network/src/main/java/javasabr/rlib/network/BufferAllocator.java index d713c3f1..3d094221 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/BufferAllocator.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/BufferAllocator.java @@ -3,49 +3,79 @@ import java.nio.ByteBuffer; /** - * The interface to implement a buffer allocator for network things. + * The interface to implement a buffer allocator for network operations. * * @author JavaSaBr + * @since 10.0.0 */ public interface BufferAllocator { /** - * Get a new read buffer to use. + * Gets a new read buffer to use for receiving data. + * + * @return a read buffer + * @since 10.0.0 */ ByteBuffer takeReadBuffer(); /** - * Get a new pending buffer to use. + * Gets a new pending buffer to use for storing partial packet data. + * + * @return a pending buffer + * @since 10.0.0 */ ByteBuffer takePendingBuffer(); /** - * Get a new write buffer to use. + * Gets a new write buffer to use for sending data. + * + * @return a write buffer + * @since 10.0.0 */ ByteBuffer takeWriteBuffer(); /** - * Get a new buffer with requested capacity. + * Gets a new buffer with the requested capacity. + * + * @param bufferSize the requested buffer size + * @return a buffer with the specified capacity + * @since 10.0.0 */ ByteBuffer takeBuffer(int bufferSize); /** - * Store an already used read buffer. + * Stores an already used read buffer for reuse. + * + * @param buffer the read buffer to store + * @return this allocator for method chaining + * @since 10.0.0 */ BufferAllocator putReadBuffer(ByteBuffer buffer); /** - * Store an already used pending buffer. + * Stores an already used pending buffer for reuse. + * + * @param buffer the pending buffer to store + * @return this allocator for method chaining + * @since 10.0.0 */ BufferAllocator putPendingBuffer(ByteBuffer buffer); /** - * Store an already used write buffer. + * Stores an already used write buffer for reuse. + * + * @param buffer the write buffer to store + * @return this allocator for method chaining + * @since 10.0.0 */ BufferAllocator putWriteBuffer(ByteBuffer buffer); /** - * Store an already used byte buffer. + * Stores an already used byte buffer for reuse. + * + * @param buffer the buffer to store + * @return this allocator for method chaining + * @since 10.0.0 */ BufferAllocator putBuffer(ByteBuffer buffer); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/Connection.java b/rlib-network/src/main/java/javasabr/rlib/network/Connection.java index 6426ddae..40662e0c 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/Connection.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/Connection.java @@ -7,12 +7,24 @@ import reactor.core.publisher.Flux; /** - * The interface to implement an async connection. + * The interface to implement an asynchronous network connection. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface Connection> { + /** + * Represents a received packet event containing the connection, packet, and validity status. + * + * @param the connection type + * @param the packet type + * @param connection the connection that received the packet + * @param packet the received packet + * @param valid whether the packet is valid + * @since 10.0.0 + */ record ReceivedPacketEvent(C connection, R packet, boolean valid) { @Override public String toString() { @@ -21,54 +33,84 @@ public String toString() { } /** - * Get a remote address of this connection. + * Gets the remote address of this connection. + * + * @return the remote address as a string + * @since 10.0.0 */ String remoteAddress(); /** - * Get a timestamp of last write/read activity. + * Gets the timestamp of last write/read activity. + * + * @return the last activity timestamp in milliseconds + * @since 10.0.0 */ long lastActivity(); /** - * Close this connection if this connection is still opened. + * Closes this connection if it is still open. + * + * @since 10.0.0 */ void close(); /** - * @return true if this connection is already closed. + * Checks if this connection is already closed. + * + * @return true if this connection is closed + * @since 10.0.0 */ boolean closed(); /** - * Send a packet to connection's owner in background. + * Sends a packet to the connection's owner in the background. + * + * @param packet the packet to send + * @since 10.0.0 */ void sendInBackground(WritableNetworkPacket packet); /** - * Send a packet to connection's owner with async feedback of this action. + * Sends a packet to the connection's owner with async feedback. * - * @return the async result with true if the packet was sent or false if sending was failed. + * @param packet the packet to send + * @return a future with true if sent successfully, false otherwise + * @since 10.0.0 */ CompletableFuture sendAsync(WritableNetworkPacket packet); /** - * Register a consumer to handle received valid packets. + * Registers a consumer to handle received valid packets. + * + * @param consumer the consumer to handle valid packets + * @since 10.0.0 */ void onReceiveValidPacket(BiConsumer> consumer); /** - * Register a consumer to handle received invalid packets. + * Registers a consumer to handle received invalid packets. + * + * @param consumer the consumer to handle invalid packets + * @since 10.0.0 */ void onReceiveInvalidPacket(BiConsumer> consumer); /** - * Get a stream of received packet events. + * Gets a stream of received packet events. + * + * @return a reactive stream of packet events + * @since 10.0.0 */ Flux>> receivedEvents(); /** - * Get a stream of received packet events with expected packet type. + * Gets a stream of received packet events filtered by expected packet type. + * + * @param the expected packet type + * @param packetType the packet type class + * @return a reactive stream of packet events + * @since 10.0.0 */ default > Flux> receivedEvents(Class packetType) { return receivedEvents() @@ -77,17 +119,28 @@ default > Flux> rec } /** - * Get a stream of received valid packets. + * Gets a stream of received valid packets. + * + * @return a reactive stream of valid packets + * @since 10.0.0 */ Flux> receivedValidPackets(); /** - * Get a stream of received invalid packets. + * Gets a stream of received invalid packets. + * + * @return a reactive stream of invalid packets + * @since 10.0.0 */ Flux> receivedInvalidPackets(); /** - * Get a stream of received valid packets with expected type. + * Gets a stream of received valid packets filtered by expected type. + * + * @param the expected packet type + * @param packetType the packet type class + * @return a reactive stream of valid packets + * @since 10.0.0 */ default > Flux receivedValidPackets(Class packetType) { return receivedValidPackets() @@ -96,7 +149,12 @@ default > Flux receivedValidPackets(Class< } /** - * Get a stream of received invalid packets with expected type. + * Gets a stream of received invalid packets filtered by expected type. + * + * @param the expected packet type + * @param packetType the packet type class + * @return a reactive stream of invalid packets + * @since 10.0.0 */ default > Flux receivedInvalidPackets(Class packetType) { return receivedInvalidPackets() diff --git a/rlib-network/src/main/java/javasabr/rlib/network/Network.java b/rlib-network/src/main/java/javasabr/rlib/network/Network.java index 3c8b8443..ec91b9e1 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/Network.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/Network.java @@ -5,18 +5,40 @@ /** * The interface to implement an asynchronous network. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface Network> { + /** + * Gets the scheduled executor service for this network. + * + * @return the scheduled executor service + * @since 10.0.0 + */ ScheduledExecutorService scheduledExecutor(); + /** + * Gets the network configuration. + * + * @return the network config + * @since 10.0.0 + */ NetworkConfig config(); + /** + * Executes a task in the network thread. + * + * @param task the task to execute + * @since 10.0.0 + */ void inNetworkThread(Runnable task); /** - * Shutdown this network. + * Shuts down this network and releases all resources. + * + * @since 10.0.0 */ void shutdown(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/NetworkConfig.java b/rlib-network/src/main/java/javasabr/rlib/network/NetworkConfig.java index 9b45cc62..79d94a8c 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/NetworkConfig.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/NetworkConfig.java @@ -7,12 +7,18 @@ import lombok.experimental.Accessors; /** - * The interface to implement a network config. + * The interface to implement a network configuration. * * @author JavaSaBr + * @since 10.0.0 */ public interface NetworkConfig { + /** + * Simple implementation of network configuration using Lombok builder. + * + * @since 10.0.0 + */ @Builder @Getter @Accessors(fluent = true, chain = false) @@ -52,82 +58,123 @@ public String threadGroupName() { }; /** - * Get a thread constructor which should be used to create network threads. + * Gets the thread constructor for creating network threads. + * + * @return the thread constructor + * @since 10.0.0 */ default ThreadConstructor threadConstructor() { return Thread::new; } /** - * Get a priority of network threads. + * Gets the priority of network threads. * - * @return the priority of network threads. + * @return the thread priority + * @since 10.0.0 */ default int threadPriority() { return Thread.NORM_PRIORITY; } /** - * Get a group name of network threads. + * Gets the group name of network threads. + * + * @return the thread group name + * @since 10.0.0 */ default String threadGroupName() { return "NetworkThread"; } /** - * Get a group name of scheduling network threads. + * Gets the group name of scheduled network threads. + * + * @return the scheduled thread group name + * @since 10.0.0 */ default String scheduledThreadGroupName() { return "ScheduledNetworkThread"; } /** - * Get size of buffer with will be used to collect received data from network. + * Gets the size of buffer used to collect received data from network. + * + * @return the read buffer size in bytes + * @since 10.0.0 */ default int readBufferSize() { return 2048; } /** - * Gets size of buffer with pending reading data. Pending buffer allows to construct a packet with bigger data part than - * {@link #readBufferSize()}. It should be at least 2x of {@link #readBufferSize()} + * Gets the size of buffer for pending reading data. The pending buffer allows constructing + * a packet with bigger data part than {@link #readBufferSize()}. + * It should be at least 2x of {@link #readBufferSize()}. + * + * @return the pending buffer size in bytes + * @since 10.0.0 */ default int pendingBufferSize() { return readBufferSize() * 2; } /** - * Gets a size of buffer which will be used for packet serialization. + * Gets the size of buffer used for packet serialization. + * + * @return the write buffer size in bytes + * @since 10.0.0 */ default int writeBufferSize() { return 2048; } /** - * Gets the max size of one single network packet. + * Gets the maximum size of a single network packet. + * + * @return the max packet size in bytes + * @since 10.0.0 */ default int maxPacketSize() { return 5 * 1024 * 1024; } /** - * Gets a timeout for retry read/write operation. + * Gets the timeout for retry read/write operations. + * + * @return the retry delay in milliseconds + * @since 10.0.0 */ default int retryDelayInMs() { return 1000; } /** - * Gets a max allowed empty reads from socket channel before closing a connection. + * Gets the maximum allowed empty reads from socket channel before closing a connection. + * + * @return the max empty reads count + * @since 10.0.0 */ default int maxEmptyReadsBeforeClose() { return 3; } + /** + * Gets the byte order for network data. + * + * @return the byte order + * @since 10.0.0 + */ default ByteOrder byteOrder() { return ByteOrder.BIG_ENDIAN; } + /** + * Checks if direct byte buffers should be used. + * + * @return true if direct buffers should be used + * @since 10.0.0 + */ default boolean useDirectByteBuffer() { return false; } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/NetworkCryptor.java b/rlib-network/src/main/java/javasabr/rlib/network/NetworkCryptor.java index bbd5c2c9..7c68be7f 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/NetworkCryptor.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/NetworkCryptor.java @@ -4,14 +4,17 @@ import org.jspecify.annotations.Nullable; /** - * The interface to implement a network cryptor. + * The interface to implement network data encryption and decryption. * * @author JavaSaBr + * @since 10.0.0 */ public interface NetworkCryptor { /** - * Default NULL implementation of the network crypt. + * Default NULL implementation that performs no encryption or decryption. + * + * @since 10.0.0 */ NetworkCryptor NULL = new NetworkCryptor() { @@ -29,23 +32,25 @@ public ByteBuffer encrypt(ByteBuffer data, int length, ByteBuffer toStore) { }; /** - * Decrypt data. + * Decrypts data from the source buffer. * - * @param data the buffer with data to decrypt. - * @param length the data length. - * @param toStore the buffer to store decrypted data. - * @return the buffer with decrypted data or null if don't need to decrypt anything. + * @param data the buffer with data to decrypt + * @param length the data length + * @param toStore the buffer to store decrypted data + * @return the buffer with decrypted data or null if decryption is not needed + * @since 10.0.0 */ @Nullable ByteBuffer decrypt(ByteBuffer data, int length, ByteBuffer toStore); /** - * Encrypt data. + * Encrypts data from the source buffer. * - * @param data the buffer with data to encrypt. - * @param length the data length. - * @param toStore the buffer to store encrypted data. - * @return the buffer with encrypted data or null if don't need to decrypt encrypt. + * @param data the buffer with data to encrypt + * @param length the data length + * @param toStore the buffer to store encrypted data + * @return the buffer with encrypted data or null if encryption is not needed + * @since 10.0.0 */ @Nullable ByteBuffer encrypt(ByteBuffer data, int length, ByteBuffer toStore); diff --git a/rlib-network/src/main/java/javasabr/rlib/network/NetworkFactory.java b/rlib-network/src/main/java/javasabr/rlib/network/NetworkFactory.java index 187ec72b..0c09b9a5 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/NetworkFactory.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/NetworkFactory.java @@ -16,19 +16,38 @@ import lombok.experimental.UtilityClass; /** - * Class with factory methods to build client/server networks. + * Factory class with methods to build client and server networks. * * @author JavaSaBr + * @since 10.0.0 */ @UtilityClass public final class NetworkFactory { + /** + * Creates a new client network with custom connection factory. + * + * @param the connection type + * @param networkConfig the network configuration + * @param channelToConnection the function to create connections from channels + * @return a new client network + * @since 10.0.0 + */ public static > ClientNetwork clientNetwork( NetworkConfig networkConfig, BiFunction, AsynchronousSocketChannel, C> channelToConnection) { return new DefaultClientNetwork<>(networkConfig, channelToConnection); } + /** + * Creates a new server network with custom connection factory. + * + * @param the connection type + * @param networkConfig the server network configuration + * @param channelToConnection the function to create connections from channels + * @return a new server network + * @since 10.0.0 + */ public static > ServerNetwork serverNetwork( ServerNetworkConfig networkConfig, BiFunction, AsynchronousSocketChannel, C> channelToConnection) { @@ -36,14 +55,21 @@ public static > ServerNetwork serverNetwork( } /** - * Create a string packet based asynchronous client network. + * Creates a string packet based asynchronous client network with default configuration. + * + * @return a new string data client network + * @since 10.0.0 */ public static ClientNetwork stringDataClientNetwork() { return stringDataClientNetwork(NetworkConfig.DEFAULT_CLIENT); } /** - * Create a string packet based asynchronous client network. + * Creates a string packet based asynchronous client network with custom configuration. + * + * @param networkConfig the network configuration + * @return a new string data client network + * @since 10.0.0 */ public static ClientNetwork stringDataClientNetwork( NetworkConfig networkConfig) { @@ -51,7 +77,12 @@ public static ClientNetwork stringDataClientNetwork( } /** - * Create a string packet based asynchronous client network. + * Creates a string packet based asynchronous client network with custom configuration and allocator. + * + * @param networkConfig the network configuration + * @param bufferAllocator the buffer allocator + * @return a new string data client network + * @since 10.0.0 */ public static ClientNetwork stringDataClientNetwork( NetworkConfig networkConfig, @@ -62,7 +93,11 @@ public static ClientNetwork stringDataClientNetwork( } /** - * Create id based packet default asynchronous client network. + * Creates an ID-based packet default asynchronous client network. + * + * @param packetRegistry the packet registry + * @return a new default client network + * @since 10.0.0 */ public static ClientNetwork defaultClientNetwork( ReadableNetworkPacketRegistry, DefaultConnection> packetRegistry) { @@ -73,7 +108,13 @@ public static ClientNetwork defaultClientNetwork( } /** - * Create id based packet default asynchronous client network. + * Creates an ID-based packet default asynchronous client network with custom configuration. + * + * @param networkConfig the network configuration + * @param bufferAllocator the buffer allocator + * @param packetRegistry the packet registry + * @return a new default client network + * @since 10.0.0 */ public static ClientNetwork defaultClientNetwork( NetworkConfig networkConfig, @@ -85,8 +126,13 @@ public static ClientNetwork defaultClientNetwork( } /** - * Create string packet based asynchronous secure client network. + * Creates a string packet based asynchronous secure client network. * + * @param networkConfig the network configuration + * @param bufferAllocator the buffer allocator + * @param sslContext the SSL context + * @return a new SSL string data client network + * @since 10.0.0 */ public static ClientNetwork stringDataSslClientNetwork( NetworkConfig networkConfig, @@ -98,14 +144,21 @@ public static ClientNetwork stringDataSslClientNetwork( } /** - * Create string packet based asynchronous server network. + * Creates a string packet based asynchronous server network with default configuration. + * + * @return a new string data server network + * @since 10.0.0 */ public static ServerNetwork stringDataServerNetwork() { return stringDataServerNetwork(ServerNetworkConfig.DEFAULT_SERVER); } /** - * Create string packet based asynchronous server network. + * Creates a string packet based asynchronous server network with custom configuration. + * + * @param networkConfig the server network configuration + * @return a new string data server network + * @since 10.0.0 */ public static ServerNetwork stringDataServerNetwork( ServerNetworkConfig networkConfig) { @@ -113,7 +166,12 @@ public static ServerNetwork stringDataServerNetwork( } /** - * Create string packet based asynchronous server network. + * Creates a string packet based asynchronous server network with custom configuration and allocator. + * + * @param networkConfig the server network configuration + * @param bufferAllocator the buffer allocator + * @return a new string data server network + * @since 10.0.0 */ public static ServerNetwork stringDataServerNetwork( ServerNetworkConfig networkConfig, @@ -124,7 +182,13 @@ public static ServerNetwork stringDataServerNetwork( } /** - * Create string packet based asynchronous secure server network. + * Creates a string packet based asynchronous secure server network. + * + * @param networkConfig the server network configuration + * @param bufferAllocator the buffer allocator + * @param sslContext the SSL context + * @return a new SSL string data server network + * @since 10.0.0 */ public static ServerNetwork stringDataSslServerNetwork( ServerNetworkConfig networkConfig, @@ -136,7 +200,11 @@ public static ServerNetwork stringDataSslServerNetwork( } /** - * Create id based packet default asynchronous server network. + * Creates an ID-based packet default asynchronous server network. + * + * @param packetRegistry the packet registry + * @return a new default server network + * @since 10.0.0 */ public static ServerNetwork defaultServerNetwork( ReadableNetworkPacketRegistry, DefaultConnection> packetRegistry) { @@ -147,7 +215,13 @@ public static ServerNetwork defaultServerNetwork( } /** - * Create id based packet default asynchronous server network. + * Creates an ID-based packet default asynchronous server network with custom configuration. + * + * @param networkConfig the server network configuration + * @param bufferAllocator the buffer allocator + * @param packetRegistry the packet registry + * @return a new default server network + * @since 10.0.0 */ public static ServerNetwork defaultServerNetwork( ServerNetworkConfig networkConfig, diff --git a/rlib-network/src/main/java/javasabr/rlib/network/ServerNetworkConfig.java b/rlib-network/src/main/java/javasabr/rlib/network/ServerNetworkConfig.java index ab090343..dc21a9cb 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/ServerNetworkConfig.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/ServerNetworkConfig.java @@ -7,12 +7,18 @@ import lombok.experimental.Accessors; /** - * The interface to implement a server network config. + * The interface to implement a server network configuration. * * @author JavaSaBr + * @since 10.0.0 */ public interface ServerNetworkConfig extends NetworkConfig { + /** + * Simple implementation of server network configuration using Lombok builder. + * + * @since 10.0.0 + */ @Builder @Getter @Accessors(fluent = true, chain = false) @@ -63,21 +69,30 @@ public String scheduledThreadGroupName() { }; /** - * Get a minimal size of network thread executor. + * Gets the minimal size of network thread executor. + * + * @return the minimum thread pool size + * @since 10.0.0 */ default int threadGroupMinSize() { return 1; } /** - * Get a maximum size of network thread executor. + * Gets the maximum size of network thread executor. + * + * @return the maximum thread pool size + * @since 10.0.0 */ default int threadGroupMaxSize() { return threadGroupMinSize(); } /** - * Get a size of network scheduled thread executor. + * Gets the size of network scheduled thread executor. + * + * @return the scheduled thread pool size + * @since 10.0.0 */ default int scheduledThreadGroupSize() { return 1; diff --git a/rlib-network/src/main/java/javasabr/rlib/network/UnsafeConnection.java b/rlib-network/src/main/java/javasabr/rlib/network/UnsafeConnection.java index 46f37847..b72051cf 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/UnsafeConnection.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/UnsafeConnection.java @@ -2,13 +2,43 @@ import java.nio.channels.AsynchronousSocketChannel; +/** + * An unsafe connection interface providing direct access to internal network components. + * Use with caution as modifications may affect connection stability. + * + * @param the connection type + * @since 10.0.0 + */ public interface UnsafeConnection> extends Connection { + /** + * Gets the network this connection belongs to. + * + * @return the network + * @since 10.0.0 + */ Network network(); + /** + * Gets the buffer allocator used by this connection. + * + * @return the buffer allocator + * @since 10.0.0 + */ BufferAllocator bufferAllocator(); + /** + * Gets the underlying asynchronous socket channel. + * + * @return the socket channel + * @since 10.0.0 + */ AsynchronousSocketChannel channel(); + /** + * Called when the connection is established. + * + * @since 10.0.0 + */ void onConnected(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/annotation/NetworkPacketDescription.java b/rlib-network/src/main/java/javasabr/rlib/network/annotation/NetworkPacketDescription.java index 7c75856e..c2627d54 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/annotation/NetworkPacketDescription.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/annotation/NetworkPacketDescription.java @@ -7,13 +7,21 @@ import java.lang.annotation.Target; /** - * The annotation to describe a network packet. + * Annotation to describe a network packet with its unique identifier. * * @author JavaSaBr + * @since 10.0.0 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface NetworkPacketDescription { + + /** + * The unique identifier for this packet type. + * + * @return the packet ID + * @since 10.0.0 + */ int id(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/client/ClientNetwork.java b/rlib-network/src/main/java/javasabr/rlib/network/client/ClientNetwork.java index 586c43ef..f04c52ce 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/client/ClientNetwork.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/client/ClientNetwork.java @@ -9,35 +9,55 @@ import reactor.core.publisher.Mono; /** - * Interface to implement a client network. + * Interface to implement a client network for connecting to servers. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface ClientNetwork> extends Network { /** - * Connect to a server by the address. + * Connects to a server synchronously by the address. + * + * @param serverAddress the server address + * @return the established connection + * @since 10.0.0 */ C connect(InetSocketAddress serverAddress); /** - * Connect to a server by the address. + * Connects to a server asynchronously by the address. + * + * @param serverAddress the server address + * @return a future containing the established connection + * @since 10.0.0 */ CompletableFuture connectAsync(InetSocketAddress serverAddress); /** - * Connect to a server by the address. + * Connects to a server reactively by the address. + * + * @param serverAddress the server address + * @return a mono containing the established connection + * @since 10.0.0 */ Mono connectReactive(InetSocketAddress serverAddress); /** - * Get a current connection to a server or null. + * Gets the current connection to a server. + * + * @return the current connection or null if not connected + * @since 10.0.0 */ @Nullable C currentConnection(); /** - * Get a current connection to a server or empty. + * Gets the current connection to a server as an optional. + * + * @return an optional containing the current connection + * @since 10.0.0 */ Optional currentConnectionOptional(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/exception/MalformedProtocolException.java b/rlib-network/src/main/java/javasabr/rlib/network/exception/MalformedProtocolException.java index 4441f294..1269f438 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/exception/MalformedProtocolException.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/exception/MalformedProtocolException.java @@ -1,6 +1,18 @@ package javasabr.rlib.network.exception; +/** + * Exception thrown when a network protocol is malformed or invalid. + * + * @since 10.0.0 + */ public class MalformedProtocolException extends NetworkException { + + /** + * Constructs a new malformed protocol exception with the specified message. + * + * @param message the error message + * @since 10.0.0 + */ public MalformedProtocolException(String message) { super(message); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/exception/NetworkException.java b/rlib-network/src/main/java/javasabr/rlib/network/exception/NetworkException.java index 0fe81c62..41edbd14 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/exception/NetworkException.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/exception/NetworkException.java @@ -1,15 +1,39 @@ package javasabr.rlib.network.exception; +/** + * Base exception class for network-related errors. + * + * @since 10.0.0 + */ public class NetworkException extends RuntimeException { + /** + * Constructs a new network exception with the specified message. + * + * @param message the error message + * @since 10.0.0 + */ protected NetworkException(String message) { super(message); } + /** + * Constructs a new network exception with the specified message and cause. + * + * @param message the error message + * @param cause the cause of the exception + * @since 10.0.0 + */ protected NetworkException(String message, Throwable cause) { super(message, cause); } + /** + * Constructs a new network exception with the specified cause. + * + * @param cause the cause of the exception + * @since 10.0.0 + */ protected NetworkException(Throwable cause) { super(cause); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/exception/UserDefinedNetworkException.java b/rlib-network/src/main/java/javasabr/rlib/network/exception/UserDefinedNetworkException.java index 47932ef1..c19014cd 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/exception/UserDefinedNetworkException.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/exception/UserDefinedNetworkException.java @@ -1,14 +1,39 @@ package javasabr.rlib.network.exception; +/** + * Base exception class for user-defined network errors. + * + * @since 10.0.0 + */ public class UserDefinedNetworkException extends NetworkException { + + /** + * Constructs a new user-defined network exception with the specified message. + * + * @param message the error message + * @since 10.0.0 + */ protected UserDefinedNetworkException(String message) { super(message); } + /** + * Constructs a new user-defined network exception with the specified message and cause. + * + * @param message the error message + * @param cause the cause of the exception + * @since 10.0.0 + */ protected UserDefinedNetworkException(String message, Throwable cause) { super(message, cause); } + /** + * Constructs a new user-defined network exception with the specified cause. + * + * @param cause the cause of the exception + * @since 10.0.0 + */ protected UserDefinedNetworkException(Throwable cause) { super(cause); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/impl/AbstractConnection.java b/rlib-network/src/main/java/javasabr/rlib/network/impl/AbstractConnection.java index 520e4f14..7e8e04c6 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/impl/AbstractConnection.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/impl/AbstractConnection.java @@ -79,7 +79,7 @@ public AbstractConnection( this.maxPacketsByRead = maxPacketsByRead; this.lock = new StampedLock(); this.channel = channel; - this.pendingPackets = DequeFactory.arrayBasedBased(WritableNetworkPacket.class); + this.pendingPackets = DequeFactory.arrayBased(WritableNetworkPacket.class); this.network = network; this.closed = new AtomicBoolean(false); this.validPacketSubscribers = ArrayFactory.copyOnModifyArray(BiConsumer.class); diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedNetworkPacket.java index 9f75c928..4af676ba 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedNetworkPacket.java @@ -4,14 +4,19 @@ import javasabr.rlib.network.annotation.NetworkPacketDescription; /** + * Interface for network packets that have a unique identifier. + * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface IdBasedNetworkPacket> extends NetworkPacket { /** - * Get id of this packet. + * Gets the ID of this packet from its {@link NetworkPacketDescription} annotation. * - * @return the packet type's id. + * @return the packet type's ID + * @since 10.0.0 */ default int packetId() { return getClass() diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedReadableNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedReadableNetworkPacket.java index 9664624d..00eba30b 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedReadableNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedReadableNetworkPacket.java @@ -4,13 +4,20 @@ import javasabr.rlib.network.Connection; /** + * Interface for ID-based readable network packets that can create new instances. + * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface IdBasedReadableNetworkPacket> extends ReadableNetworkPacket, IdBasedNetworkPacket { /** - * Create a new instance of this type. + * Creates a new instance of this packet type. + * + * @return a new instance + * @since 10.0.0 */ default IdBasedReadableNetworkPacket newInstance() { return ClassUtils.newInstance(getClass()); diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedWritableNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedWritableNetworkPacket.java index d06c6bee..7c1b87db 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedWritableNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/IdBasedWritableNetworkPacket.java @@ -3,7 +3,11 @@ import javasabr.rlib.network.Connection; /** + * Interface for ID-based writable network packets. + * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface IdBasedWritableNetworkPacket> extends WritableNetworkPacket, IdBasedNetworkPacket { diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/MarkerNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/MarkerNetworkPacket.java index fe98047c..bbfe0b81 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/MarkerNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/MarkerNetworkPacket.java @@ -1,6 +1,8 @@ package javasabr.rlib.network.packet; /** - * Interface to mark that some specific packet doesn't have any data. + * Marker interface indicating that a packet has no data payload. + * + * @since 10.0.0 */ public interface MarkerNetworkPacket {} diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacket.java index 037500e7..2bf5fdcf 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacket.java @@ -3,14 +3,19 @@ import javasabr.rlib.network.Connection; /** - * The interface to implement a network packet. + * Base interface for all network packets. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface NetworkPacket> { /** - * @return the packet's name. + * Gets the name of this packet. + * + * @return the packet name + * @since 10.0.0 */ String name(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketReader.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketReader.java index 6111320f..1284fffa 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketReader.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketReader.java @@ -1,17 +1,24 @@ package javasabr.rlib.network.packet; /** + * Interface for reading network packets from a connection. + * * @author JavaSaBr + * @since 10.0.0 */ public interface NetworkPacketReader { /** - * Activate a process of receiving packets. + * Activates the process of receiving packets. + * + * @since 10.0.0 */ void startRead(); /** - * Close all used resources. + * Closes all used resources. + * + * @since 10.0.0 */ void close(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketWriter.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketWriter.java index a2c1d985..3c05224b 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketWriter.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/NetworkPacketWriter.java @@ -1,17 +1,25 @@ package javasabr.rlib.network.packet; /** + * Interface for writing network packets to a connection. + * * @author JavaSaBr + * @since 10.0.0 */ public interface NetworkPacketWriter { /** - * @return true if the writer starting writing new data to channel. + * Tries to send the next packet in the queue. + * + * @return true if the writer started writing new data to the channel + * @since 10.0.0 */ boolean tryToSendNextPacket(); /** - * Close all used resources. + * Closes all used resources. + * + * @since 10.0.0 */ void close(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/ReadableNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/ReadableNetworkPacket.java index 72407a02..8aeaa4eb 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/ReadableNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/ReadableNetworkPacket.java @@ -4,18 +4,22 @@ import javasabr.rlib.network.Connection; /** - * The interface to implement a readable network packet. + * Interface for network packets that can be read from a byte buffer. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface ReadableNetworkPacket> extends NetworkPacket { /** - * Read packet's data from byte buffer. + * Reads packet data from the byte buffer. * - * @param buffer the buffer with received data. - * @param remainingDataLength the expected remaining data length. - * @return true if reading was success. + * @param connection the connection this packet was received from + * @param buffer the buffer with received data + * @param remainingDataLength the expected remaining data length + * @return true if reading was successful + * @since 10.0.0 */ boolean read(C connection, ByteBuffer buffer, int remainingDataLength); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/ReusableWritablePacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/ReusableWritablePacket.java index ccbd5262..708d5e0a 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/ReusableWritablePacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/ReusableWritablePacket.java @@ -5,53 +5,71 @@ import javasabr.rlib.reusable.pool.Pool; /** - * The interface to implement a reusable writable packet. + * Interface for reusable writable packets that can be pooled. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface ReusableWritablePacket> extends WritableNetworkPacket, Reusable { /** - * Handle completion of packet sending. + * Handles completion of packet sending. + * + * @since 10.0.0 */ void complete(); /** - * Force complete this packet. + * Forces completion of this packet. + * + * @since 10.0.0 */ void forceComplete(); /** - * Decrease sending count. + * Decreases the sending count by one. + * + * @since 10.0.0 */ void decreaseSends(); /** - * Decrease sending count. + * Decreases the sending count by the specified amount. * - * @param count the count. + * @param count the count to decrease by + * @since 10.0.0 */ void decreaseSends(int count); /** - * Increase sending count. + * Increases the sending count by one. + * + * @since 10.0.0 */ void increaseSends(); /** - * Increase sending count. + * Increases the sending count by the specified amount. * - * @param count the count. + * @param count the count to increase by + * @since 10.0.0 */ void increaseSends(int count); /** - * Set the pool. + * Sets the pool to store this packet when done. * - * @param pool the pool to store used packet. + * @param pool the pool to store used packets + * @since 10.0.0 */ void setPool(Pool> pool); + /** + * Called when this packet is added to the send queue. + * + * @since 10.0.0 + */ default void notifyAddedToSend() { increaseSends(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/WritableNetworkPacket.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/WritableNetworkPacket.java index 1a9d149e..05201689 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/WritableNetworkPacket.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/WritableNetworkPacket.java @@ -4,23 +4,37 @@ import javasabr.rlib.network.Connection; /** - * Interface to implement a writable packet. + * Interface for network packets that can be written to a byte buffer. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface WritableNetworkPacket> extends NetworkPacket { + /** + * Constant indicating unknown expected packet length. + * + * @since 10.0.0 + */ int UNKNOWN_EXPECTED_BYTES = -1; /** - * Write this packet to the buffer. + * Writes this packet to the buffer. * - * @return true if writing was successful. + * @param connection the connection to write to + * @param buffer the buffer to write to + * @return true if writing was successful + * @since 10.0.0 */ boolean write(C connection, ByteBuffer buffer); /** - * @return expected data length of this packet or {@code UNKNOWN_EXPECTED_BYTES}. + * Gets the expected data length of this packet. + * + * @param connection the connection + * @return the expected length or {@link #UNKNOWN_EXPECTED_BYTES} + * @since 10.0.0 */ default int expectedLength(C connection) { return UNKNOWN_EXPECTED_BYTES; diff --git a/rlib-network/src/main/java/javasabr/rlib/network/packet/registry/ReadableNetworkPacketRegistry.java b/rlib-network/src/main/java/javasabr/rlib/network/packet/registry/ReadableNetworkPacketRegistry.java index ddffaf70..9f7ed998 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/packet/registry/ReadableNetworkPacketRegistry.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/packet/registry/ReadableNetworkPacketRegistry.java @@ -11,21 +11,33 @@ import javasabr.rlib.network.packet.registry.impl.IdBasedReadableNetworkPacketRegistry; /** - * The interface to implement a registry of readable packets. + * Interface to implement a registry of readable network packets. * + * @param the readable packet type + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface ReadableNetworkPacketRegistry, C extends Connection> { /** * Creates a new empty readable packet registry. + * + * @return an empty registry + * @since 10.0.0 */ static ReadableNetworkPacketRegistry empty() { return new IdBasedReadableNetworkPacketRegistry<>(IdBasedReadableNetworkPacket.class); } /** - * Create a new empty readable packet registry. + * Creates a new empty readable packet registry with the specified type. + * + * @param the readable packet type + * @param the connection type + * @param type the packet type class + * @return an empty registry + * @since 10.0.0 */ static < R extends IdBasedReadableNetworkPacket, @@ -34,7 +46,13 @@ C extends Connection> ReadableNetworkPacketRegistry empty(Class type } /** - * Creates a new class path scanning based readable packet registry. + * Creates a new classpath scanning based readable packet registry. + * + * @param the readable packet type + * @param the connection type + * @param baseType the base packet type class + * @return a registry populated with discovered packets + * @since 10.0.0 */ static < R extends IdBasedReadableNetworkPacket, @@ -46,7 +64,14 @@ C extends Connection> ReadableNetworkPacketRegistry classPathBased(Clas } /** - * Creates a new class path scanning based readable packet registry by scanning the main class. + * Creates a new classpath scanning based readable packet registry by scanning the main class. + * + * @param the readable packet type + * @param the connection type + * @param baseType the base packet type class + * @param mainClass the main class to scan from + * @return a registry populated with discovered packets + * @since 10.0.0 */ static < R extends IdBasedReadableNetworkPacket, @@ -60,7 +85,14 @@ C extends Connection> ReadableNetworkPacketRegistry classPathBased( } /** - * Creates a new class path scanning based readable packet registry. + * Creates a new readable packet registry from a classpath scanner. + * + * @param the readable packet type + * @param the connection type + * @param baseType the base packet type class + * @param scanner the classpath scanner + * @return a registry populated with discovered packets + * @since 10.0.0 */ static < R extends IdBasedReadableNetworkPacket, @@ -78,7 +110,14 @@ C extends Connection> ReadableNetworkPacketRegistry scannerBased( } /** - * Creates a new readable packet registry. + * Creates a new readable packet registry from varargs classes. + * + * @param the readable packet type + * @param the connection type + * @param type the packet type class + * @param classes the packet classes to register + * @return a registry with registered packets + * @since 10.0.0 */ @SafeVarargs static < @@ -90,6 +129,17 @@ C extends Connection> ReadableNetworkPacketRegistry of( .register(classes, classes.length); } + /** + * Creates a new readable packet registry from varargs classes with explicit connection type. + * + * @param the readable packet type + * @param the connection type + * @param type the packet type class + * @param connectionType the connection type class + * @param classes the packet classes to register + * @return a registry with registered packets + * @since 10.0.0 + */ @SafeVarargs static < R extends IdBasedReadableNetworkPacket, @@ -102,7 +152,14 @@ C extends Connection> ReadableNetworkPacketRegistry of( } /** - * Creates a new readable packet registry. + * Creates a new readable packet registry from an array of classes. + * + * @param the readable packet type + * @param the connection type + * @param type the packet type class + * @param classes the array of packet classes to register + * @return a registry with registered packets + * @since 10.0.0 */ static < R extends IdBasedReadableNetworkPacket, @@ -114,11 +171,12 @@ C extends Connection> ReadableNetworkPacketRegistry of( } /** - * Resolve a network packet prototype based on packet id. + * Resolves a network packet prototype based on packet ID. * - * @param id the network packet id. - * @return the resolve prototype. - * @throws IllegalArgumentException if can't resolve a prototype by the id. + * @param id the network packet ID + * @return the resolved prototype + * @throws IllegalArgumentException if no prototype found for the ID + * @since 10.0.0 */ R resolvePrototypeById(int id); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/server/ServerNetwork.java b/rlib-network/src/main/java/javasabr/rlib/network/server/ServerNetwork.java index 158e672e..f3404e3b 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/server/ServerNetwork.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/server/ServerNetwork.java @@ -7,29 +7,45 @@ import reactor.core.publisher.Flux; /** - * The interface to implement a server network. + * The interface to implement a server network for accepting client connections. * + * @param the connection type * @author JavaSaBr + * @since 10.0.0 */ public interface ServerNetwork> extends Network { /** - * Start a server using any available address. + * Starts the server using any available address. + * + * @return the bound server address + * @since 10.0.0 */ InetSocketAddress start(); /** - * Start a server by the address. + * Starts the server on the specified address. + * + * @param the server network type + * @param serverAddress the address to bind to + * @return this server network + * @since 10.0.0 */ > S start(InetSocketAddress serverAddress); /** - * Register a consumer of new connections. + * Registers a consumer to handle new connections. + * + * @param consumer the consumer to handle accepted connections + * @since 10.0.0 */ void onAccept(Consumer consumer); /** - * Get a stream of new accepted connections. + * Gets a reactive stream of new accepted connections. + * + * @return a flux of accepted connections + * @since 10.0.0 */ Flux accepted(); } diff --git a/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java b/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java index 71e21c6c..fb4e6fff 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java @@ -22,12 +22,30 @@ import org.jspecify.annotations.Nullable; /** + * Utility class for network operations including SSL context creation, + * hex dump generation, and buffer manipulation. +/** + * Utility class for network operations including SSL context creation, + * hex dump generation, and buffer manipulation. + * * @author JavaSaBr + * @since 10.0.0 */ public class NetworkUtils { + /** + * An empty byte buffer constant. + * + * @since 10.0.0 + */ public static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0); + /** + * A trust manager that accepts all certificates without validation. + * Use with caution in production environments. + * + * @since 10.0.0 + */ public static class AllTrustManager implements X509TrustManager { public static final X509Certificate[] EMPTY_CERTS = new X509Certificate[0]; @@ -41,16 +59,43 @@ public void checkClientTrusted(X509Certificate[] certificates, String arg1) {} public void checkServerTrusted(X509Certificate[] certificates, String arg1) {} } + /** + * Gets the remote address from an asynchronous socket channel. + * + * @param socketChannel the socket channel + * @return the remote socket address + * @since 10.0.0 + */ public static SocketAddress getRemoteAddress(AsynchronousSocketChannel socketChannel) { return Utils.uncheckedGet(socketChannel, AsynchronousSocketChannel::getRemoteAddress); } + /** + * Creates an SSL context from a PKCS12 key store. + * + * @param keyStoreData the key store input stream + * @param keyStorePassword the key store password + * @return the configured SSL context + * @since 10.0.0 + */ public static SSLContext createSslContext( InputStream keyStoreData, String keyStorePassword) { return createSslContext("PKCS12", keyStoreData, keyStorePassword, null, null, null); } + /** + * Creates an SSL context with custom key store and optional trust store. + * + * @param keyStoreType the key store type (e.g., "PKCS12", "JKS") + * @param keyStoreData the key store input stream + * @param keyStorePassword the key store password + * @param trustStoreType the trust store type or null + * @param trustStoreData the trust store input stream or null + * @param trustStorePassword the trust store password or null + * @return the configured SSL context + * @since 10.0.0 + */ public static SSLContext createSslContext( String keyStoreType, InputStream keyStoreData, @@ -92,6 +137,13 @@ public static SSLContext createSslContext( } } + /** + * Creates an SSL context that trusts all certificates. + * WARNING: Use only for testing or development, not in production. + * + * @return an SSL context that trusts all certificates + * @since 10.0.0 + */ public static SSLContext createAllTrustedClientSslContext() { try { var sslContext = SSLContext.getInstance("TLSv1.2"); @@ -103,11 +155,11 @@ public static SSLContext createAllTrustedClientSslContext() { } /** - * Prepare a string like 'HEX DUMP' by passed byte buffer. + * Prepares a hex dump string from a byte buffer. * - * @param buffer the byte buffer. - * @return the hex dump string. - * @since 9.9.0 + * @param buffer the byte buffer + * @return the hex dump string + * @since 10.0.0 */ public static String hexDump(ByteBuffer buffer) { if (!buffer.hasRemaining()) { @@ -118,12 +170,12 @@ public static String hexDump(ByteBuffer buffer) { } /** - * Prepare a string like 'HEX DUMP' by passed buffer and ssl engine result. + * Prepares a hex dump string from a buffer and SSL engine result. * - * @param buffer the byte buffer. - * @param result the ssl engine result. - * @return the hex dump string. - * @since 9.9.0 + * @param buffer the byte buffer + * @param result the SSL engine result + * @return the hex dump string + * @since 10.0.0 */ public static String hexDump(ByteBuffer buffer, SSLEngineResult result) { if (result.bytesProduced() < 1) { @@ -134,23 +186,25 @@ public static String hexDump(ByteBuffer buffer, SSLEngineResult result) { } /** - * Prepare a string like 'HEX DUMP' by passed array. + * Prepares a hex dump string from a byte array. * - * @param array the bytes array. - * @param length the length. - * @return the hex dump string. + * @param array the byte array + * @param length the length + * @return the hex dump string + * @since 10.0.0 */ public static String hexDump(byte[] array, int length) { return hexDump(array, 0, length); } /** - * Prepare a string like 'HEX DUMP' by passed array. + * Prepares a hex dump string from a byte array with offset. * - * @param array the byte array. - * @param offset the offset. - * @param length the length. - * @return the hex dump string. + * @param array the byte array + * @param offset the offset + * @param length the length + * @return the hex dump string + * @since 10.0.0 */ public static String hexDump(byte[] array, int offset, int length) { @@ -238,21 +292,23 @@ private static StringBuilder hexDigit(StringBuilder builder, byte value) { } /** - * Check a network port to know is available it or not to open a new socket. + * Checks if a network port is available to open a new socket. * - * @param port the port. - * @return true if the port is available. + * @param port the port + * @return true if the port is available + * @since 10.0.0 */ public static boolean isPortAvailable(int port) { return isPortAvailable("*", port); } /** - * Check a network port to know is available it or not to open a new socket. + * Checks if a network port is available on the specified host. * - * @param host the host. - * @param port the port. - * @return true if the port is available. + * @param host the host ("*" for all interfaces) + * @param port the port + * @return true if the port is available + * @since 10.0.0 */ public static boolean isPortAvailable(String host, int port) { try (var ignored = "*".equals(host) @@ -265,10 +321,11 @@ public static boolean isPortAvailable(String host, int port) { } /** - * Get a nearest available network port from a start port. + * Gets the nearest available network port starting from the specified port. * - * @param port the start port. - * @return the nearest available network port or -1. + * @param port the start port + * @return the nearest available network port or -1 if none found + * @since 10.0.0 */ public static int getAvailablePort(int port) { return IntStream @@ -278,7 +335,15 @@ public static int getAvailablePort(int port) { .orElse(-1); } - + /** + * Increases the buffer size and copies existing data to the new buffer. + * + * @param current the current buffer + * @param allocator the buffer allocator + * @param newSize the new buffer size + * @return the new larger buffer with copied data + * @since 10.0.0 + */ public static ByteBuffer increaseBuffer(ByteBuffer current, BufferAllocator allocator, int newSize) { var newBuffer = allocator.takeBuffer(newSize); @@ -291,10 +356,22 @@ public static ByteBuffer increaseBuffer(ByteBuffer current, BufferAllocator allo return newBuffer; } + /** + * Clears a network buffer and sets its limit to 0. + * + * @param networkBuffer the buffer to clean + * @since 10.0.0 + */ public static void cleanNetworkBuffer(ByteBuffer networkBuffer) { networkBuffer.clear().limit(0); } + /** + * Compacts a network buffer if there is data at position > 0. + * + * @param networkBuffer the buffer to compact + * @since 10.0.0 + */ public static void compactNetworkBufferIfNeed(ByteBuffer networkBuffer) { if (networkBuffer.position() > 0) { networkBuffer.compact().limit(networkBuffer.position()); diff --git a/rlib-network/src/main/java/javasabr/rlib/network/util/SslUtils.java b/rlib-network/src/main/java/javasabr/rlib/network/util/SslUtils.java index 45917c19..5574353e 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/util/SslUtils.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/util/SslUtils.java @@ -4,17 +4,43 @@ import javax.net.ssl.SSLEngineResult.HandshakeStatus; import lombok.experimental.UtilityClass; +/** + * Utility class for SSL/TLS operations. + * + * @since 10.0.0 + */ @UtilityClass public class SslUtils { + /** + * Checks if the SSL handshake is complete and ready for encryption. + * + * @param status the handshake status + * @return true if ready to encrypt/decrypt + * @since 10.0.0 + */ public static boolean isReadyToCrypt(HandshakeStatus status) { return status == HandshakeStatus.FINISHED || status == HandshakeStatus.NOT_HANDSHAKING; } + /** + * Checks if the SSL handshake needs further processing. + * + * @param status the handshake status + * @return true if processing is needed + * @since 10.0.0 + */ public static boolean needToProcess(HandshakeStatus status) { return status != HandshakeStatus.FINISHED && status != HandshakeStatus.NOT_HANDSHAKING; } + /** + * Executes all delegated SSL tasks and returns the new handshake status. + * + * @param engine the SSL engine + * @return the handshake status after executing tasks + * @since 10.0.0 + */ public static HandshakeStatus executeSslTasks(SSLEngine engine) { for (Runnable task = engine.getDelegatedTask(); task != null; task = engine.getDelegatedTask()) { task.run(); From 45765205b4b185638bb634af1cbac273a6249849 Mon Sep 17 00:00:00 2001 From: javasabr Date: Fri, 6 Feb 2026 20:54:36 +0100 Subject: [PATCH 3/4] add new skill --- .github/skills/generate-javadoc.md | 122 +++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 .github/skills/generate-javadoc.md diff --git a/.github/skills/generate-javadoc.md b/.github/skills/generate-javadoc.md new file mode 100644 index 00000000..31d8dd93 --- /dev/null +++ b/.github/skills/generate-javadoc.md @@ -0,0 +1,122 @@ +# Skill: Generate Javadoc + +## Purpose +Generate comprehensive Javadocs for public API interfaces and classes in a specified module, following consistent documentation standards. + +## Trigger +When the user asks to: +- Generate javadocs for a module +- Add documentation to public APIs +- Document interfaces/classes with `@since` tags + +## Requirements + +### What to Document +1. **Public API interfaces and classes only** - files in main source packages +2. **Skip implementation classes** - files in `impl/` packages are NOT documented +3. **Skip test classes** - files in `src/test/` are NOT documented + +### Javadoc Standards +Each documented element must include: + +1. **Class/Interface level:** + - Short description of purpose + - `@param` for type parameters (if generic) + - `@author JavaSaBr` + - `@since 10.0.0` + +2. **Method level:** + - Short description for ALL methods + - `@param` and `@return` only for **non-trivial** methods + - `@since 10.0.0` + - `@throws` only when explicitly thrown + +3. **Constants/Fields:** + - Short description + - `@since 10.0.0` + +### Example Format + +```java +/** + * An immutable array interface that provides type-safe, indexed access to elements. + * + * @param the type of elements in this array + * @author JavaSaBr + * @since 10.0.0 + */ +public interface Array extends Iterable { + + /** + * Creates an empty immutable array of the specified type. + * + * @param the type of elements + * @param type the component type of the array + * @return an empty immutable array + * @since 10.0.0 + */ + static Array empty(Class type) { + // ... + } + + /** + * Returns the number of elements in this array. + * + * @return the number of elements + * @since 10.0.0 + */ + int size(); + + /** + * Returns the element at the specified index. + * + * @param index the index of the element to return + * @return the element at the specified index + * @throws IndexOutOfBoundsException if the index is out of range + * @since 10.0.0 + */ + E get(int index); +} +``` + +## Execution Steps + +1. **Analyze module structure:** + ```bash + ls -la /src/main/java// + ``` + +2. **Identify public API files:** + - List all `.java` files in main packages + - Exclude `impl/` subdirectories + - Exclude `package-info.java` (optional to document) + +3. **Read each file** to understand existing documentation state + +4. **Add Javadocs** using `replace_string_in_file` tool: + - Add class-level Javadoc with description, `@author`, `@since` + - Add method-level Javadocs with description and `@since` + - Add `@param`/`@return` only for non-trivial methods + +5. **Validate changes:** + ```bash + ./gradlew ::compileJava + ``` + +6. **Run full build:** + ```bash + ./gradlew ::build + ``` + +7. **Update summary file** at `./summary.md` with documented files + +## Output +- All public API files documented with proper Javadocs +- Build passes successfully +- Summary file updated with documentation status + +## Notes +- Use existing code style from the project +- Preserve existing Javadocs that are already complete (just add `@since` if missing) +- Group related edits to minimize tool calls +- Check for errors after editing each file From f0cf8291bd8a6d808c113d666578ee83cb532e2a Mon Sep 17 00:00:00 2001 From: javasabr Date: Fri, 6 Feb 2026 20:56:11 +0100 Subject: [PATCH 4/4] fix javadoc --- .../src/main/java/javasabr/rlib/network/util/NetworkUtils.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java b/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java index fb4e6fff..b980883f 100644 --- a/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java +++ b/rlib-network/src/main/java/javasabr/rlib/network/util/NetworkUtils.java @@ -21,9 +21,6 @@ import javax.net.ssl.X509TrustManager; import org.jspecify.annotations.Nullable; -/** - * Utility class for network operations including SSL context creation, - * hex dump generation, and buffer manipulation. /** * Utility class for network operations including SSL context creation, * hex dump generation, and buffer manipulation.