Skip to content

Commit 6807501

Browse files
committed
Finish 9.3.0
2 parents 413c2ca + df1f9ee commit 6807501

14 files changed

Lines changed: 492 additions & 585 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ repositories {
1616
}
1717
1818
dependencies {
19-
compile 'com.spaceshift:rlib.common:9.2.1'
20-
compile 'com.spaceshift:rlib.fx:9.2.1'
21-
compile 'com.spaceshift:rlib.network:9.2.1'
22-
compile 'com.spaceshift:rlib.mail:9.2.1'
23-
compile 'com.spaceshift:rlib.testcontainers:9.2.1'
19+
compile 'com.spaceshift:rlib.common:9.3.0'
20+
compile 'com.spaceshift:rlib.fx:9.3.0'
21+
compile 'com.spaceshift:rlib.network:9.3.0'
22+
compile 'com.spaceshift:rlib.mail:9.3.0'
23+
compile 'com.spaceshift:rlib.testcontainers:9.3.0'
2424
}
2525
```
2626

@@ -41,27 +41,27 @@ dependencies {
4141
<dependency>
4242
<groupId>com.spaceshift</groupId>
4343
<artifactId>rlib.common</artifactId>
44-
<version>9.2.1</version>
44+
<version>9.3.0</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>com.spaceshift</groupId>
4848
<artifactId>rlib.fx</artifactId>
49-
<version>9.2.1</version>
49+
<version>9.3.0</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>com.spaceshift</groupId>
5353
<artifactId>rlib.network</artifactId>
54-
<version>9.2.1</version>
54+
<version>9.3.0</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>com.spaceshift</groupId>
5858
<artifactId>rlib.mail</artifactId>
59-
<version>9.2.1</version>
59+
<version>9.3.0</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>com.spaceshift</groupId>
6363
<artifactId>rlib.testcontainers</artifactId>
64-
<version>9.2.1</version>
64+
<version>9.3.0</version>
6565
</dependency>
6666

6767
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010

11-
rootProject.version = '9.2.1'
11+
rootProject.version = '9.3.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

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

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ public static <T> void fill(@NotNull T[] array, @NotNull IntFunction<T> factory)
158158
/**
159159
* Fill the array using the factory.
160160
*
161+
* @param <T> the element's type.
162+
* @param <F> the argument's type.
161163
* @param array the array.
162164
* @param argument the additional argument.
163165
* @param factory the element's factory.
164-
* @param <T> the element's type.
165166
*/
166167
public static <T, F> void fill(@NotNull T[] array, @Nullable F argument, @NotNull Function<F, T> factory) {
167168
for (int i = 0; i < array.length; i++) {
@@ -485,12 +486,14 @@ public static void copyTo(@NotNull final int[] source, final int[] target, final
485486
* @param object the object.
486487
* @return the object's index or -1.
487488
*/
488-
public static int indexOf(@NotNull final Object[] array, @Nullable final Object object) {
489+
public static int indexOf(@NotNull Object[] array, @Nullable Object object) {
489490

490491
int index = 0;
491492

492-
for (final Object element : array) {
493-
if (Objects.equals(element, object)) return index;
493+
for (var element : array) {
494+
if (Objects.equals(element, object)) {
495+
return index;
496+
}
494497
index++;
495498
}
496499

@@ -575,6 +578,7 @@ public static <T> void sort(@NotNull final T[] array, final int fromIndex, final
575578
/**
576579
* Convert the array to a string presentation.
577580
*
581+
* @param <T> the element's type.
578582
* @param array the array.
579583
* @param toString the to string function.
580584
* @return the string presentation.
@@ -585,11 +589,11 @@ public static <T> void sort(@NotNull final T[] array, final int fromIndex, final
585589
return "[]";
586590
}
587591

588-
String className = array.array()
592+
var className = array.array()
589593
.getClass()
590594
.getSimpleName();
591595

592-
StringBuilder builder = new StringBuilder(className.substring(0, className.length() - 1));
596+
var builder = new StringBuilder(className.substring(0, className.length() - 1));
593597

594598
for (int i = 0, length = array.size() - 1; i <= length; i++) {
595599

@@ -1885,6 +1889,61 @@ public static boolean isEmpty(@Nullable double[] array) {
18851889
return array == null || array.length == 0;
18861890
}
18871891

1892+
/**
1893+
* Convert T array to R array if a source array is not null.
1894+
*
1895+
* @param <T> the source component type.
1896+
* @param <M> the mapped element's type.
1897+
* @param <R> the result element's type.
1898+
* @param source the source array.
1899+
* @param mapper the mapper.
1900+
* @param resultType the result component type.
1901+
* @return the mapped array or null.
1902+
* @since 9.3.0
1903+
*/
1904+
public static <T, R, M extends R> R @Nullable [] mapNullable(
1905+
T @Nullable [] source,
1906+
@NotNull Function<@NotNull T, @NotNull M> mapper,
1907+
@NotNull Class<R> resultType
1908+
) {
1909+
1910+
if (source == null) {
1911+
return null;
1912+
} else if (source.length == 0) {
1913+
return create(resultType, 0);
1914+
}
1915+
1916+
R[] resultArray = create(resultType, source.length);
1917+
1918+
for (int i = 0; i < source.length; i++) {
1919+
resultArray[i] = mapper.apply(source[i]);
1920+
}
1921+
1922+
return resultArray;
1923+
}
1924+
1925+
/**
1926+
* Convert long array to int array.
1927+
*
1928+
* @param source the source array.
1929+
* @return the int array.
1930+
* @since 9.3.0
1931+
*/
1932+
public static int @NotNull [] longsToInts(long @NotNull [] source) {
1933+
1934+
if (source.length == 0) {
1935+
return ArrayUtils.EMPTY_INT_ARRAY;
1936+
}
1937+
1938+
int[] resultArray = new int[source.length];
1939+
1940+
for (int i = 0; i < source.length; i++) {
1941+
resultArray[i] = (int) source[i];
1942+
}
1943+
1944+
return resultArray;
1945+
}
1946+
18881947
private ArrayUtils() {
18891948
throw new RuntimeException();
18901949
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.ss.rlib.common.util;
2+
3+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.time.Instant;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
import java.time.ZoneOffset;
11+
import java.time.format.DateTimeFormatter;
12+
import java.time.temporal.TemporalAccessor;
13+
14+
/**
15+
* The class with utility methods to work with dates and times.
16+
*
17+
* @author JavaSaBr
18+
*/
19+
public class DateUtils {
20+
21+
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
22+
DateTimeFormatter.ofPattern("HH:mm:ss:SSS");
23+
24+
/**
25+
* Format a time to a string by pattern HH:mm:ss:SSS
26+
*
27+
* @param timestamp the timestamp.
28+
* @return the string presentation.
29+
* @since 9.3.0
30+
*/
31+
public static @NotNull String formatShortTimestamp(long timestamp) {
32+
return TIMESTAMP_FORMATTER.format(LocalDateTime.ofInstant(
33+
Instant.ofEpochMilli(timestamp),
34+
ZoneOffset.UTC
35+
));
36+
}
37+
38+
/**
39+
* Format some temporal accessor to a string by pattern HH:mm:ss:SSS
40+
*
41+
* @param temporal the timestamp.
42+
* @return the string presentation.
43+
* @since 9.3.0
44+
*/
45+
public static @NotNull String formatShortTimestamp(@NotNull TemporalAccessor temporal) {
46+
return TIMESTAMP_FORMATTER.format(temporal);
47+
}
48+
49+
/**
50+
* Convert a date string to a {@link LocalDate}.
51+
*
52+
* @param string the string to convert.
53+
* @return the local date or null if this string cannot be converted.
54+
* @since 9.3.0
55+
*/
56+
public static @Nullable LocalDate toLocalDate(@Nullable String string) {
57+
if (StringUtils.isEmpty(string)) {
58+
return null;
59+
} else {
60+
return Utils.tryGetAndConvert(string, ISO_LOCAL_DATE::parse, LocalDate::from);
61+
}
62+
}
63+
64+
/**
65+
* Convert a local date to a string by ISO_LOCAL_DATE formatter.
66+
*
67+
* @param localDate the local date.
68+
* @return the string presentation of the local date or null.
69+
* @since 9.3.0
70+
*/
71+
public static @Nullable String toString(@Nullable LocalDate localDate) {
72+
if (localDate == null) {
73+
return null;
74+
} else {
75+
return ISO_LOCAL_DATE.format(localDate);
76+
}
77+
}
78+
79+
/**
80+
* Convert a temporal accessor to a string by passed formatter.
81+
*
82+
* @param temporalAccessor the temporal accessor.
83+
* @param formatter the formatter.
84+
* @return the string presentation or null.
85+
* @since 9.3.0
86+
*/
87+
public static @Nullable String toString(
88+
@Nullable TemporalAccessor temporalAccessor,
89+
@NotNull DateTimeFormatter formatter
90+
) {
91+
if (temporalAccessor == null) {
92+
return null;
93+
} else {
94+
return formatter.format(temporalAccessor);
95+
}
96+
}
97+
}

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

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

33
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.Optional;
47

58
/**
69
* The utility class.
@@ -87,6 +90,67 @@ public static short getShort(@NotNull byte[] bytes, int offset) {
8790
return (short) (bytes[offset + 1] << 8 | bytes[offset] & 0xff);
8891
}
8992

93+
/**
94+
* Return true if a string is not null and can be converted to a long.
95+
*
96+
* @param string the string to convert.
97+
* @return if the string is not null and can be converted to a long.
98+
* @since 9.3.0
99+
*/
100+
public static boolean isLong(@Nullable String string) {
101+
102+
if (string == null) {
103+
return false;
104+
} else {
105+
try {
106+
Long.parseLong(string);
107+
return true;
108+
} catch (NumberFormatException e) {
109+
return false;
110+
}
111+
}
112+
}
113+
114+
/**
115+
* Convert a string to long object or null if this string is null or not a number.
116+
*
117+
* @param string the string to convert.
118+
* @return the long object or null.
119+
* @since 9.3.0
120+
*/
121+
public static @Nullable Long safeToLong(@Nullable String string) {
122+
123+
if (string == null) {
124+
return null;
125+
} else {
126+
try {
127+
return Long.valueOf(string);
128+
} catch (NumberFormatException e) {
129+
return null;
130+
}
131+
}
132+
}
133+
134+
/**
135+
* Convert a string to long object.
136+
*
137+
* @param string the string to convert.
138+
* @return the optional of long object.
139+
* @since 9.3.0
140+
*/
141+
public static @NotNull Optional<Long> toOptionalLong(@Nullable String string) {
142+
143+
if (string == null) {
144+
return Optional.empty();
145+
} else {
146+
try {
147+
return Optional.of(Long.valueOf(string));
148+
} catch (NumberFormatException e) {
149+
return Optional.empty();
150+
}
151+
}
152+
}
153+
90154
private NumberUtils() {
91155
throw new IllegalArgumentException();
92156
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717
public final class ObjectUtils {
1818

1919
/**
20+
* @param <T> the object's type.
21+
* @param obj the object to check on not null.
22+
* @param message the message for exception if the object is null.
23+
* @return the passed object if it is not null.
2024
* @see Objects#requireNonNull(Object, String)
2125
*/
2226
public static <T> @NotNull T notNull(@Nullable T obj, @NotNull String message) {
2327
return Objects.requireNonNull(obj, message);
2428
}
2529

2630
/**
31+
* @param <T> the object's type.
32+
* @param obj the object to check on not null.
33+
* @return the passed object if it is not null.
2734
* @see Objects#requireNonNull(Object)
2835
*/
2936
public static <T> @NotNull T notNull(@Nullable T obj) {
@@ -33,6 +40,7 @@ public final class ObjectUtils {
3340
/**
3441
* Check the object to be not null. If the object is null this method throws an exception from the supplier.
3542
*
43+
* @param <T> the object's type.
3644
* @param obj the checked object.
3745
* @param supplier the exception factory.
3846
* @return the object.
@@ -50,6 +58,8 @@ public final class ObjectUtils {
5058
/**
5159
* Check the object to be not null. If the object is null this method throws an exception from the factory.
5260
*
61+
* @param <T> the object's type.
62+
* @param <F> the argument's type.
5363
* @param obj the checked object.
5464
* @param arg the argument for the exception factory.
5565
* @param factory the exception factory.
@@ -72,6 +82,7 @@ public final class ObjectUtils {
7282
/**
7383
* Check the object to be not null. If the object is null this method throws an exception from the factory.
7484
*
85+
* @param <T> the object's type.
7586
* @param obj the checked object.
7687
* @param arg the argument for the exception factory.
7788
* @param factory the exception factory.

0 commit comments

Comments
 (0)