From 1a9c7063aee2cba8c07277a0807fe4896c6ec631 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 18 Oct 2016 02:22:07 +0800 Subject: [PATCH] Refactor SystemProperty --- source/net/filebot/media/ReleaseInfo.java | 4 ++-- source/net/filebot/util/SystemProperty.java | 25 +++++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/source/net/filebot/media/ReleaseInfo.java b/source/net/filebot/media/ReleaseInfo.java index 3ada9d1a..e72ef285 100644 --- a/source/net/filebot/media/ReleaseInfo.java +++ b/source/net/filebot/media/ReleaseInfo.java @@ -450,7 +450,7 @@ public class ReleaseInfo { private final Resource movieIndex = tsv("url.movie-list", Cache.ONE_MONTH, this::parseMovie, Movie[]::new); private final Resource osdbIndex = tsv("url.osdb-index", Cache.ONE_MONTH, this::parseSubtitle, SubtitleSearchResult[]::new); - private final SystemProperty refreshDuration = SystemProperty.of("url.refresh", Duration::parse, null); + private final SystemProperty 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 Resource resource(String name, Duration expirationTime, Function parse, IntFunction 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))) { diff --git a/source/net/filebot/util/SystemProperty.java b/source/net/filebot/util/SystemProperty.java index 10e4aa67..1022d5ee 100644 --- a/source/net/filebot/util/SystemProperty.java +++ b/source/net/filebot/util/SystemProperty.java @@ -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 { public static SystemProperty of(String key, Function valueFunction, T defaultValue) { - return new SystemProperty(key, valueFunction, defaultValue); + return new SystemProperty(key, valueFunction, () -> defaultValue); } public static SystemProperty of(String key, Function valueFunction) { return new SystemProperty(key, valueFunction, null); } + public static SystemProperty of(String key, Function valueFunction, Supplier defaultValue) { + return new SystemProperty(key, valueFunction, defaultValue); + } + private final String key; private final Function valueFunction; - private final T defaultValue; + private final Supplier defaultValueFunction; - public SystemProperty(String key, Function valueFunction, T defaultValue) { + private SystemProperty(String key, Function valueFunction, Supplier defaultValue) { this.key = key; this.valueFunction = valueFunction; - this.defaultValue = defaultValue; + this.defaultValueFunction = defaultValue; } public T get() { @@ -36,16 +42,11 @@ public class SystemProperty { } } - 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 optional() { + return Optional.ofNullable(get()); } }