-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvUtils.java
More file actions
36 lines (29 loc) · 1005 Bytes
/
Copy pathEnvUtils.java
File metadata and controls
36 lines (29 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ru.cwcode.cwutils.system;
import java.util.Optional;
import java.util.function.Function;
public class EnvUtils {
public static Optional<Integer> getInt(String key) {
return getEnv(key, Integer::parseInt);
}
public static Optional<Long> getLong(String key) {
return getEnv(key, Long::parseLong);
}
public static Optional<Double> getDouble(String key) {
return getEnv(key, Double::parseDouble);
}
public static Optional<String> getString(String key) {
return getEnv(key, Function.identity());
}
public static <E extends Enum<E>> Optional<E> getEnum(String key, Class<E> type) {
return getEnv(key, val -> Enum.valueOf(type, val));
}
public static <T> Optional<T> getEnv(String key, Function<String, T> converter) {
String value = System.getenv(key);
if (value == null) return Optional.empty();
try {
return Optional.ofNullable(converter.apply(value));
} catch (Exception e) {
return Optional.empty();
}
}
}