From 2ee0e3cd8d7756aa243d8d8cdd3c02f511b2961d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 9 Mar 2016 05:58:36 +0000 Subject: [PATCH] Stream refactoring --- source/net/filebot/util/XPathUtilities.java | 59 ++++++--------------- source/net/filebot/web/AnidbClient.java | 2 +- source/net/filebot/web/TheTVDBClient.java | 14 ++--- 3 files changed, 22 insertions(+), 53 deletions(-) diff --git a/source/net/filebot/util/XPathUtilities.java b/source/net/filebot/util/XPathUtilities.java index 980d6728..3acac261 100644 --- a/source/net/filebot/util/XPathUtilities.java +++ b/source/net/filebot/util/XPathUtilities.java @@ -1,6 +1,5 @@ package net.filebot.util; -import java.util.AbstractList; import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; @@ -20,14 +19,18 @@ public final class XPathUtilities { return (Node) evaluateXPath(xpath, node, XPathConstants.NODE); } - public static List selectNodes(String xpath, Object node) { - return new NodeListDecorator((NodeList) evaluateXPath(xpath, node, XPathConstants.NODESET)); - } - public static String selectString(String xpath, Object node) { return ((String) evaluateXPath(xpath, node, XPathConstants.STRING)).trim(); } + public static Stream streamNodes(String xpath, Object node) { + return stream((NodeList) evaluateXPath(xpath, node, XPathConstants.NODESET)); + } + + public static Node[] selectNodes(String xpath, Object node) { + return streamNodes(xpath, node).toArray(Node[]::new); + } + public static List selectStrings(String xpath, Object node) { List values = new ArrayList(); for (Node it : selectNodes(xpath, node)) { @@ -47,25 +50,15 @@ public final class XPathUtilities { * @return text content of the child node or null if no child with the given name was found */ public static Node getChild(String nodeName, Node parentNode) { - for (Node child : new NodeListDecorator(parentNode.getChildNodes())) { - if (nodeName.equals(child.getNodeName())) - return child; - } - - return null; + return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName())).findFirst().orElse(null); } - public static List getChildren(String nodeName, Node parentNode) { - List children = new ArrayList(); - - if (parentNode != null) { - for (Node child : new NodeListDecorator(parentNode.getChildNodes())) { - if (nodeName.equals(child.getNodeName())) - children.add(child); - } + public static Node[] getChildren(String nodeName, Node parentNode) { + if (parentNode == null) { + return new Node[0]; + } else { + return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName())).toArray(Node[]::new); } - - return children; } public static String getAttribute(String attribute, Node node) { @@ -142,34 +135,14 @@ public final class XPathUtilities { } } - public static Stream streamChildren(Node parent) { - return stream(parent.getChildNodes()); + public static Stream streamElements(Node parent) { + return stream(parent.getChildNodes()).filter(n -> n.getNodeType() == Node.ELEMENT_NODE); } public static Stream stream(NodeList nodes) { return IntStream.range(0, nodes.getLength()).mapToObj(nodes::item); } - protected static class NodeListDecorator extends AbstractList { - - private final NodeList nodes; - - public NodeListDecorator(NodeList nodes) { - this.nodes = nodes; - } - - @Override - public Node get(int index) { - return nodes.item(index); - } - - @Override - public int size() { - return nodes.getLength(); - } - - } - private XPathUtilities() { throw new UnsupportedOperationException(); } diff --git a/source/net/filebot/web/AnidbClient.java b/source/net/filebot/web/AnidbClient.java index a5034ce2..736a43ce 100644 --- a/source/net/filebot/web/AnidbClient.java +++ b/source/net/filebot/web/AnidbClient.java @@ -132,7 +132,7 @@ public class AnidbClient extends AbstractEpisodeListProvider { // * only use categories with weight >= 400 // * sort by weight (descending) // * limit to 5 genres - seriesInfo.setGenres(selectNodes("anime/categories/category", dom).stream().map(categoryNode -> { + seriesInfo.setGenres(streamNodes("anime/categories/category", dom).map(categoryNode -> { String name = getTextContent("name", categoryNode); Integer weight = matchInteger(getAttribute("weight", categoryNode)); return new SimpleImmutableEntry(name, weight); diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index 34660069..95e876b2 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -105,10 +105,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { // perform online search Document dom = getXmlResource(MirrorType.SEARCH, "GetSeries.php?seriesname=" + encode(query, true) + "&language=" + getLanguageCode(locale)); - List nodes = selectNodes("Data/Series", dom); Map resultSet = new LinkedHashMap(); - for (Node node : nodes) { + for (Node node : selectNodes("Data/Series", dom)) { int sid = matchInteger(getTextContent("seriesid", node)); String seriesName = getTextContent("SeriesName", node); @@ -165,12 +164,10 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { seriesInfo.setPosterUrl(getResource(MirrorType.BANNER, getTextContent("poster", seriesNode))); // parse episode data - List nodes = selectNodes("Data/Episode", dom); - - List episodes = new ArrayList(nodes.size()); + List episodes = new ArrayList(50); List specials = new ArrayList(5); - for (Node node : nodes) { + for (Node node : selectNodes("Data/Episode", dom)) { String episodeName = getTextContent("EpisodeName", node); Integer absoluteNumber = matchInteger(getTextContent("absolute_number", node)); SimpleDate airdate = SimpleDate.parse(getTextContent("FirstAired", node)); @@ -277,7 +274,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { Document dom = getXmlResource(MirrorType.NULL, "mirrors.xml"); // collect all mirror data - Map> mirrorLists = selectNodes("Mirrors/Mirror", dom).stream().flatMap(node -> { + Map> mirrorLists = streamNodes("Mirrors/Mirror", dom).flatMap(node -> { String mirror = getTextContent("mirrorpath", node); int typeMask = Integer.parseInt(getTextContent("typemask", node)); @@ -392,10 +389,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { Document dom = getXmlResource(MirrorType.XML, "series/" + series.getId() + "/banners.xml"); - List nodes = selectNodes("//Banner", dom); List banners = new ArrayList(); - for (Node node : nodes) { + for (Node node : selectNodes("//Banner", dom)) { try { Map item = new EnumMap(BannerProperty.class);