Skip to content

Commit a073202

Browse files
committed
Use Optionals
1 parent ea1f97b commit a073202

File tree

5 files changed

+37
-50
lines changed

5 files changed

+37
-50
lines changed

src/main/java/xyz/srnyx/javautilities/HttpUtility.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import com.google.gson.JsonParser;
66

77
import org.jetbrains.annotations.NotNull;
8-
import org.jetbrains.annotations.Nullable;
98

109
import java.io.BufferedReader;
1110
import java.io.IOException;
1211
import java.io.InputStreamReader;
1312
import java.net.HttpURLConnection;
1413
import java.net.URI;
14+
import java.util.Optional;
1515
import java.util.function.Function;
1616
import java.util.stream.Collectors;
1717

@@ -31,21 +31,21 @@ public class HttpUtility {
3131
*
3232
* @return the result of the specified function, or null if the request failed
3333
*/
34-
@Nullable
35-
public static <T> T get(@NotNull String userAgent, @NotNull String url, Function<InputStreamReader, T> function) {
34+
@NotNull
35+
public static <T> Optional<T> get(@NotNull String userAgent, @NotNull String url, Function<InputStreamReader, T> function) {
3636
T result = null;
3737
HttpURLConnection connection = null;
3838
try {
3939
connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
4040
connection.setRequestMethod("GET");
4141
connection.setRequestProperty("User-Agent", userAgent);
42-
if (connection.getResponseCode() == 404) return null;
42+
if (connection.getResponseCode() == 404) return Optional.empty();
4343
result = function.apply(new InputStreamReader(connection.getInputStream()));
4444
} catch (final IOException ignored) {
4545
// Ignored
4646
}
4747
if (connection != null) connection.disconnect();
48-
return result;
48+
return Optional.ofNullable(result);
4949
}
5050

5151
/**
@@ -56,8 +56,8 @@ public static <T> T get(@NotNull String userAgent, @NotNull String url, Function
5656
*
5757
* @return the {@link String}, or null if the request failed
5858
*/
59-
@Nullable
60-
public static String getString(@NotNull String userAgent, @NotNull String urlString) {
59+
@NotNull
60+
public static Optional<String> getString(@NotNull String userAgent, @NotNull String urlString) {
6161
return get(userAgent, urlString, reader -> new BufferedReader(reader).lines().collect(Collectors.joining("\n")));
6262
}
6363

@@ -69,8 +69,8 @@ public static String getString(@NotNull String userAgent, @NotNull String urlStr
6969
*
7070
* @return the {@link JsonElement} retrieved from the specified URL
7171
*/
72-
@Nullable
73-
public static JsonElement getJson(@NotNull String userAgent, @NotNull String urlString) {
72+
@NotNull
73+
public static Optional<JsonElement> getJson(@NotNull String userAgent, @NotNull String urlString) {
7474
return get(userAgent, urlString, reader -> new JsonParser().parse(reader));
7575
}
7676

src/main/java/xyz/srnyx/javautilities/MiscUtility.java

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package xyz.srnyx.javautilities;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
54

65
import java.util.Arrays;
6+
import java.util.Optional;
77
import java.util.Set;
88
import java.util.function.Supplier;
99
import java.util.stream.Collectors;
@@ -22,27 +22,12 @@ public class MiscUtility {
2222
*
2323
* @param <R> the type of the result
2424
*/
25-
@Nullable
26-
public static <R> R handleException(@NotNull Supplier<R> supplier) {
27-
return handleException(supplier, null);
28-
}
29-
30-
/**
31-
* If an exception is thrown by the {@link Supplier}, the default value is returned
32-
*
33-
* @param supplier the {@link Supplier} to execute
34-
* @param def the default value to return if an exception is thrown
35-
*
36-
* @return the result of the {@link Supplier} or the default value
37-
*
38-
* @param <R> the type of the result
39-
*/
40-
@Nullable
41-
public static <R> R handleException(@NotNull Supplier<R> supplier, @Nullable R def) {
25+
@NotNull
26+
public static <R> Optional<R> handleException(@NotNull Supplier<R> supplier) {
4227
try {
43-
return supplier.get();
44-
} catch (final Exception e) {
45-
return def;
28+
return Optional.ofNullable(supplier.get());
29+
} catch (final Exception ignored) {
30+
return Optional.empty();
4631
}
4732
}
4833

src/main/java/xyz/srnyx/javautilities/manipulation/DurationParser.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package xyz.srnyx.javautilities.manipulation;
22

33
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
54

65
import java.time.Duration;
76
import java.time.temporal.ChronoUnit;
7+
import java.util.Optional;
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
1010
import java.util.stream.Collectors;
@@ -42,21 +42,21 @@ public class DurationParser {
4242
*
4343
* @return the parsed duration, or {@code null} if the string is invalid
4444
*/
45-
@Nullable
46-
public static Duration parse(@NotNull String input) {
45+
@NotNull
46+
public static Optional<Duration> parse(@NotNull String input) {
4747
final Matcher matcher = PATTERN.matcher(input);
48-
if (!matcher.matches()) return null;
48+
if (!matcher.matches()) return Optional.empty();
4949

5050
Duration duration = Duration.ZERO;
5151
for (int i = 0; i < DURATIONS_LENGTH; i++) {
5252
final String group = matcher.group(i + 1);
5353
if (group != null && !group.isEmpty()) {
54-
final Integer number = Mapper.toInt(group);
55-
if (number != null) duration = duration.plus(DURATIONS[i].multipliedBy(number));
54+
final Optional<Integer> number = Mapper.toInt(group);
55+
if (number.isPresent()) duration = duration.plus(DURATIONS[i].multipliedBy(number.get()));
5656
}
5757
}
5858

59-
return duration;
59+
return Optional.of(duration);
6060
}
6161

6262
private DurationParser() {

src/main/java/xyz/srnyx/javautilities/manipulation/Mapper.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import xyz.srnyx.javautilities.MiscUtility;
77

8+
import java.util.Optional;
9+
810

911
/**
1012
* A utility class for converting {@link Object Objects} to other types
@@ -20,9 +22,9 @@ public class Mapper {
2022
*
2123
* @param <T> the type of the {@link Class}
2224
*/
23-
@Nullable
24-
public static <T> T to(@Nullable Object object, @NotNull Class<T> clazz) {
25-
return !clazz.isInstance(object) ? null : MiscUtility.handleException(() -> clazz.cast(object));
25+
@NotNull
26+
public static <T> Optional<T> to(@Nullable Object object, @NotNull Class<T> clazz) {
27+
return !clazz.isInstance(object) ? Optional.empty() : MiscUtility.handleException(() -> clazz.cast(object));
2628
}
2729

2830
/**
@@ -32,9 +34,9 @@ public static <T> T to(@Nullable Object object, @NotNull Class<T> clazz) {
3234
*
3335
* @return the {@link Integer} or {@code null}
3436
*/
35-
@Nullable
36-
public static Integer toInt(@Nullable Object object) {
37-
return object == null ? null : MiscUtility.handleException(() -> Integer.parseInt(object.toString()));
37+
@NotNull
38+
public static Optional<Integer> toInt(@Nullable Object object) {
39+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Integer.parseInt(object.toString()));
3840
}
3941

4042
/**
@@ -44,9 +46,9 @@ public static Integer toInt(@Nullable Object object) {
4446
*
4547
* @return the {@link Double} or {@code null}
4648
*/
47-
@Nullable
48-
public static Double toDouble(@Nullable Object object) {
49-
return object == null ? null : MiscUtility.handleException(() -> Double.parseDouble(object.toString()));
49+
@NotNull
50+
public static Optional<Double> toDouble(@Nullable Object object) {
51+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Double.parseDouble(object.toString()));
5052
}
5153

5254
/**
@@ -56,9 +58,9 @@ public static Double toDouble(@Nullable Object object) {
5658
*
5759
* @return the {@link Long} or {@code null}
5860
*/
59-
@Nullable
60-
public static Long toLong(@Nullable Object object) {
61-
return object == null ? null : MiscUtility.handleException(() -> Long.parseLong(object.toString()));
61+
@NotNull
62+
public static Optional<Long> toLong(@Nullable Object object) {
63+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Long.parseLong(object.toString()));
6264
}
6365

6466
private Mapper() {

src/main/java/xyz/srnyx/javautilities/objects/SemanticVersion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public SemanticVersion(@NotNull String version) {
5252
this.patch = split.length > 2 ? Integer.parseInt(split[2]) : 0;
5353
}
5454

55-
@Override
55+
@Override @NotNull
5656
public String toString() {
5757
return version;
5858
}

0 commit comments

Comments
 (0)