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<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 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) {
|
private SearchResult parseSeries(String[] v) {
|
||||||
int id = parseInt(v[0]);
|
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) {
|
protected <A> Resource<A[]> resource(String name, Duration expirationTime, Function<String, A> parse, IntFunction<A[]> generator) {
|
||||||
return () -> {
|
return () -> {
|
||||||
Cache cache = Cache.getCache("data", CacheType.Persistent);
|
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
|
// all data file are xz compressed
|
||||||
try (BufferedReader text = new BufferedReader(new InputStreamReader(new XZInputStream(new ByteArrayInputStream(bytes)), UTF_8))) {
|
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 static net.filebot.Logging.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class SystemProperty<T> {
|
public class SystemProperty<T> {
|
||||||
|
|
||||||
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction, T defaultValue) {
|
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) {
|
public static <T> SystemProperty<T> of(String key, Function<String, T> valueFunction) {
|
||||||
return new SystemProperty<T>(key, valueFunction, null);
|
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 String key;
|
||||||
private final Function<String, T> valueFunction;
|
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.key = key;
|
||||||
this.valueFunction = valueFunction;
|
this.valueFunction = valueFunction;
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValueFunction = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get() {
|
public T get() {
|
||||||
|
@ -36,16 +42,11 @@ public class SystemProperty<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValue;
|
return defaultValueFunction == null ? null : defaultValueFunction.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T orElse(T other) {
|
public Optional<T> optional() {
|
||||||
T value = get();
|
return Optional.ofNullable(get());
|
||||||
return value != null ? value : other;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(T value) {
|
|
||||||
System.setProperty(key, value.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue