Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,82 @@
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;

/**
* A builder for constructing immutable {@link Array} instances.
*
* @param <E> the type of elements in the array being built
* @since 10.0.0
*/
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public final class ArrayBuilder<E> {

MutableArray<E> 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<? super E> 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<E> 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<E> 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<E> add(Collection<E> 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<E> add(Array<E> 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<E> build() {
return Array.copyOf(elements);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -96,10 +101,26 @@ public Set<Characteristics> characteristics() {
};
}

/**
* Returns a collector that accumulates elements into a mutable array.
*
* @param <T> 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 <T> Collector<T, MutableArray<T>, MutableArray<T>> toMutableArray(Class<? super T> type) {
return mutableCollector(type, ArrayFactory::mutableArray);
}

/**
* Returns a collector that accumulates elements into an immutable array.
*
* @param <T> 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 <T> Collector<T, MutableArray<T>, Array<T>> toArray(Class<? super T> type) {
return collector(type, ArrayFactory::mutableArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <E> the type of elements
* @param type the component type of the array
* @return a new mutable array
* @since 10.0.0
*/
public static <E> MutableArray<E> mutableArray(Class<? super E> 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 <E> 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 <E> MutableArray<E> mutableArray(Class<? super E> type, int capacity) {
return new DefaultMutableArray<>(ClassUtils.unsafeCast(type), capacity);
}


/**
* Creates a new copy-on-modify array of the specified type.
*
* @param <E> 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 <E> MutableArray<E> copyOnModifyArray(Class<? super E> type) {
return new CopyOnWriteMutableArray<>(type);
}

/**
* Creates a new thread-safe array backed by a stamped lock.
*
* @param <E> the type of elements
* @param type the component type of the array
* @return a new lockable array
* @since 10.0.0
*/
public static <E> LockableArray<E> stampedLockBasedArray(Class<? super E> type) {
return new StampedLockBasedArray<>(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <E> the type of elements in the array
* @since 10.0.0
*/
public interface ArrayIterationFunctions<E> {

/**
* Returns iteration functions with reversed argument order.
*
* @return reversed argument iteration functions
* @since 10.0.0
*/
ReversedArgsArrayIterationFunctions<E> reversedArgs();

/**
* Finds any element matching the filter predicate.
*
* @param <A> 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
<A> E findAny(A arg1, BiPredicate<? super E, A> 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<? super E> filter);

/**
* Performs an action for each element with an additional argument.
*
* @param <A> 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
*/
<A> ArrayIterationFunctions<E> forEach(A arg1, BiConsumer<? super E, A> consumer);

/**
* Performs an action for each element with two additional arguments.
*
* @param <A> the type of the first argument
* @param <B> 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
*/
<A, B> ArrayIterationFunctions<E> forEach(A arg1, B arg2, TriConsumer<? super E, A, B> consumer);

/**
* Performs an action for each element with an object and long argument.
*
* @param <A> 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
*/
<A> ArrayIterationFunctions<E> forEach(A arg1, long arg2, ObjObjLongConsumer<? super E, A> consumer);

/**
* Returns whether any element matches the filter predicate.
*
* @param <A> 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
*/
<A> boolean anyMatch(A arg1, BiPredicate<? super E, A> filter);
}
Loading
Loading