Skip to content

Commit e6d5242

Browse files
committed
extend long/int arrays API
1 parent cada4e8 commit e6d5242

25 files changed

+968
-76
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/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

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.NoSuchElementException;
66
import javasabr.rlib.collections.array.IntArray;
77
import javasabr.rlib.collections.array.UnsafeIntArray;
8+
import javasabr.rlib.common.util.ArrayUtils;
89
import lombok.AccessLevel;
910
import lombok.experimental.Accessors;
1011
import lombok.experimental.FieldDefaults;
@@ -110,32 +111,28 @@ public int[] toArray() {
110111

111112
@Override
112113
public boolean equals(Object another) {
113-
114114
if (!(another instanceof IntArray array)) {
115115
return false;
116116
}
117-
118-
int[] wrapped = array
119-
.asUnsafe()
120-
.wrapped();
121-
117+
int[] wrapped = array.asUnsafe().wrapped();
122118
return Arrays.equals(wrapped(), 0, size(), wrapped, 0, array.size());
123119
}
124120

125121
@Override
126122
public int hashCode() {
127-
128123
int[] wrapped = wrapped();
129-
130124
int result = 1;
131-
132125
for (int i = 0, wrappedLength = size(); i < wrappedLength; i++) {
133126
result = 31 * result + wrapped[i];
134127
}
135-
136128
return result;
137129
}
138130

131+
@Override
132+
public String toString() {
133+
return ArrayUtils.toString(wrapped(), 0, size(), ",", false, true);
134+
}
135+
139136
@Override
140137
public UnsafeIntArray asUnsafe() {
141138
return this;

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.NoSuchElementException;
66
import javasabr.rlib.collections.array.LongArray;
77
import javasabr.rlib.collections.array.UnsafeLongArray;
8+
import javasabr.rlib.common.util.ArrayUtils;
89
import lombok.AccessLevel;
910
import lombok.experimental.Accessors;
1011
import lombok.experimental.FieldDefaults;
@@ -110,32 +111,28 @@ public long[] toArray() {
110111

111112
@Override
112113
public boolean equals(Object another) {
113-
114114
if (!(another instanceof LongArray array)) {
115115
return false;
116116
}
117-
118-
long[] wrapped = array
119-
.asUnsafe()
120-
.wrapped();
121-
117+
long[] wrapped = array.asUnsafe().wrapped();
122118
return Arrays.equals(wrapped(), 0, size(), wrapped, 0, array.size());
123119
}
124120

125121
@Override
126122
public int hashCode() {
127-
128123
long[] wrapped = wrapped();
129-
130124
int result = 1;
131-
132125
for (int i = 0, wrappedLength = size(); i < wrappedLength; i++) {
133126
result = 31 * result + Long.hashCode(wrapped[i]);
134127
}
135-
136128
return result;
137129
}
138130

131+
@Override
132+
public String toString() {
133+
return ArrayUtils.toString(wrapped(), 0, size(), ",", false, true);
134+
}
135+
139136
@Override
140137
public UnsafeLongArray asUnsafe() {
141138
return this;

0 commit comments

Comments
 (0)