-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArray.java
More file actions
251 lines (213 loc) · 7.83 KB
/
Array.java
File metadata and controls
251 lines (213 loc) · 7.83 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package javasabr.rlib.collections.array;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import javasabr.rlib.collections.array.impl.DefaultArrayIterator;
import javasabr.rlib.collections.array.impl.ImmutableArray;
import javasabr.rlib.common.util.ArrayUtils;
import javasabr.rlib.common.util.ClassUtils;
import org.jspecify.annotations.Nullable;
/**
* @author JavaSaBr
*/
public interface Array<E> extends Iterable<E>, Serializable, Cloneable {
static <E> Array<E> empty(Class<? super E> type) {
return new ImmutableArray<>(ClassUtils.unsafeCast(type));
}
static <E> Array<E> of(E single) {
@SuppressWarnings("unchecked")
Class<E> type = (Class<E>) single.getClass();
return new ImmutableArray<>(type, single);
}
static <E> Array<E> of(E e1, E e2) {
Class<E> commonType = ClassUtils.commonType(e1, e2);
return new ImmutableArray<>(commonType, e1, e2);
}
static <E> Array<E> of(E e1, E e2, E e3) {
Class<E> commonType = ClassUtils.commonType(e1, e2, e3);
return new ImmutableArray<>(commonType, e1, e2, e3);
}
static <E> Array<E> of(E e1, E e2, E e3, E e4) {
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4);
return new ImmutableArray<>(commonType, e1, e2, e3, e4);
}
static <E> Array<E> of(E e1, E e2, E e3, E e4, E e5) {
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4, e5);
return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5);
}
static <E> Array<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4, e5, e6);
return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5, e6);
}
@SafeVarargs
static <E> Array<E> of(E... elements) {
//noinspection unchecked
Class<E> type = (Class<E>) elements
.getClass()
.getComponentType();
return new ImmutableArray<>(type, elements);
}
@SafeVarargs
static <E> Array<E> typed(Class<? super E> type, E... elements) {
return new ImmutableArray<>(type, elements);
}
@SafeVarargs
static <E> Array<E> optionals(Class<? super E> type, Optional<E>... elements) {
return Stream
.of(elements)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(ArrayCollectors.toArray(type));
}
/**
* Creates array with the same element repeated 'count' times
*/
static <E> Array<E> repeated(E element, int count) {
@SuppressWarnings("unchecked")
Class<E> type = (Class<E>) element.getClass();
E[] array = ArrayUtils.create(type, count);
Arrays.fill(array, element);
return ImmutableArray.trustWrap(array);
}
static <E> Array<E> copyOf(Array<E> array) {
if (array instanceof ImmutableArray<E>) {
return array;
}
return new ImmutableArray<>(array.type(), array.toArray());
}
Class<E> type();
/**
* 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
*/
int size();
/**
* Returns {@code true} if this array contains the specified element.
* More formally, returns {@code true} if and only if this array
* contains at least one element {@code e} such that
* {@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})
*/
boolean contains(@Nullable Object object);
/**
* 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
* @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)
*/
boolean containsAll(Array<?> array);
/**
* Returns {@code true} if this array contains all of the elements
* in the specified 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)
*/
boolean containsAll(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
* @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)
*/
boolean containsAll(Object[] array);
/**
* Gets the first element of this array.
* @return the retrieved element or null
*/
@Nullable
E first();
E get(int index);
/**
* Gets the last element of this array.
* @return the retrieved element or null
*/
@Nullable
E last();
@Override
default Iterator<E> iterator() {
return new DefaultArrayIterator<>(this);
}
/**
* @return the index of the object or -1.
*/
int indexOf(@Nullable Object object);
/**
* @return the index of the object or -1.
*/
<T> int indexOf(Function<E, T> getter, @Nullable Object object);
int lastIndexOf(@Nullable Object object);
<T > T[] toArray(T[] newArray);
/**
* Copy this array to the new array.
*
* @param <T> the type parameter
* @param componentType the type of the new array.
* @return the copied array.
*/
<T> T[] toArray(Class<T> componentType);
boolean isEmpty();
E[] toArray();
String toString(Function<E, String> toString);
/**
* Returns a sequential {@code Stream} with this array as its source.
*
* @return a sequential {@code Stream} over the elements in this array
*/
Stream<E> stream();
ArrayIterationFunctions<E> iterations();
UnsafeArray<E> asUnsafe();
List<E> toList();
}