Skip to content

Commit ddda59e

Browse files
committed
update some utils methods, cover these methods by tests
1 parent d09daf0 commit ddda59e

8 files changed

Lines changed: 214 additions & 16 deletions

File tree

rlib-common/src/main/java/com/ss/rlib/common/util/ArrayUtils.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,14 @@ public static void copyTo(@NotNull final int[] source, final int[] target, final
485485
* @param object the object.
486486
* @return the object's index or -1.
487487
*/
488-
public static int indexOf(@NotNull final Object[] array, @Nullable final Object object) {
488+
public static int indexOf(@NotNull Object[] array, @Nullable Object object) {
489489

490490
int index = 0;
491491

492-
for (final Object element : array) {
493-
if (Objects.equals(element, object)) return index;
492+
for (var element : array) {
493+
if (Objects.equals(element, object)) {
494+
return index;
495+
}
494496
index++;
495497
}
496498

@@ -1885,6 +1887,38 @@ public static boolean isEmpty(@Nullable double[] array) {
18851887
return array == null || array.length == 0;
18861888
}
18871889

1890+
/**
1891+
* Create a new array with mapped each element from source array.
1892+
*
1893+
* @param source the source array.
1894+
* @param mapper the mapper.
1895+
* @param resultType the result component type.
1896+
* @param <T> the source component type.
1897+
* @param <R> the result component type.
1898+
* @return the mapped array or null.
1899+
* @since 9.3.0
1900+
*/
1901+
public static <T, R, M extends R> R @Nullable [] mapNullable(
1902+
T @Nullable [] source,
1903+
@NotNull Function<@NotNull T, @NotNull M> mapper,
1904+
@NotNull Class<R> resultType
1905+
) {
1906+
1907+
if (source == null) {
1908+
return null;
1909+
} else if (source.length == 0) {
1910+
return create(resultType, 0);
1911+
}
1912+
1913+
R[] resultArray = create(resultType, source.length);
1914+
1915+
for (int i = 0; i < source.length; i++) {
1916+
resultArray[i] = mapper.apply(source[i]);
1917+
}
1918+
1919+
return resultArray;
1920+
}
1921+
18881922
private ArrayUtils() {
18891923
throw new RuntimeException();
18901924
}

rlib-common/src/main/java/com/ss/rlib/common/util/NumberUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static short getShort(@NotNull byte[] bytes, int offset) {
9595
*
9696
* @param string the string to convert.
9797
* @return if the string is not null and can be converted to a long.
98+
* @since 9.3.0
9899
*/
99100
public static boolean isLong(@Nullable String string) {
100101

@@ -115,6 +116,7 @@ public static boolean isLong(@Nullable String string) {
115116
*
116117
* @param string the string to convert.
117118
* @return the long object or null.
119+
* @since 9.3.0
118120
*/
119121
public static @Nullable Long safeToLong(@Nullable String string) {
120122

@@ -134,6 +136,7 @@ public static boolean isLong(@Nullable String string) {
134136
*
135137
* @param string the string to convert.
136138
* @return the optional of long object.
139+
* @since 9.3.0
137140
*/
138141
public static @NotNull Optional<Long> toOptionalLong(@Nullable String string) {
139142

rlib-common/src/main/java/com/ss/rlib/common/util/StringUtils.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.rlib.common.util;
22

3+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56

@@ -8,7 +9,7 @@
89
import java.math.BigInteger;
910
import java.security.MessageDigest;
1011
import java.security.NoSuchAlgorithmException;
11-
import java.time.Instant;
12+
import java.time.*;
1213
import java.time.format.DateTimeFormatter;
1314
import java.time.temporal.TemporalAccessor;
1415
import java.util.concurrent.ThreadLocalRandom;
@@ -61,28 +62,39 @@ public class StringUtils {
6162
*
6263
* @param timestamp the timestamp.
6364
* @return the string presentation.
65+
* @since 9.3.0
6466
*/
65-
public static @NotNull String formatTimestamp(long timestamp) {
66-
return TIMESTAMP_FORMETTER.format(Instant.ofEpochMilli(timestamp));
67+
public static @NotNull String formatShortTimestamp(long timestamp) {
68+
return TIMESTAMP_FORMETTER.format(LocalDateTime.ofInstant(
69+
Instant.ofEpochMilli(timestamp),
70+
ZoneOffset.UTC
71+
));
6772
}
6873

6974
/**
7075
* Format some temporal accessor to a string by pattern HH:mm:ss:SSS
7176
*
7277
* @param temporal the timestamp.
7378
* @return the string presentation.
79+
* @since 9.3.0
7480
*/
75-
public static @NotNull String formatTimestamp(@NotNull TemporalAccessor temporal) {
81+
public static @NotNull String formatShortTimestamp(@NotNull TemporalAccessor temporal) {
7682
return TIMESTAMP_FORMETTER.format(temporal);
7783
}
7884

7985
/**
80-
* @see #isValidEmail(String)
86+
* Convert a date string to a {@link LocalDate}.
87+
*
88+
* @param string the string to convert.
89+
* @return the local date or null if this string cannot be converted.
90+
* @since 9.3.0
8191
*/
82-
@Deprecated(forRemoval = true)
83-
public static boolean checkEmail(@NotNull String email) {
84-
var matcher = EMAIL_PATTERN.matcher(email);
85-
return matcher.matches();
92+
public static @Nullable LocalDate toLocalDate(@Nullable String string) {
93+
if (StringUtils.isEmpty(string)) {
94+
return null;
95+
} else {
96+
return Utils.tryGetAndConvert(string, ISO_LOCAL_DATE::parse, LocalDate::from);
97+
}
8698
}
8799

88100
/**
@@ -179,16 +191,16 @@ public static boolean equalsIgnoreCase(@Nullable String first, @Nullable String
179191
*/
180192
public static @NotNull String toString(@NotNull Throwable throwable, int deepLevel) {
181193

182-
StringWriter writer = new StringWriter();
183-
PrintWriter printWriter = new PrintWriter(writer);
194+
var writer = new StringWriter();
195+
var printWriter = new PrintWriter(writer);
184196

185197
throwable.printStackTrace(printWriter);
186198

187-
StringBuilder stackTrace = new StringBuilder(writer.toString());
199+
var stackTrace = new StringBuilder(writer.toString());
188200

189201
int level = 0;
190202

191-
for (Throwable cause = throwable.getCause(); cause != null && level < deepLevel; cause = cause.getCause(), level++) {
203+
for (var cause = throwable.getCause(); cause != null && level < deepLevel; cause = cause.getCause(), level++) {
192204

193205
writer = new StringWriter();
194206
printWriter = new PrintWriter(writer);

rlib-common/src/main/java/com/ss/rlib/common/util/Utils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ public static <F, S> void unchecked(
255255
* @param argument the argument.
256256
* @param function the function.
257257
* @return the result or null.
258+
* @since 9.3.0
258259
*/
259260
public static <F, R> @Nullable R tryGet(
260261
@NotNull F argument,
@@ -276,6 +277,7 @@ public static <F, S> void unchecked(
276277
* @param argument the argument.
277278
* @param function the function.
278279
* @return the result or null.
280+
* @since 9.3.0
279281
*/
280282
public static <F, R> @NotNull R tryGet(
281283
@NotNull F argument,
@@ -299,6 +301,7 @@ public static <F, S> void unchecked(
299301
* @param function the function.
300302
* @param resultConverter the result converter.
301303
* @return the final result or null.
304+
* @since 9.3.0
302305
*/
303306
public static <F, R, FR> @Nullable FR tryGetAndConvert(
304307
@NotNull F argument,

rlib-common/src/test/java/com/ss/rlib/common/test/util/ArrayUtilsTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,25 @@ void convertStringToIntArrayTest() {
4646
Assertions.assertThrows(NumberFormatException.class,
4747
() -> ArrayUtils.toIntArray(" 1 ,2.5 , 3,4, 5", ","));
4848
}
49+
50+
@Test
51+
void mapNullableTest() {
52+
53+
String[] nullStrings = null;
54+
String[] emptyStrings = new String[0];
55+
String[] singleStrings = ArrayFactory.toArray("5");
56+
String[] strings = ArrayFactory.toArray("8", "1", "6");
57+
58+
Assertions.assertNull(ArrayUtils.mapNullable(nullStrings, Integer::parseInt, Integer.class));
59+
60+
Assertions.assertArrayEquals(
61+
new Integer[0], ArrayUtils.mapNullable(emptyStrings, Integer::parseInt, Integer.class)
62+
);
63+
Assertions.assertArrayEquals(
64+
ArrayFactory.toArray(5), ArrayUtils.mapNullable(singleStrings, Integer::parseInt, Integer.class)
65+
);
66+
Assertions.assertArrayEquals(
67+
ArrayFactory.toArray(8, 1, 6), ArrayUtils.mapNullable(strings, Integer::parseInt, Integer.class)
68+
);
69+
}
4970
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.ss.rlib.common.test.util;
2+
3+
import com.ss.rlib.common.util.NumberUtils;
4+
import com.ss.rlib.common.util.StringUtils;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.time.LocalDate;
9+
10+
/**
11+
* Test methods in {@link NumberUtils}
12+
*
13+
* @author JavaSaBr
14+
*/
15+
class NumberUtilsTest {
16+
17+
@Test
18+
void shouldCheckStringIsLong() {
19+
Assertions.assertTrue(NumberUtils.isLong("1"));
20+
Assertions.assertTrue(NumberUtils.isLong("123123213123"));
21+
Assertions.assertFalse(NumberUtils.isLong("notlong"));
22+
Assertions.assertFalse(NumberUtils.isLong(null));
23+
Assertions.assertFalse(NumberUtils.isLong("2.1234"));
24+
}
25+
26+
@Test
27+
void shouldSafetyConvertStringToLong() {
28+
Assertions.assertNotNull(NumberUtils.safeToLong("1"));
29+
Assertions.assertNotNull(NumberUtils.safeToLong("123123213123"));
30+
Assertions.assertNull(NumberUtils.safeToLong("notlong"));
31+
Assertions.assertNull(NumberUtils.safeToLong(null));
32+
Assertions.assertNull(NumberUtils.safeToLong("2.1234"));
33+
}
34+
35+
@Test
36+
void shouldConvertStringToOptionalLong() {
37+
Assertions.assertTrue(NumberUtils.toOptionalLong("1").isPresent());
38+
Assertions.assertTrue(NumberUtils.toOptionalLong("123123213123").isPresent());
39+
Assertions.assertFalse(NumberUtils.toOptionalLong("notlong").isPresent());
40+
Assertions.assertFalse(NumberUtils.toOptionalLong(null).isPresent());
41+
Assertions.assertFalse(NumberUtils.toOptionalLong("2.1234").isPresent());
42+
}
43+
}

rlib-common/src/test/java/com/ss/rlib/common/test/util/StringUtilsTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.jupiter.api.Assertions;
55
import org.junit.jupiter.api.Test;
66

7+
import java.time.*;
8+
79
/**
810
* Test methods in {@link com.ss.rlib.common.util.StringUtils}
911
*
@@ -118,4 +120,65 @@ void shouldDetectEmails() {
118120
Assertions.assertFalse(StringUtils.isEmail("test@test"));
119121
Assertions.assertFalse(StringUtils.isEmail("test@test."));
120122
}
123+
124+
@Test
125+
void convertToLocalDateTest() {
126+
127+
Assertions.assertEquals(
128+
LocalDate.of(1800, 5, 20),
129+
StringUtils.toLocalDate("1800-05-20")
130+
);
131+
132+
Assertions.assertNull(StringUtils.toLocalDate("2020-1-10"));
133+
Assertions.assertEquals(
134+
LocalDate.of(2020, 1, 10),
135+
StringUtils.toLocalDate("2020-01-10")
136+
);
137+
138+
Assertions.assertNull(StringUtils.toLocalDate("2015-5-1"));
139+
Assertions.assertEquals(
140+
LocalDate.of(2015, 5, 1),
141+
StringUtils.toLocalDate("2015-05-01")
142+
);
143+
144+
Assertions.assertNull(StringUtils.toLocalDate("invaliddate"));
145+
}
146+
147+
@Test
148+
void formatTimestampTest() {
149+
150+
var localDateTime = LocalDateTime.of(
151+
2010,
152+
5,
153+
12,
154+
23,
155+
10,
156+
35,
157+
0
158+
);
159+
160+
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(localDateTime));
161+
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(localDateTime
162+
.toInstant(ZoneOffset.UTC)
163+
.toEpochMilli())
164+
);
165+
166+
var zonedDateTime = ZonedDateTime.of(
167+
2010,
168+
5,
169+
12,
170+
23,
171+
10,
172+
35,
173+
0,
174+
ZoneOffset.ofHours(3)
175+
);
176+
177+
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(zonedDateTime));
178+
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(zonedDateTime
179+
.toLocalDateTime()
180+
.toInstant(ZoneOffset.UTC)
181+
.toEpochMilli())
182+
);
183+
}
121184
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
package com.ss.rlib.common.test.util;
22

3+
import com.ss.rlib.common.util.Utils;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
37
/**
48
* Tests of {@link com.ss.rlib.common.util.Utils} methods.
59
*
610
* @author JavaSaBr
711
*/
812
public class UtilsTests {
913

14+
@Test
15+
void shouldSafetyTryGet() {
16+
17+
Assertions.assertEquals(Integer.valueOf(15), Utils.tryGet("15", Integer::valueOf));
18+
Assertions.assertNull(Utils.tryGet("invalidnumber", Integer::valueOf));
19+
20+
Assertions.assertEquals(Integer.valueOf(15), Utils.tryGet("15", Integer::valueOf, 2));
21+
Assertions.assertEquals(Integer.valueOf(2), Utils.tryGet("invalidnumber", Integer::valueOf, 2));
22+
}
23+
24+
@Test
25+
void shouldSafetyTryGetAndConvert() {
26+
Assertions.assertEquals("15", Utils.tryGetAndConvert("15", Integer::valueOf, Object::toString));
27+
Assertions.assertNull(Utils.tryGetAndConvert("invalidnumber", Integer::valueOf, Object::toString));
28+
}
1029
}

0 commit comments

Comments
 (0)