More types less bugs

This commit is contained in:
Reinhard Pointner 2016-03-07 13:39:38 +00:00
parent e9f92a87e6
commit bc2b96d09b
2 changed files with 13 additions and 13 deletions

View File

@ -12,7 +12,7 @@ import java.util.Arrays;
import java.util.function.Predicate; import java.util.function.Predicate;
import net.filebot.CachedResource2.Fetch; import net.filebot.CachedResource2.Fetch;
import net.filebot.CachedResource2.Source; import net.filebot.CachedResource2.Resource;
import net.filebot.CachedResource2.Transform; import net.filebot.CachedResource2.Transform;
import net.filebot.web.FloodLimit; import net.filebot.web.FloodLimit;
import net.sf.ehcache.Element; import net.sf.ehcache.Element;
@ -28,7 +28,7 @@ public class Cache {
return CacheManager.getInstance().getCache(name.toLowerCase(), type); return CacheManager.getInstance().getCache(name.toLowerCase(), type);
} }
public <R> CachedResource2<String, R> resource(String key, Source<String> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime) { public <R> CachedResource2<String, R> resource(String key, Resource<String> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime) {
return new CachedResource2<String, R>(key, source, fetch, parse, cast, expirationTime, this); return new CachedResource2<String, R>(key, source, fetch, parse, cast, expirationTime, this);
} }
@ -36,11 +36,11 @@ public class Cache {
return new CachedResource2<String, String>(url, URL::new, withPermit(fetchIfModified(), r -> limit.acquirePermit() != null), getText(UTF_8), String.class::cast, expirationTime, this); return new CachedResource2<String, String>(url, URL::new, withPermit(fetchIfModified(), r -> limit.acquirePermit() != null), getText(UTF_8), String.class::cast, expirationTime, this);
} }
public CachedResource2<String, Document> xml(String key, Source<String> source, Duration expirationTime) { public CachedResource2<String, Document> xml(String key, Resource<String> source, Duration expirationTime) {
return new CachedResource2<String, Document>(key, source, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this); return new CachedResource2<String, Document>(key, source, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this);
} }
public CachedResource2<String, Object> json(String key, Source<String> source, Duration expirationTime) { public CachedResource2<String, Object> json(String key, Resource<String> source, Duration expirationTime) {
return new CachedResource2<String, Object>(key, source, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this); return new CachedResource2<String, Object>(key, source, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this);
} }

View File

@ -22,7 +22,7 @@ public class CachedResource2<K, R> {
private K key; private K key;
private Source<K> source; private Resource<K> source;
private Fetch fetch; private Fetch fetch;
private Transform<ByteBuffer, ? extends Object> parse; private Transform<ByteBuffer, ? extends Object> parse;
private Transform<? super Object, R> cast; private Transform<? super Object, R> cast;
@ -30,15 +30,15 @@ public class CachedResource2<K, R> {
private Duration expirationTime; private Duration expirationTime;
private int retryCountLimit; private int retryCountLimit;
private long retryWaitTime; private Duration retryWaitTime;
private final Cache cache; private final Cache cache;
public CachedResource2(K key, Source<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime, Cache cache) { public CachedResource2(K key, Resource<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, Duration expirationTime, Cache cache) {
this(key, source, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache); this(key, source, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache);
} }
public CachedResource2(K key, Source<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, int retryCountLimit, Duration retryWaitTime, Duration expirationTime, Cache cache) { public CachedResource2(K key, Resource<K> source, Fetch fetch, Transform<ByteBuffer, ? extends Object> parse, Transform<? super Object, R> cast, int retryCountLimit, Duration retryWaitTime, Duration expirationTime, Cache cache) {
this.key = key; this.key = key;
this.source = source; this.source = source;
this.fetch = fetch; this.fetch = fetch;
@ -46,7 +46,7 @@ public class CachedResource2<K, R> {
this.cast = cast; this.cast = cast;
this.expirationTime = expirationTime; this.expirationTime = expirationTime;
this.retryCountLimit = retryCountLimit; this.retryCountLimit = retryCountLimit;
this.retryWaitTime = retryWaitTime.toMillis(); this.retryWaitTime = retryWaitTime;
this.cache = cache; this.cache = cache;
} }
@ -80,7 +80,7 @@ public class CachedResource2<K, R> {
return cast.transform(value); return cast.transform(value);
} }
protected <T> T retry(Callable<T> callable, int retryCount, long retryWaitTime) throws Exception { protected <T> T retry(Callable<T> callable, int retryCount, Duration retryWaitTime) throws Exception {
try { try {
return callable.call(); return callable.call();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -91,13 +91,13 @@ public class CachedResource2<K, R> {
if (retryCount > 0) { if (retryCount > 0) {
throw e; throw e;
} }
Thread.sleep(retryWaitTime); Thread.sleep(retryWaitTime.toMillis());
return retry(callable, retryCount - 1, retryWaitTime * 2); return retry(callable, retryCount - 1, retryWaitTime.multipliedBy(2));
} }
} }
@FunctionalInterface @FunctionalInterface
public interface Source<K> { public interface Resource<K> {
URL source(K key) throws Exception; URL source(K key) throws Exception;
} }