Refactor SystemProperty
This commit is contained in:
parent
803413fb01
commit
1a9c7063ae
|
@ -450,7 +450,7 @@ public class ReleaseInfo {
|
|||
private final Resource<Movie[]> movieIndex = tsv("url.movie-list", Cache.ONE_MONTH, this::parseMovie, Movie[]::new);
|
||||
private final Resource<SubtitleSearchResult[]> osdbIndex = tsv("url.osdb-index", Cache.ONE_MONTH, this::parseSubtitle, SubtitleSearchResult[]::new);
|
||||
|
||||
private final SystemProperty<Duration> refreshDuration = SystemProperty.of("url.refresh", Duration::parse, null);
|
||||
private final SystemProperty<Duration> refreshDuration = SystemProperty.of("url.refresh", Duration::parse);
|
||||
|
||||
private SearchResult parseSeries(String[] v) {
|
||||
int id = parseInt(v[0]);
|
||||
|
@ -489,7 +489,7 @@ public class ReleaseInfo {
|
|||
protected <A> Resource<A[]> resource(String name, Duration expirationTime, Function<String, A> parse, IntFunction<A[]> generator) {
|
||||
return () -> {
|
||||
Cache cache = Cache.getCache("data", CacheType.Persistent);
|
||||
byte[] bytes = cache.bytes(name, n -> new URL(getProperty(n))).expire(refreshDuration.orElse(expirationTime)).get();
|
||||
byte[] bytes = cache.bytes(name, n -> new URL(getProperty(n))).expire(refreshDuration.optional().orElse(expirationTime)).get();
|
||||
|
||||
// all data file are xz compressed
|
||||
try (BufferedReader text = new BufferedReader(new InputStreamReader(new XZInputStream(new ByteArrayInputStream(bytes)), UTF_8))) {
|
||||
|
|
|
@ -2,27 +2,33 @@ package net.filebot.util;
|
|||
|
||||
import static net.filebot.Logging.*;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SystemProperty<T> {
|
||||
|
||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction, T defaultValue) {
|
||||
return new SystemProperty<T>(key, valueFunction, defaultValue);
|
||||
return new SystemProperty<T>(key, valueFunction, () -> defaultValue);
|
||||
}
|
||||
|
||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction) {
|
||||
return new SystemProperty<T>(key, valueFunction, null);
|
||||
}
|
||||
|
||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction, Supplier<T> defaultValue) {
|
||||
return new SystemProperty<T>(key, valueFunction, defaultValue);
|
||||
}
|
||||
|
||||
private final String key;
|
||||
private final Function<String, T> valueFunction;
|
||||
private final T defaultValue;
|
||||
private final Supplier<T> defaultValueFunction;
|
||||
|
||||
public SystemProperty(String key, Function<String, T> valueFunction, T defaultValue) {
|
||||
private SystemProperty(String key, Function<String, T> valueFunction, Supplier<T> defaultValue) {
|
||||
this.key = key;
|
||||
this.valueFunction = valueFunction;
|
||||
this.defaultValue = defaultValue;
|
||||
this.defaultValueFunction = defaultValue;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
|
@ -36,16 +42,11 @@ public class SystemProperty<T> {
|
|||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
return defaultValueFunction == null ? null : defaultValueFunction.get();
|
||||
}
|
||||
|
||||
public T orElse(T other) {
|
||||
T value = get();
|
||||
return value != null ? value : other;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
System.setProperty(key, value.toString());
|
||||
public Optional<T> optional() {
|
||||
return Optional.ofNullable(get());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue