Skip to content

Commit 9ecc214

Browse files
committed
update array API and test coverage
1 parent 9c511c1 commit 9ecc214

14 files changed

Lines changed: 300 additions & 297 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
/**
6+
* @author JavaSaBr
7+
*/
8+
@FunctionalInterface
9+
public interface NotNullIntBiObjectConsumer<S, T> extends IntBiObjectConsumer<S, T> {
10+
11+
@Override
12+
void accept(int first, @NotNull S second, @NotNull T third);
13+
}

rlib-common/src/main/java/com/ss/rlib/common/util/ArrayUtils.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.ss.rlib.common.function.*;
44
import com.ss.rlib.common.util.array.Array;
5-
import com.ss.rlib.common.util.array.ConcurrentArray;
65
import com.ss.rlib.common.util.array.IntegerArray;
76
import com.ss.rlib.common.util.array.LongArray;
87
import org.jetbrains.annotations.NotNull;
@@ -103,7 +102,7 @@ public final class ArrayUtils {
103102

104103
int length = array.length;
105104

106-
array = copyOf(array, 1);
105+
array = copyOfAndExtend(array, 1);
107106
array[length] = element;
108107

109108
return array;
@@ -332,15 +331,15 @@ public static boolean contains(@NotNull Object[] array, @NotNull Object object)
332331
}
333332

334333
/**
335-
* Copy and extend if need a native array.
334+
* Copy an array and extend if need.
336335
*
337336
* @param <T> the array component's type.
338337
* @param original the original array.
339-
* @param added the added size.
340-
* @return the new copied native array.
338+
* @param added the additional size.
339+
* @return the new copied array.
341340
*/
342341
@SuppressWarnings("unchecked")
343-
public static <T> @NotNull T[] copyOf(@NotNull T[] original, int added) {
342+
public static <T> @NotNull T[] copyOfAndExtend(@NotNull T[] original, int added) {
344343
return Arrays.copyOf(original, original.length + added);
345344
}
346345

rlib-common/src/main/java/com/ss/rlib/common/util/array/Array.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface Array<E> extends Collection<E>, Serializable, Reusable, Cloneab
5555
* @return the new read only array.
5656
*/
5757
static <T> @NotNull ReadOnlyArray<T> of(@NotNull Array<T> another) {
58-
return ArrayFactory.newReadOnlyArray(Arrays.copyOf(another.array(), another.size()));
58+
return ArrayFactory.newReadOnlyArray(ArrayUtils.copyOf(another.array()));
5959
}
6060

6161
/**
@@ -292,31 +292,14 @@ default boolean fastRemove(@NotNull Object object) {
292292
* @param array the array with elements to remove.
293293
* @return count of removed elements.
294294
*/
295-
default int fastRemove(@NotNull Array<? extends E> array) {
295+
default int fastRemoveAll(@NotNull E[] array) {
296296

297297
int count = 0;
298298

299-
for (E object : array.array()) {
300-
if (object == null) break;
301-
if (fastRemove(object)) count++;
302-
}
303-
304-
return count;
305-
}
306-
307-
/**
308-
* Removes the each element from the array.
309-
*
310-
* @param array the array with elements to remove.
311-
* @return count of removed elements.
312-
*/
313-
default int fastRemove(@NotNull E[] array) {
314-
315-
int count = 0;
316-
317-
for (E object : array) {
318-
if (object == null) break;
319-
if (fastRemove(object)) count++;
299+
for (var object : array) {
300+
if (fastRemove(object)) {
301+
count++;
302+
}
320303
}
321304

322305
return count;
@@ -493,10 +476,8 @@ default boolean removeAll(@NotNull Collection<?> target) {
493476

494477
int count = 0;
495478

496-
for (Object element : target) {
497-
if (element == null) {
498-
break;
499-
} else if (slowRemove(element)) {
479+
for (var element : target) {
480+
if (slowRemove(element)) {
500481
count++;
501482
}
502483
}
@@ -583,7 +564,7 @@ default void set(int index, @NotNull E element) {
583564
@Override
584565
default boolean remove(@NotNull Object object) {
585566

586-
int index = indexOf(object);
567+
var index = indexOf(object);
587568

588569
if (index >= 0) {
589570
remove(index);
@@ -1318,6 +1299,27 @@ default <F, S> void forEach(
13181299
}
13191300
}
13201301

1302+
/**
1303+
* Apply a function to each element.
1304+
*
1305+
* @param first the first argument.
1306+
* @param second the second argument.
1307+
* @param consumer the function.
1308+
* @param <A> the second argument's type.
1309+
*/
1310+
default <A> void forEach(
1311+
int first,
1312+
@NotNull A second,
1313+
@NotNull NotNullIntBiObjectConsumer<A, ? super E> consumer
1314+
) {
1315+
1316+
var array = array();
1317+
1318+
for (int i = 0, length = size(); i < length; i++) {
1319+
consumer.accept(first, second, array[i]);
1320+
}
1321+
}
1322+
13211323
/**
13221324
* Apply the function to each element.
13231325
*

rlib-common/src/main/java/com/ss/rlib/common/util/array/ArrayFactory.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,11 @@ public class ArrayFactory {
1414

1515
public static final Array<?> EMPTY_ARRAY = newReadOnlyArray(ArrayUtils.EMPTY_OBJECT_ARRAY);
1616

17-
/**
18-
* Wrap arguments as an array collection.
19-
*
20-
* @param args the elements to be wrapped.
21-
* @param <E> the element's type.
22-
* @return the new array collection with the wrapped arguments.
23-
*/
2417
@SafeVarargs
2518
public static <E> @NotNull Array<E> asArray(@NotNull E... args) {
2619
return new FastArray<>(args);
2720
}
2821

29-
/**
30-
* Create the new mutable array.
31-
*
32-
* @param type the type of the array.
33-
* @param <E> the element's type.
34-
* @return the new mutable array collection.
35-
*/
3622
public static <E> @NotNull Array<E> newArray(@NotNull Class<? super E> type) {
3723
return newUnsafeArray(type);
3824
}

rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ default void writeUnlock(long stamp) {
159159
return this;
160160
}
161161

162+
/**
163+
* Apply a function to each element.
164+
*
165+
* @param first the first argument.
166+
* @param second the second argument.
167+
* @param consumer the function.
168+
* @param <A> the second argument's type.
169+
*/
170+
default <A> @NotNull ConcurrentArray<E> forEachInReadLock(
171+
int first,
172+
@NotNull A second,
173+
@NotNull NotNullIntBiObjectConsumer<A, ? super E> consumer
174+
) {
175+
176+
var stamp = readLock();
177+
try {
178+
forEach(first, second, consumer);
179+
} finally {
180+
readUnlock(stamp);
181+
}
182+
183+
return this;
184+
}
185+
162186
/**
163187
* Apply a function to each element under {@link #readLock()} block.
164188
*

rlib-common/src/main/java/com/ss/rlib/common/util/array/impl/AbstractArray.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import com.ss.rlib.common.util.ClassUtils;
55
import com.ss.rlib.common.util.array.Array;
66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
78

9+
import java.util.Arrays;
10+
import java.util.Objects;
811
import java.util.function.Function;
912

1013
/**
@@ -18,27 +21,22 @@ public abstract class AbstractArray<E> implements Array<E> {
1821
private static final long serialVersionUID = 2113052245369887690L;
1922

2023
/**
21-
* The size of big array.
24+
* The default size of new backend array.
2225
*/
23-
protected static final int SIZE_BIG_ARRAY = 10;
24-
25-
/**
26-
* The default size of new array.
27-
*/
28-
protected static final int DEFAULT_SIZE = 10;
26+
protected static final int DEFAULT_CAPACITY = 10;
2927

3028
public AbstractArray(@NotNull Class<? super E> type) {
31-
this(type, DEFAULT_SIZE);
29+
this(type, DEFAULT_CAPACITY);
3230
}
3331

34-
public AbstractArray(@NotNull Class<? super E> type, int size) {
32+
public AbstractArray(@NotNull Class<? super E> type, int capacity) {
3533
super();
3634

37-
if (size < 0) {
38-
throw new IllegalArgumentException("negative size");
35+
if (capacity < 0) {
36+
throw new IllegalArgumentException("Negative capacity");
3937
}
4038

41-
setArray(ArrayUtils.create(type, size));
39+
setArray(ArrayUtils.create(type, capacity));
4240
}
4341

4442
public AbstractArray(@NotNull E[] array) {
@@ -79,4 +77,24 @@ public String toString() {
7977
return getClass().getSimpleName() + " size = " + size() +
8078
" :\n " + ArrayUtils.toString(this, toString);
8179
}
80+
81+
@Override
82+
public boolean equals(@Nullable Object another) {
83+
84+
if (this == another) {
85+
return true;
86+
} else if (!(another instanceof Array)) {
87+
return false;
88+
}
89+
90+
var array = (Array<?>) another;
91+
return size() == array.size() && Arrays.equals(array(), 0, size(), array.array(), 0, array.size());
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
int result = Objects.hash(size());
97+
result = 31 * result + Arrays.hashCode(array());
98+
return result;
99+
}
82100
}

rlib-common/src/main/java/com/ss/rlib/common/util/array/impl/AbstractConcurrentArray.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public AbstractConcurrentArray(@NotNull Class<? super E> type, int size) {
4848
public boolean add(@NotNull E element) {
4949

5050
if (size() == array.length) {
51-
array = ArrayUtils.copyOf(array, Math.max(array.length >> 1, 1));
51+
array = ArrayUtils.copyOfAndExtend(array, Math.max(array.length >> 1, 1));
5252
}
5353

5454
array[size.getAndIncrement()] = element;
@@ -68,7 +68,7 @@ public final boolean addAll(@NotNull Array<? extends E> elements) {
6868
var diff = selfSize + targetSize - current;
6969

7070
if (diff > 0) {
71-
array = copyOf(array, max(current >> 1, diff));
71+
array = ArrayUtils.copyOfAndExtend(array, max(current >> 1, diff));
7272
}
7373

7474
processAdd(elements, selfSize, targetSize);
@@ -86,7 +86,7 @@ public boolean addAll(@NotNull Collection<? extends E> collection) {
8686
var diff = size() + collection.size() - current;
8787

8888
if (diff > 0) {
89-
array = ArrayUtils.copyOf(array, Math.max(current >> 1, diff));
89+
array = ArrayUtils.copyOfAndExtend(array, Math.max(current >> 1, diff));
9090
}
9191

9292
for (var element : collection) {
@@ -109,7 +109,7 @@ public final boolean addAll(@NotNull E[] elements) {
109109
var diff = selfSize + targetSize - current;
110110

111111
if (diff > 0) {
112-
array = copyOf(array, max(current >> 1, diff));
112+
array = ArrayUtils.copyOfAndExtend(array, max(current >> 1, diff));
113113
}
114114

115115
processAdd(elements, selfSize, targetSize);
@@ -129,7 +129,7 @@ public void prepareForSize(int size) {
129129
var diff = selfSize + size - current;
130130

131131
if (diff > 0) {
132-
array = ArrayUtils.copyOf(array, Math.max(current >> 1, diff));
132+
array = ArrayUtils.copyOfAndExtend(array, Math.max(current >> 1, diff));
133133
}
134134
}
135135

@@ -276,7 +276,7 @@ protected void processAdd(@NotNull E[] elements, int selfSize, int targetSize) {
276276
@Override
277277
public @NotNull AbstractConcurrentArray<E> clone() throws CloneNotSupportedException {
278278
var clone = (AbstractConcurrentArray<E>) super.clone();
279-
clone.array = ArrayUtils.copyOf(array, size());
279+
clone.array = ArrayUtils.copyOfAndExtend(array, size());
280280
return clone;
281281
}
282282
}

rlib-common/src/main/java/com/ss/rlib/common/util/array/impl/CopyOnModifyArray.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
import com.ss.rlib.common.util.array.ArrayComparator;
77
import com.ss.rlib.common.util.array.ArrayFactory;
88
import org.jetbrains.annotations.NotNull;
9-
import org.jetbrains.annotations.Nullable;
109

1110
import java.util.Collection;
1211
import java.util.Iterator;
1312
import java.util.NoSuchElementException;
14-
import java.util.function.BiConsumer;
15-
import java.util.function.BiFunction;
1613

1714
/**
1815
* The implementation of the array which create a new back-end array for each modification.
@@ -36,7 +33,7 @@ public CopyOnModifyArray(@NotNull Class<? super E> type, int size) {
3633
public boolean add(@NotNull E object) {
3734

3835
var current = array.get();
39-
var newArray = ArrayUtils.copyOf(current, 1);
36+
var newArray = ArrayUtils.copyOfAndExtend(current, 1);
4037
newArray[current.length] = object;
4138

4239
if (!array.compareAndSet(current, newArray)) {
@@ -54,7 +51,7 @@ public boolean addAll(@NotNull Array<? extends E> elements) {
5451
}
5552

5653
var current = array.get();
57-
var newArray = ArrayUtils.copyOf(current, elements.size());
54+
var newArray = ArrayUtils.copyOfAndExtend(current, elements.size());
5855

5956
for (int i = current.length, g = 0; i < newArray.length; i++, g++) {
6057
newArray[i] = elements.get(g);
@@ -75,7 +72,7 @@ public boolean addAll(@NotNull Collection<? extends E> collection) {
7572
}
7673

7774
var current = array.get();
78-
var newArray = ArrayUtils.copyOf(current, collection.size());
75+
var newArray = ArrayUtils.copyOfAndExtend(current, collection.size());
7976

8077
Iterator<? extends E> iterator = collection.iterator();
8178

@@ -259,7 +256,7 @@ public final int size() {
259256
public @NotNull Array<E> sort(@NotNull ArrayComparator<E> comparator) {
260257

261258
var current = array();
262-
var newArray = ArrayUtils.copyOf(current, 0);
259+
var newArray = ArrayUtils.copyOfAndExtend(current, 0);
263260

264261
ArrayUtils.sort(newArray, 0, newArray.length, comparator);
265262

0 commit comments

Comments
 (0)