Refactor XattrMetaInfo

This commit is contained in:
Reinhard Pointner 2016-05-10 20:04:11 +08:00
parent 3443fc0de7
commit e168f18dd4
1 changed files with 13 additions and 18 deletions

View File

@ -6,9 +6,9 @@ import static net.filebot.util.ExceptionUtilities.*;
import java.io.File;
import java.util.Locale;
import java.util.function.Function;
import net.filebot.Cache;
import net.filebot.Cache.Compute;
import net.filebot.CacheType;
import net.filebot.Resource;
import net.filebot.WebServices;
@ -54,36 +54,31 @@ public class XattrMetaInfo {
}
public synchronized Object getMetaInfo(File file) {
return getCachedValue(xattrMetaInfoCache, file, key -> {
return xattr(file).getObject();
});
return getXattrValue(xattrMetaInfoCache, file, MetaAttributes::getObject);
}
public synchronized String getOriginalName(File file) {
return (String) getCachedValue(xattrOriginalNameCache, file, key -> {
return xattr(file).getOriginalName();
});
return (String) getXattrValue(xattrOriginalNameCache, file, MetaAttributes::getOriginalName);
}
private MetaAttributes xattr(File file) throws Exception {
return new MetaAttributes(file);
}
private Object getCachedValue(Cache cache, File file, Compute<?> compute) {
private Object getXattrValue(Cache cache, File file, Function<MetaAttributes, Object> compute) {
// try in-memory cache of previously stored xattr metadata
if (!useExtendedFileAttributes) {
return cache.get(file);
}
try {
return cache.computeIfAbsent(file, key -> {
if (useExtendedFileAttributes) {
return compute.apply(key);
}
return null;
});
return cache.computeIfAbsent(file, element -> compute.apply(xattr(file)));
} catch (Throwable e) {
debug.warning("Failed to read xattr: " + getRootCauseMessage(e));
}
return null;
}
private MetaAttributes xattr(File file) throws Exception {
return new MetaAttributes(file);
}
public synchronized void setMetaInfo(File file, Object model, String original) {
// only for Episode / Movie objects
if (!isMetaInfo(model) || !file.isFile()) {