Skip to content

Commit e57759e

Browse files
committed
#88 Rename class to ArrayUtils
1 parent c49702a commit e57759e

File tree

2 files changed

+65
-64
lines changed

2 files changed

+65
-64
lines changed

Diff for: src/main/java/ch/jalu/typeresolver/array/ArraysMethodsDelegator.java renamed to src/main/java/ch/jalu/typeresolver/array/ArrayUtils.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
import java.util.stream.Stream;
1212

1313
/**
14-
* Offers methods that delegate to various methods in {@link Arrays} with specific array types. Useful if an array
15-
* is of unknown type ({@code float[]}, {@code String[]}, etc.). Methods throw a runtime exception if the given
16-
* object is not an array; check whether an object is an array first with {@code object.getClass().isArray()}.
14+
* This class contains methods that can handle arrays of any type ({@code boolean[]}, {@code byte[]}, {@code Object[]},
15+
* etc.). Most methods delegate to methods in {@link Arrays} with the appropriate signature. The methods in this class
16+
* are useful when dealing with arrays held as {@code Object} variable whose specific array type is unknown.
1717
* <p>
18-
* See individual methods for specific caveats.
18+
* Methods throw a runtime exception if the given object is not an array. To check if an object is an array, use
19+
* {@code object.getClass().isArray()}. See individual methods for specific caveats.
1920
*/
2021
@SuppressWarnings("checkstyle:OneStatementPerLine") // Justification: line-by-line aligned casts in switch statements
21-
public final class ArraysMethodsDelegator {
22+
public final class ArrayUtils {
2223

23-
private ArraysMethodsDelegator() {
24+
private ArrayUtils() {
2425
}
2526

2627
/**
@@ -258,8 +259,8 @@ public static void parallelSort(Object a, int fromIndex, int toIndex) {
258259
* {@link Arrays#stream(int[])}, return different types of streams, this method returns an object stream which is
259260
* constructed by getting the elements of the array by index.
260261
* <p>
261-
* It may be better to handle array types individually with their respective stream types when performance is
262-
* crucial.
262+
* It may be more efficient to handle array types individually with their respective stream types when performance
263+
* is crucial.
263264
*
264265
* @param array the array to stream over
265266
* @return stream of the array's elements (as reference types)

Diff for: src/test/java/ch/jalu/typeresolver/array/ArraysMethodsDelegatorTest.java renamed to src/test/java/ch/jalu/typeresolver/array/ArrayUtilsTest.java

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ch.jalu.typeresolver.array;
22

33
import ch.jalu.typeresolver.EnumUtil;
4-
import ch.jalu.typeresolver.array.ArraysMethodsDelegator.ArrayComponentType;
4+
import ch.jalu.typeresolver.array.ArrayUtils.ArrayComponentType;
55
import ch.jalu.typeresolver.primitives.Primitives;
66
import org.junit.jupiter.api.Nested;
77
import org.junit.jupiter.api.Test;
@@ -28,9 +28,9 @@
2828
import static org.junit.jupiter.api.Assertions.assertThrows;
2929

3030
/**
31-
* Test for {@link ArraysMethodsDelegator}.
31+
* Test for {@link ArrayUtils}.
3232
*/
33-
class ArraysMethodsDelegatorTest {
33+
class ArrayUtilsTest {
3434

3535
/** Holds all tests for delegation methods that test the working scenarios (i.e. no exceptions). */
3636
@Nested
@@ -46,10 +46,10 @@ void shouldDelegateForBinarySearch(ArrayComponentType componentType) {
4646
Object searchKey = elements.get(3);
4747

4848
// when
49-
int idxOfEmpty = ArraysMethodsDelegator.binarySearch(emptyArr, searchKey);
50-
int idxOfFull = ArraysMethodsDelegator.binarySearch(sortedArr, searchKey);
51-
int idxOfPartial = ArraysMethodsDelegator.binarySearch(sortedArr, 2, 5, searchKey);
52-
int idxOutOfRange = ArraysMethodsDelegator.binarySearch(sortedArr, 0, 3, searchKey);
49+
int idxOfEmpty = ArrayUtils.binarySearch(emptyArr, searchKey);
50+
int idxOfFull = ArrayUtils.binarySearch(sortedArr, searchKey);
51+
int idxOfPartial = ArrayUtils.binarySearch(sortedArr, 2, 5, searchKey);
52+
int idxOutOfRange = ArrayUtils.binarySearch(sortedArr, 0, 3, searchKey);
5353

5454
// then
5555
assertThat(idxOfEmpty, lessThan(0));
@@ -71,9 +71,9 @@ void shouldDelegateForCopyOf(ArrayComponentType componentType) {
7171
Object fiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, false);
7272

7373
// when
74-
Object emptyCopy = ArraysMethodsDelegator.copyOf(emptyArr, 0);
75-
Object sevenArr = ArraysMethodsDelegator.copyOf(fiveArr, 7);
76-
Object threeArr = ArraysMethodsDelegator.copyOf(emptyArr, 3);
74+
Object emptyCopy = ArrayUtils.copyOf(emptyArr, 0);
75+
Object sevenArr = ArrayUtils.copyOf(fiveArr, 7);
76+
Object threeArr = ArrayUtils.copyOf(emptyArr, 3);
7777

7878
// then
7979
assertThat(emptyCopy, instanceOf(emptyArr.getClass()));
@@ -104,9 +104,9 @@ void shouldDelegateForCopyOfRange(ArrayComponentType componentType) {
104104
Object fiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, false);
105105

106106
// when
107-
Object emptyCopy = ArraysMethodsDelegator.copyOfRange(emptyArr, 0, 3);
108-
Object arr26 = ArraysMethodsDelegator.copyOfRange(fiveArr, 2, 6);
109-
Object arr49 = ArraysMethodsDelegator.copyOfRange(fiveArr, 4, 9);
107+
Object emptyCopy = ArrayUtils.copyOfRange(emptyArr, 0, 3);
108+
Object arr26 = ArrayUtils.copyOfRange(fiveArr, 2, 6);
109+
Object arr49 = ArrayUtils.copyOfRange(fiveArr, 4, 9);
110110

111111
// then
112112
List<Object> elements = createListWithItems(componentType, false);
@@ -141,9 +141,9 @@ void shouldDelegateForEqualityCheck(ArrayComponentType componentType) {
141141
Object fiveSortedArr = createArray(componentType, ArrayType.FIVE_ITEMS, true);
142142

143143
// when
144-
boolean isEq1 = ArraysMethodsDelegator.equals(fiveArr, fiveArr);
145-
boolean isEq2 = ArraysMethodsDelegator.equals(fiveArr, fiveSortedArr);
146-
boolean isEq3 = ArraysMethodsDelegator.equals(emptyArr, fiveArr);
144+
boolean isEq1 = ArrayUtils.equals(fiveArr, fiveArr);
145+
boolean isEq2 = ArrayUtils.equals(fiveArr, fiveSortedArr);
146+
boolean isEq3 = ArrayUtils.equals(emptyArr, fiveArr);
147147

148148
// then
149149
assertThat(isEq1, equalTo(true));
@@ -162,10 +162,10 @@ void shouldDelegateForFilling(ArrayComponentType componentType) {
162162
List<Object> list = createListWithItems(componentType, false);
163163

164164
// when
165-
ArraysMethodsDelegator.fill(emptyArr, list.get(1));
166-
ArraysMethodsDelegator.fill(fiveArr1, list.get(0));
167-
ArraysMethodsDelegator.fill(fiveArr2, 0, 2, list.get(0));
168-
ArraysMethodsDelegator.fill(fiveArr3, 2, 5, list.get(3));
165+
ArrayUtils.fill(emptyArr, list.get(1));
166+
ArrayUtils.fill(fiveArr1, list.get(0));
167+
ArrayUtils.fill(fiveArr2, 0, 2, list.get(0));
168+
ArrayUtils.fill(fiveArr3, 2, 5, list.get(3));
169169

170170
// then
171171
assertThat(Array.get(fiveArr1, 0), equalTo(list.get(0)));
@@ -196,9 +196,9 @@ void shouldDelegateForHashCode(ArrayComponentType componentType) {
196196
Object fiveArrSorted = createArray(componentType, ArrayType.FIVE_ITEMS, true);
197197

198198
// when
199-
int hashCode1 = ArraysMethodsDelegator.hashCode(emptyArr);
200-
int hashCode2 = ArraysMethodsDelegator.hashCode(fiveArr);
201-
int hashCode3 = ArraysMethodsDelegator.hashCode(fiveArrSorted);
199+
int hashCode1 = ArrayUtils.hashCode(emptyArr);
200+
int hashCode2 = ArrayUtils.hashCode(fiveArr);
201+
int hashCode3 = ArrayUtils.hashCode(fiveArrSorted);
202202

203203
// then
204204
assertThat(hashCode1, equalTo(1));
@@ -216,13 +216,13 @@ void shouldDelegateForParallelSort(ArrayComponentType componentType) {
216216
Object fiveArr2 = createArray(componentType, ArrayType.FIVE_ITEMS, false);
217217

218218
// when
219-
ArraysMethodsDelegator.parallelSort(emptyArr);
220-
ArraysMethodsDelegator.parallelSort(fiveArr1);
221-
ArraysMethodsDelegator.parallelSort(fiveArr2, 2, 5);
219+
ArrayUtils.parallelSort(emptyArr);
220+
ArrayUtils.parallelSort(fiveArr1);
221+
ArrayUtils.parallelSort(fiveArr2, 2, 5);
222222

223223
// then
224224
Object sortedFiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, true);
225-
assertThat(ArraysMethodsDelegator.equals(fiveArr1, sortedFiveArr), equalTo(true));
225+
assertThat(ArrayUtils.equals(fiveArr1, sortedFiveArr), equalTo(true));
226226

227227
List<Object> list = createListWithItems(componentType, false);
228228
List<Object> list2Thru5Sorted = new ArrayList<>(5);
@@ -245,13 +245,13 @@ void shouldDelegateForSort(ArrayComponentType componentType) {
245245
Object fiveArr2 = createArray(componentType, ArrayType.FIVE_ITEMS, false);
246246

247247
// when
248-
ArraysMethodsDelegator.sort(emptyArr);
249-
ArraysMethodsDelegator.sort(fiveArr1);
250-
ArraysMethodsDelegator.sort(fiveArr2, 2, 5);
248+
ArrayUtils.sort(emptyArr);
249+
ArrayUtils.sort(fiveArr1);
250+
ArrayUtils.sort(fiveArr2, 2, 5);
251251

252252
// then
253253
Object sortedFiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, true);
254-
assertThat(ArraysMethodsDelegator.equals(fiveArr1, sortedFiveArr), equalTo(true));
254+
assertThat(ArrayUtils.equals(fiveArr1, sortedFiveArr), equalTo(true));
255255

256256
List<Object> list = createListWithItems(componentType, false);
257257
List<Object> list2Thru5Sorted = new ArrayList<>(5);
@@ -273,8 +273,8 @@ void shouldDelegateForToString(ArrayComponentType componentType) {
273273
Object fiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, false);
274274

275275
// when
276-
String toString1 = ArraysMethodsDelegator.toString(emptyArr);
277-
String toString2 = ArraysMethodsDelegator.toString(fiveArr);
276+
String toString1 = ArrayUtils.toString(emptyArr);
277+
String toString2 = ArrayUtils.toString(fiveArr);
278278

279279
// then
280280
assertThat(toString1, equalTo("[]"));
@@ -291,9 +291,9 @@ void shouldCreateStreamForArray(ArrayComponentType componentType) {
291291
Object fiveArr = createArray(componentType, ArrayType.FIVE_ITEMS, false);
292292

293293
// when
294-
List<Object> listOfEmptyArr = ArraysMethodsDelegator.stream(emptyArr)
294+
List<Object> listOfEmptyArr = ArrayUtils.stream(emptyArr)
295295
.collect(Collectors.toList());
296-
String toStringViaStream = "[" + ArraysMethodsDelegator.stream(fiveArr)
296+
String toStringViaStream = "[" + ArrayUtils.stream(fiveArr)
297297
.map(String::valueOf)
298298
.collect(Collectors.joining(", ")) + "]";
299299

@@ -329,35 +329,35 @@ void shouldThrowForNonArrayInput() {
329329
Object object = new ArrayList<>();
330330

331331
// when / then
332-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.binarySearch(object, "test"));
333-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.binarySearch(object, 3, 4, "test"));
334-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.copyOf(object, 3));
335-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.copyOfRange(object, 3, 7));
336-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.equals(object, object));
337-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.fill(object, "obj"));
338-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.fill(object, 0, 2, "obj"));
339-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.hashCode(object));
340-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.parallelSort(object));
341-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.parallelSort(object, 0, 2));
342-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.stream(object));
343-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.sort(object));
344-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.sort(object, 0, 3));
345-
assertThrows(IllegalArgumentException.class, () -> ArraysMethodsDelegator.toString(object));
332+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.binarySearch(object, "test"));
333+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.binarySearch(object, 3, 4, "test"));
334+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.copyOf(object, 3));
335+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.copyOfRange(object, 3, 7));
336+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.equals(object, object));
337+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.fill(object, "obj"));
338+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.fill(object, 0, 2, "obj"));
339+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.hashCode(object));
340+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.parallelSort(object));
341+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.parallelSort(object, 0, 2));
342+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.stream(object));
343+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.sort(object));
344+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.sort(object, 0, 3));
345+
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.toString(object));
346346
}
347347

348348
@Test
349349
void shouldHaveMessageAboutTypeMismatch() {
350350
// given / when
351351
IllegalArgumentException iae1 = assertThrows(IllegalArgumentException.class,
352-
() -> ArraysMethodsDelegator.copyOf(BigDecimal.TEN, 2));
352+
() -> ArrayUtils.copyOf(BigDecimal.TEN, 2));
353353
NullPointerException npe2 = assertThrows(NullPointerException.class,
354-
() -> ArraysMethodsDelegator.hashCode(null));
354+
() -> ArrayUtils.hashCode(null));
355355
IllegalArgumentException iae3 = assertThrows(IllegalArgumentException.class,
356-
() -> ArraysMethodsDelegator.fill(new Object(), "t"));
356+
() -> ArrayUtils.fill(new Object(), "t"));
357357
ClassCastException cce4 = assertThrows(ClassCastException.class,
358-
() -> ArraysMethodsDelegator.fill(new int[]{3, 1, 4, 1}, "t"));
358+
() -> ArrayUtils.fill(new int[]{3, 1, 4, 1}, "t"));
359359
NullPointerException npe5 = assertThrows(NullPointerException.class,
360-
() -> ArraysMethodsDelegator.fill(new double[]{2.0, 1.85}, null));
360+
() -> ArrayUtils.fill(new double[]{2.0, 1.85}, null));
361361

362362
// then
363363
assertThat(iae1.getMessage(), equalTo("Expected an array as argument, but got: class java.math.BigDecimal"));
@@ -479,7 +479,7 @@ void shouldSortBooleanArrayWithinRange() {
479479
private void verifyFallbackSearchWithBinarySearch(boolean[] array, int from, int to, boolean value) {
480480
Boolean[] objArray = copyToReferenceBooleanArrayType(array);
481481

482-
int actualIndex = ArraysMethodsDelegator.simpleBooleanArrayBinarySearch(array, from, to, value);
482+
int actualIndex = ArrayUtils.simpleBooleanArrayBinarySearch(array, from, to, value);
483483
int expectedIndex = Arrays.binarySearch(objArray, from, to, value);
484484
if (expectedIndex >= 0) {
485485
assertThat(actualIndex, greaterThanOrEqualTo(0));
@@ -493,7 +493,7 @@ private void verifyFallbackSortWithObjectArraySort(boolean[] array, int from, in
493493
Boolean[] objArray = copyToReferenceBooleanArrayType(array);
494494

495495
Arrays.sort(objArray, from, to);
496-
ArraysMethodsDelegator.simpleBooleanArraySort(array, from, to);
496+
ArrayUtils.simpleBooleanArraySort(array, from, to);
497497

498498
if (array.length > 0) {
499499
Boolean[] arrayAfterSorting = copyToReferenceBooleanArrayType(array);

0 commit comments

Comments
 (0)