Skip to content

Commit d09daf0

Browse files
committed
update some utils methods
1 parent ffd5f3b commit d09daf0

4 files changed

Lines changed: 106 additions & 554 deletions

File tree

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/NumberUtils.java

Lines changed: 61 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,64 @@ 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+
*/
99+
public static boolean isLong(@Nullable String string) {
100+
101+
if (string == null) {
102+
return false;
103+
} else {
104+
try {
105+
Long.parseLong(string);
106+
return true;
107+
} catch (NumberFormatException e) {
108+
return false;
109+
}
110+
}
111+
}
112+
113+
/**
114+
* Convert a string to long object or null if this string is null or not a number.
115+
*
116+
* @param string the string to convert.
117+
* @return the long object or null.
118+
*/
119+
public static @Nullable Long safeToLong(@Nullable String string) {
120+
121+
if (string == null) {
122+
return null;
123+
} else {
124+
try {
125+
return Long.valueOf(string);
126+
} catch (NumberFormatException e) {
127+
return null;
128+
}
129+
}
130+
}
131+
132+
/**
133+
* Convert a string to long object.
134+
*
135+
* @param string the string to convert.
136+
* @return the optional of long object.
137+
*/
138+
public static @NotNull Optional<Long> toOptionalLong(@Nullable String string) {
139+
140+
if (string == null) {
141+
return Optional.empty();
142+
} else {
143+
try {
144+
return Optional.of(Long.valueOf(string));
145+
} catch (NumberFormatException e) {
146+
return Optional.empty();
147+
}
148+
}
149+
}
150+
90151
private NumberUtils() {
91152
throw new IllegalArgumentException();
92153
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import java.math.BigInteger;
99
import java.security.MessageDigest;
1010
import java.security.NoSuchAlgorithmException;
11+
import java.time.Instant;
12+
import java.time.format.DateTimeFormatter;
13+
import java.time.temporal.TemporalAccessor;
1114
import java.util.concurrent.ThreadLocalRandom;
12-
import java.util.regex.Matcher;
1315
import java.util.regex.Pattern;
1416

1517
/**
@@ -30,6 +32,9 @@ public class StringUtils {
3032
private static final ThreadLocal<MessageDigest> LOCAL_HASH_MD =
3133
ThreadLocal.withInitial(StringUtils::getHashMD5);
3234

35+
private static final DateTimeFormatter TIMESTAMP_FORMETTER =
36+
DateTimeFormatter.ofPattern("HH:mm:ss:SSS");
37+
3338
/**
3439
* Return an empty string if the received string is null.
3540
*
@@ -51,10 +56,30 @@ public class StringUtils {
5156
return isEmpty(string) ? another : string;
5257
}
5358

59+
/**
60+
* Format a time to a string by pattern HH:mm:ss:SSS
61+
*
62+
* @param timestamp the timestamp.
63+
* @return the string presentation.
64+
*/
65+
public static @NotNull String formatTimestamp(long timestamp) {
66+
return TIMESTAMP_FORMETTER.format(Instant.ofEpochMilli(timestamp));
67+
}
68+
69+
/**
70+
* Format some temporal accessor to a string by pattern HH:mm:ss:SSS
71+
*
72+
* @param temporal the timestamp.
73+
* @return the string presentation.
74+
*/
75+
public static @NotNull String formatTimestamp(@NotNull TemporalAccessor temporal) {
76+
return TIMESTAMP_FORMETTER.format(temporal);
77+
}
78+
5479
/**
5580
* @see #isValidEmail(String)
5681
*/
57-
@Deprecated
82+
@Deprecated(forRemoval = true)
5883
public static boolean checkEmail(@NotNull String email) {
5984
var matcher = EMAIL_PATTERN.matcher(email);
6085
return matcher.matches();

0 commit comments

Comments
 (0)