Skip to content

Commit 144c4e7

Browse files
committed
Add ArrayUtil.concat overloads for primitive types, re #61
1 parent 9bf54ae commit 144c4e7

File tree

8 files changed

+962
-274
lines changed

8 files changed

+962
-274
lines changed

src/main/java/org/libj/util/ArrayUtil.java

+396-53
Large diffs are not rendered by default.

src/main/java/org/libj/util/CollectionUtil.java

+357-197
Large diffs are not rendered by default.

src/main/java/org/libj/util/StringPaths.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,17 @@ public static String getName(final String path) {
433433
}
434434

435435
/**
436-
* Returns the simple name of the file or directory denoted by the specified pathname. This is just the last name in the name
437-
* sequence of {@code path}, with its dot-extension removed, if present. If the name sequence of {@code path} is empty, then the
438-
* empty string is returned.
436+
* Returns the last name in the {@code '/'}-delimited sequence of path segments of the given {@code path} with its dot-prefixed
437+
* extension removed. The unmodified {@linkplain StringPaths#getName(String) name of the path} is returned if no dot-prefixed
438+
* extension is present.
439439
*
440440
* @param path The path string.
441-
* @return The simple name of the file or directory denoted by the specified pathname, or the empty string if the name sequence of
442-
* {@code path} is empty.
441+
* @return The the last name in the {@code '/'}-delimited sequence of path segments of the given {@code path} with its dot-prefixed
442+
* extension removed.
443443
* @throws NullPointerException If {@code path} is null.
444444
* @throws IllegalArgumentException If {@code path} is an empty string.
445445
*/
446-
public static String getSimpleName(String path) {
446+
public static String getNameWithoutExtension(String path) {
447447
if (path.length() == 0)
448448
throw new IllegalArgumentException("Empty path");
449449

src/main/java/org/libj/util/Temporals.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.time.LocalDateTime;
2323
import java.time.LocalTime;
2424
import java.time.ZoneId;
25+
import java.time.ZoneOffset;
26+
import java.time.ZonedDateTime;
2527
import java.time.temporal.ChronoUnit;
2628
import java.time.temporal.Temporal;
2729
import java.util.Comparator;
@@ -219,13 +221,42 @@ public static LocalDateTime toLocalDateTime(final Date date) {
219221
* Returns a new {@link LocalDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
220222
* in the default time-zone.
221223
*
222-
* @param time Value representing milliseconds from the epoch.
224+
* @param epochTimeMs Value representing milliseconds from the epoch.
223225
* @return A new {@link LocalDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
224226
* in the default time-zone.
225227
* @throws DateTimeException If the result exceeds the supported range.
226228
*/
227-
public static LocalDateTime toLocalDateTime(final long time) {
228-
return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
229+
public static LocalDateTime toLocalDateTime(final long epochTimeMs) {
230+
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochTimeMs), ZoneId.systemDefault());
231+
}
232+
233+
/**
234+
* Returns a new {@link ZonedDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
235+
* in the provided {@link ZoneId}.
236+
*
237+
* @param epochTimeMs Value representing milliseconds from the epoch.
238+
* @param zoneId The time zone.
239+
* @return A new {@link ZonedDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
240+
* in the provided {@link ZoneId}.
241+
* @throws DateTimeException If the result exceeds the supported range.
242+
* @throws NullPointerException If {@code zoneId} is null.
243+
*/
244+
public static ZonedDateTime toZonedDateTime(final long epochTimeMs, final ZoneId zoneId) {
245+
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochTimeMs), zoneId);
246+
}
247+
248+
/**
249+
* Returns a new {@link ZonedDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
250+
* in the UTC time-zone.
251+
*
252+
* @param epochTimeMs Value representing milliseconds from the epoch.
253+
* @return A new {@link ZonedDateTime} of the instant specified by the {@code long} value representing milliseconds from the epoch,
254+
* in the UTC time-zone.
255+
* @throws DateTimeException If the result exceeds the supported range.
256+
* @throws NullPointerException If {@code zoneId} is null.
257+
*/
258+
public static ZonedDateTime toZonedDateTimeUTC(final long epochTimeMs) {
259+
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochTimeMs), ZoneOffset.UTC);
229260
}
230261

231262
/**

src/main/java/org/libj/util/retry/RetryPolicy.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.function.IntConsumer;
2727

2828
import org.libj.lang.Throwables;
29-
import org.libj.lang.WrappedArrayList;
29+
import org.libj.lang.ToArrayList;
3030
import org.libj.util.function.ThrowingRunnable;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
@@ -190,7 +190,7 @@ public Builder<E> clone() {
190190
}
191191
}
192192

193-
private static final ArrayList<Exception> testExceptions = new WrappedArrayList<>(new Exception());
193+
private static final ArrayList<Exception> testExceptions = new ToArrayList<>(new Exception());
194194

195195
private final int maxRetries;
196196
private final long maxDelayMs;

src/test/java/org/libj/util/ArrayUtilTest.java

+156-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static Object[] createRandomNestedArray() {
3838
public void testFlattenArrayRetainingReferences() {
3939
final Object[] array = {"a", "b", new Object[] {"c", "d"}, "e", new Object[] {"f", new Object[] {"g", "h"}}, "i"};
4040
final Object[] result = ArrayUtil.flatten(array);
41-
assertEquals("[" + java.util.Arrays.deepToString(array).replace("[", "").replace("]", "") + "]", java.util.Arrays.deepToString(result));
41+
assertEquals("[" + Arrays.deepToString(array).replace("[", "").replace("]", "") + "]", Arrays.deepToString(result));
4242

4343
final Object[] expected = {array[0], array[1], array[2], ((Object[])array[2])[0], ((Object[])array[2])[1], array[3], array[4], ((Object[])array[4])[0], ((Object[])array[4])[1], ((Object[])((Object[])array[4])[1])[0], ((Object[])((Object[])array[4])[1])[1], array[5]};
4444
final Object[] resultRetainingReferences = ArrayUtil.flatten(array, true);
@@ -50,7 +50,7 @@ public void testFlattenArray() {
5050
for (int i = 0; i < 100; ++i) { // [N]
5151
final Object[] array = createRandomNestedArray();
5252
final Object[] result = ArrayUtil.flatten(array);
53-
assertEquals("[" + java.util.Arrays.deepToString(array).replace("[", "").replace("]", "") + "]", java.util.Arrays.deepToString(result));
53+
assertEquals("[" + Arrays.deepToString(array).replace("[", "").replace("]", "") + "]", Arrays.deepToString(result));
5454
}
5555
}
5656

@@ -63,6 +63,8 @@ public void testBinaryClosestSearch() {
6363
catch (final NullPointerException e) {
6464
}
6565

66+
assertEquals(0, ArrayUtil.binaryClosestSearch(new int[0], 0));
67+
6668
final int[] sorted = {1, 3, 5, 9, 19};
6769
try {
6870
ArrayUtil.binaryClosestSearch(sorted, 4, 3, -10);
@@ -99,6 +101,158 @@ public void testFilter() {
99101
assertArrayEquals(expected, filtered);
100102
}
101103

104+
@Test
105+
public void testConcatBooleans() {
106+
final boolean[] a0 = {true, false};
107+
final boolean[] a1 = {false, true};
108+
final boolean[] a2 = {false};
109+
final boolean[] a3 = {};
110+
final boolean[] a4 = {true};
111+
final boolean[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
112+
assertArrayEquals(new boolean[] {true, false, false, true, false, true}, concat);
113+
}
114+
115+
@Test
116+
public void testConcatBytes() {
117+
final byte[] a0 = {'a', 'b'};
118+
final byte[] a1 = {'c', 'd'};
119+
final byte[] a2 = {'e'};
120+
final byte[] a3 = {};
121+
final byte[] a4 = {'f'};
122+
final byte[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
123+
assertArrayEquals(new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
124+
}
125+
126+
@Test
127+
public void testConcatChars() {
128+
final char[] a0 = {'a', 'b'};
129+
final char[] a1 = {'c', 'd'};
130+
final char[] a2 = {'e'};
131+
final char[] a3 = {};
132+
final char[] a4 = {'f'};
133+
final char[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
134+
assertArrayEquals(new char[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
135+
}
136+
137+
@Test
138+
public void testConcatShorts() {
139+
final short[] a0 = {'a', 'b'};
140+
final short[] a1 = {'c', 'd'};
141+
final short[] a2 = {'e'};
142+
final short[] a3 = {};
143+
final short[] a4 = {'f'};
144+
final short[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
145+
assertArrayEquals(new short[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
146+
}
147+
148+
@Test
149+
public void testConcatInts() {
150+
final int[] a0 = {'a', 'b'};
151+
final int[] a1 = {'c', 'd'};
152+
final int[] a2 = {'e'};
153+
final int[] a3 = {};
154+
final int[] a4 = {'f'};
155+
final int[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
156+
assertArrayEquals(new int[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
157+
}
158+
159+
@Test
160+
public void testConcatLongs() {
161+
final long[] a0 = {'a', 'b'};
162+
final long[] a1 = {'c', 'd'};
163+
final long[] a2 = {'e'};
164+
final long[] a3 = {};
165+
final long[] a4 = {'f'};
166+
final long[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
167+
assertArrayEquals(new long[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
168+
}
169+
170+
@Test
171+
public void testConcatFloats() {
172+
final float[] a0 = {'a', 'b'};
173+
final float[] a1 = {'c', 'd'};
174+
final float[] a2 = {'e'};
175+
final float[] a3 = {};
176+
final float[] a4 = {'f'};
177+
final float[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
178+
assertArrayEquals(new float[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat, 0);
179+
}
180+
181+
@Test
182+
public void testConcatDoubles() {
183+
final double[] a0 = {'a', 'b'};
184+
final double[] a1 = {'c', 'd'};
185+
final double[] a2 = {'e'};
186+
final double[] a3 = {};
187+
final double[] a4 = {'f'};
188+
final double[] concat = ArrayUtil.concat(a0, a1, a2, a3, a4);
189+
assertArrayEquals(new double[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat, 0);
190+
}
191+
192+
@Test
193+
public void testConcatBoolean() {
194+
final boolean[] one = {false, true, false};
195+
final boolean[] two = {true, true, false};
196+
final boolean[] concat = ArrayUtil.concat(one, two);
197+
assertArrayEquals(new boolean[] {false, true, false, true, true, false}, concat);
198+
}
199+
200+
@Test
201+
public void testConcatByte() {
202+
final byte[] one = {'a', 'b', 'c'};
203+
final byte[] two = {'d', 'e', 'f'};
204+
final byte[] concat = ArrayUtil.concat(one, two);
205+
assertArrayEquals(new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
206+
}
207+
208+
@Test
209+
public void testConcatChar() {
210+
final char[] one = {'a', 'b', 'c'};
211+
final char[] two = {'d', 'e', 'f'};
212+
final char[] concat = ArrayUtil.concat(one, two);
213+
assertArrayEquals(new char[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
214+
}
215+
216+
@Test
217+
public void testConcatShort() {
218+
final short[] one = {'a', 'b', 'c'};
219+
final short[] two = {'d', 'e', 'f'};
220+
final short[] concat = ArrayUtil.concat(one, two);
221+
assertArrayEquals(new short[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
222+
}
223+
224+
@Test
225+
public void testConcatInt() {
226+
final int[] one = {'a', 'b', 'c'};
227+
final int[] two = {'d', 'e', 'f'};
228+
final int[] concat = ArrayUtil.concat(one, two);
229+
assertArrayEquals(new int[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
230+
}
231+
232+
@Test
233+
public void testConcatLong() {
234+
final long[] one = {'a', 'b', 'c'};
235+
final long[] two = {'d', 'e', 'f'};
236+
final long[] concat = ArrayUtil.concat(one, two);
237+
assertArrayEquals(new long[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat);
238+
}
239+
240+
@Test
241+
public void testConcatFloat() {
242+
final float[] one = {'a', 'b', 'c'};
243+
final float[] two = {'d', 'e', 'f'};
244+
final float[] concat = ArrayUtil.concat(one, two);
245+
assertArrayEquals(new float[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat, 0);
246+
}
247+
248+
@Test
249+
public void testConcatDouble() {
250+
final double[] one = {'a', 'b', 'c'};
251+
final double[] two = {'d', 'e', 'f'};
252+
final double[] concat = ArrayUtil.concat(one, two);
253+
assertArrayEquals(new double[] {'a', 'b', 'c', 'd', 'e', 'f'}, concat, 0);
254+
}
255+
102256
@Test
103257
public void testConcat() {
104258
final String[] one = {"a", "b", "c"};

src/test/java/org/libj/util/PairedSortTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.Collections;
2323

2424
import org.junit.Test;
25-
import org.libj.lang.WrappedArrayList;
25+
import org.libj.lang.ToArrayList;
2626
import org.libj.util.primitive.IntComparator;
2727

2828
public class PairedSortTest {
@@ -75,7 +75,7 @@ private static <T> void test(final ArrayList<T> data, final IntComparator c) {
7575
@Test
7676
public void testExceptions() {
7777
try {
78-
test(new WrappedArrayList<>("a", "b", "c"), null);
78+
test(new ToArrayList<>("a", "b", "c"), null);
7979
fail("Expected NullPointerException");
8080
}
8181
catch (final NullPointerException e) {
@@ -84,7 +84,7 @@ public void testExceptions() {
8484

8585
@Test
8686
public void test26() {
87-
test(new WrappedArrayList<>("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));
87+
test(new ToArrayList<>("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));
8888
}
8989

9090
private static void testN(final int n) {

src/test/java/org/libj/util/StringPathsTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,25 @@ public void testGetName() {
142142
@Test
143143
public void testGetSimpleName() {
144144
try {
145-
StringPaths.getSimpleName(null);
145+
StringPaths.getNameWithoutExtension(null);
146146
fail("Expected NullPointerException");
147147
}
148148
catch (final NullPointerException e) {
149149
}
150150

151151
try {
152-
StringPaths.getSimpleName("");
152+
StringPaths.getNameWithoutExtension("");
153153
fail("Expected IllegalArgumentException");
154154
}
155155
catch (final IllegalArgumentException e) {
156156
}
157157

158-
assertEquals("share", StringPaths.getSimpleName("file:///usr/share/../share.txt"));
159-
assertEquals("lib", StringPaths.getSimpleName("file:///usr/share/../share/../lib"));
160-
assertEquals("var", StringPaths.getSimpleName("/usr/share/../share/../lib/../../var.tar.old"));
161-
assertEquals("var", StringPaths.getSimpleName("/usr/share/../share/../lib/../../var/"));
162-
assertEquals("resolv", StringPaths.getSimpleName("/etc/resolv.conf"));
163-
assertEquals("name", StringPaths.getSimpleName("name"));
158+
assertEquals("share", StringPaths.getNameWithoutExtension("file:///usr/share/../share.txt"));
159+
assertEquals("lib", StringPaths.getNameWithoutExtension("file:///usr/share/../share/../lib"));
160+
assertEquals("var", StringPaths.getNameWithoutExtension("/usr/share/../share/../lib/../../var.tar.old"));
161+
assertEquals("var", StringPaths.getNameWithoutExtension("/usr/share/../share/../lib/../../var/"));
162+
assertEquals("resolv", StringPaths.getNameWithoutExtension("/etc/resolv.conf"));
163+
assertEquals("name", StringPaths.getNameWithoutExtension("name"));
164164
}
165165

166166
@Test

0 commit comments

Comments
 (0)