From 6c98f249d1b7f858a5782264ab9a44196aeefa28 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 9 Oct 2016 03:14:09 +0800 Subject: [PATCH] Minor optimizations --- source/net/filebot/media/MetaAttributes.java | 22 ++++++++++--------- source/net/filebot/media/XattrMetaInfo.java | 16 +------------- .../net/filebot/ui/rename/BindingDialog.java | 2 +- source/net/filebot/ui/rename/RenamePanel.java | 13 ++++++----- source/net/filebot/util/JsonUtilities.java | 8 ++----- source/net/filebot/web/TheTVDBClient.java | 4 ++-- 6 files changed, 26 insertions(+), 39 deletions(-) diff --git a/source/net/filebot/media/MetaAttributes.java b/source/net/filebot/media/MetaAttributes.java index 158f4baf..c37b8a9a 100644 --- a/source/net/filebot/media/MetaAttributes.java +++ b/source/net/filebot/media/MetaAttributes.java @@ -1,6 +1,7 @@ package net.filebot.media; import static java.util.Collections.*; +import static java.util.stream.Collectors.*; import java.io.File; import java.io.IOException; @@ -9,31 +10,32 @@ import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.FileTime; import java.util.HashMap; import java.util.Map; +import java.util.stream.Stream; import com.cedarsoftware.util.io.JsonReader; import com.cedarsoftware.util.io.JsonWriter; import net.filebot.MetaAttributeView; +import net.filebot.vfs.SimpleFileInfo; +import net.filebot.web.AudioTrack; +import net.filebot.web.Episode; +import net.filebot.web.Movie; +import net.filebot.web.MoviePart; +import net.filebot.web.MultiEpisode; public class MetaAttributes { public static final String FILENAME_KEY = "net.filebot.filename"; public static final String METADATA_KEY = "net.filebot.metadata"; + public static final Map JSON_TYPE_MAP = unmodifiableMap(Stream.of(Episode.class, MultiEpisode.class, Movie.class, MoviePart.class, AudioTrack.class, SimpleFileInfo.class).collect(toMap(Class::getName, Class::getSimpleName))); + private final BasicFileAttributeView fileAttributeView; private final MetaAttributeView metaAttributeView; - private final Map jsonTypeMap; - - // compatibility constructor for sysinfo.groovy script public MetaAttributes(File file) throws IOException { - this(file, emptyMap()); - } - - public MetaAttributes(File file, Map jsonTypeMap) throws IOException { this.metaAttributeView = new MetaAttributeView(file); this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); - this.jsonTypeMap = jsonTypeMap; } public void setCreationDate(long millis) throws IOException { @@ -58,7 +60,7 @@ public class MetaAttributes { public void setObject(Object object) { try { - metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, jsonTypeMap))); + metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, JSON_TYPE_MAP))); } catch (Exception e) { throw new RuntimeException(e); } @@ -69,7 +71,7 @@ public class MetaAttributes { String jsonObject = metaAttributeView.get(METADATA_KEY); if (jsonObject != null && jsonObject.length() > 0) { Map options = new HashMap(2); - options.put(JsonReader.TYPE_NAME_MAP, jsonTypeMap); + options.put(JsonReader.TYPE_NAME_MAP, JSON_TYPE_MAP); // options must be a modifiable map return JsonReader.jsonToJava(jsonObject, options); diff --git a/source/net/filebot/media/XattrMetaInfo.java b/source/net/filebot/media/XattrMetaInfo.java index 94fc27ce..0b4e0aa4 100644 --- a/source/net/filebot/media/XattrMetaInfo.java +++ b/source/net/filebot/media/XattrMetaInfo.java @@ -1,27 +1,19 @@ package net.filebot.media; -import static java.util.Collections.*; -import static java.util.stream.Collectors.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.util.ExceptionUtilities.*; import java.io.File; import java.util.Locale; -import java.util.Map; import java.util.function.Function; -import java.util.stream.Stream; import net.filebot.Cache; import net.filebot.CacheType; import net.filebot.Resource; import net.filebot.WebServices; -import net.filebot.vfs.SimpleFileInfo; -import net.filebot.web.AudioTrack; import net.filebot.web.Episode; import net.filebot.web.Movie; -import net.filebot.web.MoviePart; -import net.filebot.web.MultiEpisode; import net.filebot.web.SimpleDate; public class XattrMetaInfo { @@ -34,8 +26,6 @@ public class XattrMetaInfo { private final Cache xattrMetaInfoCache = Cache.getCache(MetaAttributes.METADATA_KEY, CacheType.Ephemeral); private final Cache xattrOriginalNameCache = Cache.getCache(MetaAttributes.FILENAME_KEY, CacheType.Ephemeral); - private final Map jsonTypeMap = unmodifiableMap(Stream.of(Episode.class, MultiEpisode.class, Movie.class, MoviePart.class, AudioTrack.class, SimpleFileInfo.class).collect(toMap(Class::getName, Class::getSimpleName))); - public XattrMetaInfo(boolean useExtendedFileAttributes, boolean useCreationDate) { this.useExtendedFileAttributes = useExtendedFileAttributes; this.useCreationDate = useCreationDate; @@ -86,11 +76,7 @@ public class XattrMetaInfo { } private MetaAttributes xattr(File file) throws Exception { - return new MetaAttributes(file, getJsonTypeMap()); - } - - public Map getJsonTypeMap() { - return jsonTypeMap; + return new MetaAttributes(file); } public synchronized void setMetaInfo(File file, Object model, String original) { diff --git a/source/net/filebot/ui/rename/BindingDialog.java b/source/net/filebot/ui/rename/BindingDialog.java index 2f00076c..f4e888e0 100644 --- a/source/net/filebot/ui/rename/BindingDialog.java +++ b/source/net/filebot/ui/rename/BindingDialog.java @@ -228,7 +228,7 @@ class BindingDialog extends JDialog { infoTextField.putClientProperty("model", info); infoTextField.setText(info == null ? "" : infoObjectFormat.format(info)); - infoTextField.setToolTipText(info == null ? "null" : "
" + escapeHTML(asPrettyJsonString(info)) + "
"); + infoTextField.setToolTipText(info == null ? "null" : "
" + escapeHTML(json(info, true)) + "
"); } public Object getInfoObject() { diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index 0f50e648..c5ac4be7 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -7,7 +7,6 @@ import static javax.swing.SwingUtilities.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.media.MediaDetection.*; -import static net.filebot.media.XattrMetaInfo.*; import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.FileUtilities.*; import static net.filebot.util.ui.LoadingOverlayPane.*; @@ -68,6 +67,7 @@ import net.filebot.UserFiles; import net.filebot.WebServices; import net.filebot.format.MediaBindingBean; import net.filebot.mac.MacAppUtilities; +import net.filebot.media.MetaAttributes; import net.filebot.similarity.Match; import net.filebot.ui.rename.FormatDialog.Mode; import net.filebot.ui.rename.RenameModel.FormattedFuture; @@ -362,8 +362,11 @@ public class RenamePanel extends JComponent { installAction(this, WHEN_IN_FOCUSED_WINDOW, getKeyStroke(VK_F7, 0), newAction("Copy Debug Information", evt -> { try { withWaitCursor(evt.getSource(), () -> { - copyToClipboard(getDebugInfo()); - log.info("Match model has been copied to clipboard"); + String text = getDebugInfo(); + if (text.length() > 0) { + copyToClipboard(text); + log.info("Match model has been copied to clipboard"); + } }); } catch (Exception e) { debug.log(Level.WARNING, e, e::getMessage); @@ -617,12 +620,12 @@ public class RenamePanel extends JComponent { StringBuilder sb = new StringBuilder(); Map options = new HashMap(2); - options.put(JsonWriter.TYPE_NAME_MAP, xattr.getJsonTypeMap()); + options.put(JsonWriter.TYPE_NAME_MAP, MetaAttributes.JSON_TYPE_MAP); options.put(JsonWriter.SKIP_NULL_FIELDS, true); for (Match m : renameModel.matches()) { - Object v = m.getValue(); String f = getStructurePathTail(m.getCandidate()).getPath(); + Object v = m.getValue(); // convert FastFile items if (v instanceof File) { diff --git a/source/net/filebot/util/JsonUtilities.java b/source/net/filebot/util/JsonUtilities.java index def37bcc..72f19499 100644 --- a/source/net/filebot/util/JsonUtilities.java +++ b/source/net/filebot/util/JsonUtilities.java @@ -24,12 +24,8 @@ public class JsonUtilities { return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); } - public static String asJsonString(Object object) { - return JsonWriter.objectToJson(object); - } - - public static String asPrettyJsonString(Object object) { - return JsonWriter.objectToJson(object, singletonMap(JsonWriter.PRETTY_PRINT, true)); + public static String json(Object object, boolean pretty) { + return JsonWriter.objectToJson(object, singletonMap(JsonWriter.PRETTY_PRINT, pretty)); } public static Map asMap(Object node) { diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index 98530de8..279ae440 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -53,9 +53,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider implements Artwor return true; } - protected Object postJson(String path, Object json) throws Exception { + protected Object postJson(String path, Object object) throws Exception { // curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'https://api.thetvdb.com/login' --data '{"apikey":"XXXXX"}' - ByteBuffer response = post(getEndpoint(path), asJsonString(json).getBytes(UTF_8), "application/json", null); + ByteBuffer response = post(getEndpoint(path), json(object, false).getBytes(UTF_8), "application/json", null); return readJson(UTF_8.decode(response)); }