Skip to content

Commit df79663

Browse files
committed
add sort method to arrays
1 parent b6256e8 commit df79663

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

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

Lines changed: 5 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.util.Collection;
4+
import java.util.Comparator;
45
import java.util.Iterator;
56
import java.util.function.Consumer;
67
import java.util.function.IntFunction;
@@ -48,4 +49,8 @@ default void forEach(Consumer<? super E> action) {
4849

4950
@Override
5051
UnsafeMutableArray<E> asUnsafe();
52+
53+
void sort();
54+
55+
void sort(Comparator<E> comparator);
5156
}

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

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

33
import java.util.Arrays;
44
import java.util.Collection;
5+
import java.util.Comparator;
56
import java.util.Iterator;
67
import java.util.Objects;
78
import java.util.Spliterator;
@@ -10,6 +11,7 @@
1011
import java.util.stream.StreamSupport;
1112
import javasabr.rlib.collections.array.Array;
1213
import javasabr.rlib.collections.array.UnsafeMutableArray;
14+
import javasabr.rlib.common.util.ObjectUtils;
1315
import lombok.AccessLevel;
1416
import lombok.experimental.FieldDefaults;
1517
import org.jspecify.annotations.Nullable;
@@ -226,6 +228,30 @@ public UnsafeMutableArray<E> asUnsafe() {
226228
return this;
227229
}
228230

231+
@Override
232+
233+
public void sort() {
234+
sortInternalArray(wrapped(), size());
235+
}
236+
237+
@SuppressWarnings({
238+
"rawtypes",
239+
"unchecked"
240+
})
241+
protected void sortInternalArray(@Nullable E[] array, int size) {
242+
if (Comparable.class.isAssignableFrom(type())) {
243+
Comparable[] wrapped = (Comparable[]) array;
244+
Arrays.sort(wrapped, 0, size, Comparator.naturalOrder());
245+
} else {
246+
Arrays.sort(array, 0, size, Comparator.comparingInt(ObjectUtils::hash));
247+
}
248+
}
249+
250+
@Override
251+
public void sort(Comparator<E> comparator) {
252+
Arrays.sort(wrapped(), 0, size(), comparator);
253+
}
254+
229255
protected static void validateCapacity(int capacity) {
230256
if (capacity < 0) {
231257
throw new IllegalArgumentException("Capacity cannot be negative");

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

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

33
import java.util.Arrays;
44
import java.util.Collection;
5+
import java.util.Comparator;
56
import java.util.ConcurrentModificationException;
67
import java.util.concurrent.atomic.AtomicReference;
78
import javasabr.rlib.collections.array.Array;
@@ -208,4 +209,30 @@ protected int getAndIncrementSize() {
208209
protected int decrementAnGetSize() {
209210
return 0;
210211
}
212+
213+
@Override
214+
public void sort() {
215+
for (int i = 0; i < LIMIT_ATTEMPTS; i++) {
216+
@Nullable E[] original = wrapped.get();
217+
@Nullable E[] copy = Arrays.copyOf(original, original.length);
218+
sortInternalArray(copy, copy.length);
219+
if (wrapped.compareAndSet(original, copy)) {
220+
return;
221+
}
222+
}
223+
throw new ConcurrentModificationException("Cannot successfully sort this array");
224+
}
225+
226+
@Override
227+
public void sort(Comparator<E> comparator) {
228+
for (int i = 0; i < LIMIT_ATTEMPTS; i++) {
229+
@Nullable E[] original = wrapped.get();
230+
@Nullable E[] copy = Arrays.copyOf(original, original.length);
231+
Arrays.sort(copy, comparator);
232+
if (wrapped.compareAndSet(original, copy)) {
233+
return;
234+
}
235+
}
236+
throw new ConcurrentModificationException("Cannot successfully sort this array");
237+
}
211238
}

rlib-collections/src/test/java/javasabr/rlib/collections/array/MutableArrayTest.java

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

3+
import java.util.Comparator;
34
import java.util.List;
45
import java.util.stream.Stream;
56
import org.junit.jupiter.api.Assertions;
@@ -253,6 +254,36 @@ void shouldRenderToStringCorrectly(MutableArray<String> mutableArray) {
253254
mutableArray.toString());
254255
}
255256

257+
@ParameterizedTest
258+
@MethodSource("generateMutableArrays")
259+
@DisplayName("should sort array correctly")
260+
void shouldSortArrayCorrectly(MutableArray<String> mutableArray) {
261+
// given:
262+
mutableArray.addAll(Array.of("10", "99", "5", "3", "77", "45", "25", "56"));
263+
264+
// when:
265+
mutableArray.sort();
266+
267+
// then:
268+
var expected = Array.of("10", "25", "3", "45", "5", "56", "77", "99");
269+
Assertions.assertEquals(expected, mutableArray);
270+
}
271+
272+
@ParameterizedTest
273+
@MethodSource("generateMutableArrays")
274+
@DisplayName("should sort array correctly")
275+
void shouldSortArrayUsingComparatorCorrectly(MutableArray<String> mutableArray) {
276+
// given:
277+
mutableArray.addAll(Array.of("10", "99", "5", "3", "77", "45", "25", "56"));
278+
279+
// when:
280+
mutableArray.sort(Comparator.comparingInt(Integer::parseInt));
281+
282+
// then:
283+
var expected = Array.of("3", "5", "10", "25", "45", "56", "77", "99");
284+
Assertions.assertEquals(expected, mutableArray);
285+
}
286+
256287
private static Stream<Arguments> generateMutableArrays() {
257288
return Stream.of(
258289
Arguments.of(ArrayFactory.mutableArray(String.class)),

0 commit comments

Comments
 (0)