-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArrayBuilder.java
More file actions
86 lines (77 loc) · 2.01 KB
/
ArrayBuilder.java
File metadata and controls
86 lines (77 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package javasabr.rlib.collections.array;
import java.util.Collection;
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);
}
}