Skip to content

Commit b4c0b95

Browse files
authored
Merge pull request #51 from JavaSaBr/extend-api-v6
Extend API v6
2 parents cada4e8 + a492669 commit b4c0b95

30 files changed

+1506
-109
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repositories {
1414
}
1515
1616
ext {
17-
rlibVersion = "10.0.alpha6"
17+
rlibVersion = "10.0.alpha7"
1818
}
1919
2020
dependencies {

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rootProject.version = "10.0.alpha6"
1+
rootProject.version = "10.0.alpha7"
22
group = 'javasabr.rlib'
33

44
allprojects {

rlib-collections/src/main/java/javasabr/rlib/collections/array/Array.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javasabr.rlib.collections.array;
22

33
import java.io.Serializable;
4+
import java.util.Arrays;
45
import java.util.Collection;
56
import java.util.Iterator;
67
import java.util.List;
@@ -9,6 +10,7 @@
910
import java.util.stream.Stream;
1011
import javasabr.rlib.collections.array.impl.DefaultArrayIterator;
1112
import javasabr.rlib.collections.array.impl.ImmutableArray;
13+
import javasabr.rlib.common.util.ArrayUtils;
1214
import javasabr.rlib.common.util.ClassUtils;
1315
import org.jspecify.annotations.Nullable;
1416

@@ -21,20 +23,44 @@ static <E> Array<E> empty(Class<? super E> type) {
2123
return new ImmutableArray<>(ClassUtils.unsafeCast(type));
2224
}
2325

24-
static <E> Array<E> of(E e1) {
25-
return new ImmutableArray<>(Object.class, e1);
26+
static <E> Array<E> of(E single) {
27+
@SuppressWarnings("unchecked")
28+
Class<E> type = (Class<E>) single.getClass();
29+
return new ImmutableArray<>(type, single);
2630
}
2731

2832
static <E> Array<E> of(E e1, E e2) {
29-
return new ImmutableArray<>(Object.class, e1, e2);
33+
Class<E> commonType = ClassUtils.commonType(e1, e2);
34+
return new ImmutableArray<>(commonType, e1, e2);
3035
}
3136

3237
static <E> Array<E> of(E e1, E e2, E e3) {
33-
return new ImmutableArray<>(Object.class, e1, e2, e3);
38+
Class<E> commonType = ClassUtils.commonType(e1, e2, e3);
39+
return new ImmutableArray<>(commonType, e1, e2, e3);
3440
}
3541

3642
static <E> Array<E> of(E e1, E e2, E e3, E e4) {
37-
return new ImmutableArray<>(Object.class, e1, e2, e3, e4);
43+
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4);
44+
return new ImmutableArray<>(commonType, e1, e2, e3, e4);
45+
}
46+
47+
static <E> Array<E> of(E e1, E e2, E e3, E e4, E e5) {
48+
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4, e5);
49+
return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5);
50+
}
51+
52+
static <E> Array<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
53+
Class<E> commonType = ClassUtils.commonType(e1, e2, e3, e4, e5, e6);
54+
return new ImmutableArray<>(commonType, e1, e2, e3, e4, e5, e6);
55+
}
56+
57+
@SafeVarargs
58+
static <E> Array<E> of(E... elements) {
59+
//noinspection unchecked
60+
Class<E> type = (Class<E>) elements
61+
.getClass()
62+
.getComponentType();
63+
return new ImmutableArray<>(type, elements);
3864
}
3965

4066
@SafeVarargs
@@ -51,7 +77,21 @@ static <E> Array<E> optionals(Class<? super E> type, Optional<E>... elements) {
5177
.collect(ArrayCollectors.toArray(type));
5278
}
5379

54-
static <E> Array<E> copyOf(MutableArray<E> array) {
80+
/**
81+
* Creates array with the same element repeated 'count' times
82+
*/
83+
static <E> Array<E> repeated(E element, int count) {
84+
@SuppressWarnings("unchecked")
85+
Class<E> type = (Class<E>) element.getClass();
86+
E[] array = ArrayUtils.create(type, count);
87+
Arrays.fill(array, element);
88+
return ImmutableArray.trustWrap(array);
89+
}
90+
91+
static <E> Array<E> copyOf(Array<E> array) {
92+
if (array instanceof ImmutableArray<E>) {
93+
return array;
94+
}
5595
return new ImmutableArray<>(array.type(), array.toArray());
5696
}
5797

@@ -203,8 +243,6 @@ default Iterator<E> iterator() {
203243
*/
204244
Stream<E> stream();
205245

206-
ReversedArrayIterationFunctions<E> reversedIterations();
207-
208246
ArrayIterationFunctions<E> iterations();
209247

210248
UnsafeArray<E> asUnsafe();

rlib-collections/src/main/java/javasabr/rlib/collections/array/ArrayIterationFunctions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
public interface ArrayIterationFunctions<E> {
1111

12+
ReversedArgsArrayIterationFunctions<E> reversedArgs();
13+
1214
@Nullable
1315
<A> E findAny(A arg1, BiPredicate<? super E, A> filter);
1416

rlib-collections/src/main/java/javasabr/rlib/collections/array/IntArray.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javasabr.rlib.collections.array;
22

33
import java.io.Serializable;
4+
import java.util.Arrays;
45
import java.util.RandomAccess;
56
import java.util.stream.IntStream;
67
import javasabr.rlib.collections.array.impl.ImmutableIntArray;
@@ -40,12 +41,20 @@ static IntArray copyOf(IntArray intArray) {
4041
return new ImmutableIntArray(intArray.toArray());
4142
}
4243

44+
static IntArray repeated(int value, int count) {
45+
int[] values = new int[count];
46+
Arrays.fill(values, value);
47+
return new ImmutableIntArray(values);
48+
}
49+
4350
int size();
4451

4552
boolean contains(int value);
4653

4754
boolean containsAll(IntArray array);
4855

56+
boolean containsAll(int[] array);
57+
4958
/**
5059
* @return the first element or {@link java.util.NoSuchElementException}
5160
*/

rlib-collections/src/main/java/javasabr/rlib/collections/array/LongArray.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javasabr.rlib.collections.array;
22

33
import java.io.Serializable;
4+
import java.util.Arrays;
45
import java.util.RandomAccess;
56
import java.util.stream.LongStream;
67
import javasabr.rlib.collections.array.impl.ImmutableLongArray;
@@ -40,12 +41,20 @@ static LongArray copyOf(LongArray intArray) {
4041
return new ImmutableLongArray(intArray.toArray());
4142
}
4243

44+
static LongArray repeated(long value, int count) {
45+
long[] values = new long[count];
46+
Arrays.fill(values, value);
47+
return new ImmutableLongArray(values);
48+
}
49+
4350
int size();
4451

4552
boolean contains(long value);
4653

4754
boolean containsAll(LongArray array);
4855

56+
boolean containsAll(long[] array);
57+
4958
/**
5059
* @return the first element or {@link java.util.NoSuchElementException}
5160
*/

rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableIntArray.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ public interface MutableIntArray extends IntArray {
1111
/**
1212
* @return the element previously at the specified position
1313
*/
14-
int removeByInex(int index);
14+
int removeByIndex(int index);
1515

1616
boolean remove(int value);
1717

18+
boolean removeAll(IntArray array);
19+
20+
boolean removeAll(int[] array);
21+
1822
void replace(int index, int value);
1923

2024
void clear();

rlib-collections/src/main/java/javasabr/rlib/collections/array/MutableLongArray.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ public interface MutableLongArray extends LongArray {
1111
/**
1212
* @return the element previously at the specified position
1313
*/
14-
long removeByInex(int index);
14+
long removeByIndex(int index);
1515

1616
boolean remove(long value);
1717

18+
boolean removeAll(LongArray array);
19+
20+
boolean removeAll(long[] array);
21+
1822
void replace(int index, long value);
1923

2024
void clear();
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import javasabr.rlib.functions.TriConsumer;
66
import org.jspecify.annotations.Nullable;
77

8-
public interface ReversedArrayIterationFunctions<E> {
8+
public interface ReversedArgsArrayIterationFunctions<E> {
99

1010
@Nullable
1111
<A> E findAny(A arg1, BiPredicate<A, ? super E> filter);
1212

13-
<A> ReversedArrayIterationFunctions<E> forEach(A arg1, BiConsumer<A, ? super E> consumer);
13+
<A> ReversedArgsArrayIterationFunctions<E> forEach(A arg1, BiConsumer<A, ? super E> consumer);
1414

15-
<A, B> ReversedArrayIterationFunctions<E> forEach(A arg1, B arg2, TriConsumer<A, B, ? super E> consumer);
15+
<A, B> ReversedArgsArrayIterationFunctions<E> forEach(A arg1, B arg2, TriConsumer<A, B, ? super E> consumer);
1616

1717
<A> boolean anyMatch(A arg1, BiPredicate<A, ? super E> filter);
1818
}

rlib-collections/src/main/java/javasabr/rlib/collections/array/impl/AbstractArray.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.function.Function;
1010
import javasabr.rlib.collections.array.Array;
1111
import javasabr.rlib.collections.array.ArrayIterationFunctions;
12-
import javasabr.rlib.collections.array.ReversedArrayIterationFunctions;
1312
import javasabr.rlib.collections.array.UnsafeArray;
1413
import javasabr.rlib.common.util.ArrayUtils;
1514
import javasabr.rlib.common.util.ClassUtils;
@@ -174,13 +173,13 @@ public void forEach(Consumer<? super E> action) {
174173
}
175174

176175
@Override
177-
public ReversedArrayIterationFunctions<E> reversedIterations() {
178-
return new DefaultReversedArrayIterationFunctions<>(this);
176+
public ArrayIterationFunctions<E> iterations() {
177+
return createIterations();
179178
}
180179

181-
@Override
182-
public ArrayIterationFunctions<E> iterations() {
183-
return new DefaultArrayIterationFunctions<>(this);
180+
protected ArrayIterationFunctions<E> createIterations() {
181+
return new DefaultArrayIterationFunctions<>(this,
182+
new DefaultReversedArgsArrayIterationFunctions<>(this));
184183
}
185184

186185
@Override

0 commit comments

Comments
 (0)