Stream refactoring
This commit is contained in:
parent
1e7fa00ef4
commit
2ee0e3cd8d
|
@ -1,6 +1,5 @@
|
||||||
package net.filebot.util;
|
package net.filebot.util;
|
||||||
|
|
||||||
import java.util.AbstractList;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
@ -20,14 +19,18 @@ public final class XPathUtilities {
|
||||||
return (Node) evaluateXPath(xpath, node, XPathConstants.NODE);
|
return (Node) evaluateXPath(xpath, node, XPathConstants.NODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Node> selectNodes(String xpath, Object node) {
|
|
||||||
return new NodeListDecorator((NodeList) evaluateXPath(xpath, node, XPathConstants.NODESET));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String selectString(String xpath, Object node) {
|
public static String selectString(String xpath, Object node) {
|
||||||
return ((String) evaluateXPath(xpath, node, XPathConstants.STRING)).trim();
|
return ((String) evaluateXPath(xpath, node, XPathConstants.STRING)).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Stream<Node> 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<String> selectStrings(String xpath, Object node) {
|
public static List<String> selectStrings(String xpath, Object node) {
|
||||||
List<String> values = new ArrayList<String>();
|
List<String> values = new ArrayList<String>();
|
||||||
for (Node it : selectNodes(xpath, node)) {
|
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
|
* @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) {
|
public static Node getChild(String nodeName, Node parentNode) {
|
||||||
for (Node child : new NodeListDecorator(parentNode.getChildNodes())) {
|
return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName())).findFirst().orElse(null);
|
||||||
if (nodeName.equals(child.getNodeName()))
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Node> getChildren(String nodeName, Node parentNode) {
|
public static Node[] getChildren(String nodeName, Node parentNode) {
|
||||||
List<Node> children = new ArrayList<Node>();
|
if (parentNode == null) {
|
||||||
|
return new Node[0];
|
||||||
if (parentNode != null) {
|
} else {
|
||||||
for (Node child : new NodeListDecorator(parentNode.getChildNodes())) {
|
return stream(parentNode.getChildNodes()).filter(n -> nodeName.equals(n.getNodeName())).toArray(Node[]::new);
|
||||||
if (nodeName.equals(child.getNodeName()))
|
|
||||||
children.add(child);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return children;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getAttribute(String attribute, Node node) {
|
public static String getAttribute(String attribute, Node node) {
|
||||||
|
@ -142,34 +135,14 @@ public final class XPathUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Stream<Node> streamChildren(Node parent) {
|
public static Stream<Node> streamElements(Node parent) {
|
||||||
return stream(parent.getChildNodes());
|
return stream(parent.getChildNodes()).filter(n -> n.getNodeType() == Node.ELEMENT_NODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Stream<Node> stream(NodeList nodes) {
|
public static Stream<Node> stream(NodeList nodes) {
|
||||||
return IntStream.range(0, nodes.getLength()).mapToObj(nodes::item);
|
return IntStream.range(0, nodes.getLength()).mapToObj(nodes::item);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static class NodeListDecorator extends AbstractList<Node> {
|
|
||||||
|
|
||||||
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() {
|
private XPathUtilities() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
|
||||||
// * only use categories with weight >= 400
|
// * only use categories with weight >= 400
|
||||||
// * sort by weight (descending)
|
// * sort by weight (descending)
|
||||||
// * limit to 5 genres
|
// * 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);
|
String name = getTextContent("name", categoryNode);
|
||||||
Integer weight = matchInteger(getAttribute("weight", categoryNode));
|
Integer weight = matchInteger(getAttribute("weight", categoryNode));
|
||||||
return new SimpleImmutableEntry<String, Integer>(name, weight);
|
return new SimpleImmutableEntry<String, Integer>(name, weight);
|
||||||
|
|
|
@ -105,10 +105,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
|
||||||
// perform online search
|
// perform online search
|
||||||
Document dom = getXmlResource(MirrorType.SEARCH, "GetSeries.php?seriesname=" + encode(query, true) + "&language=" + getLanguageCode(locale));
|
Document dom = getXmlResource(MirrorType.SEARCH, "GetSeries.php?seriesname=" + encode(query, true) + "&language=" + getLanguageCode(locale));
|
||||||
|
|
||||||
List<Node> nodes = selectNodes("Data/Series", dom);
|
|
||||||
Map<Integer, TheTVDBSearchResult> resultSet = new LinkedHashMap<Integer, TheTVDBSearchResult>();
|
Map<Integer, TheTVDBSearchResult> resultSet = new LinkedHashMap<Integer, TheTVDBSearchResult>();
|
||||||
|
|
||||||
for (Node node : nodes) {
|
for (Node node : selectNodes("Data/Series", dom)) {
|
||||||
int sid = matchInteger(getTextContent("seriesid", node));
|
int sid = matchInteger(getTextContent("seriesid", node));
|
||||||
String seriesName = getTextContent("SeriesName", node);
|
String seriesName = getTextContent("SeriesName", node);
|
||||||
|
|
||||||
|
@ -165,12 +164,10 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
|
||||||
seriesInfo.setPosterUrl(getResource(MirrorType.BANNER, getTextContent("poster", seriesNode)));
|
seriesInfo.setPosterUrl(getResource(MirrorType.BANNER, getTextContent("poster", seriesNode)));
|
||||||
|
|
||||||
// parse episode data
|
// parse episode data
|
||||||
List<Node> nodes = selectNodes("Data/Episode", dom);
|
List<Episode> episodes = new ArrayList<Episode>(50);
|
||||||
|
|
||||||
List<Episode> episodes = new ArrayList<Episode>(nodes.size());
|
|
||||||
List<Episode> specials = new ArrayList<Episode>(5);
|
List<Episode> specials = new ArrayList<Episode>(5);
|
||||||
|
|
||||||
for (Node node : nodes) {
|
for (Node node : selectNodes("Data/Episode", dom)) {
|
||||||
String episodeName = getTextContent("EpisodeName", node);
|
String episodeName = getTextContent("EpisodeName", node);
|
||||||
Integer absoluteNumber = matchInteger(getTextContent("absolute_number", node));
|
Integer absoluteNumber = matchInteger(getTextContent("absolute_number", node));
|
||||||
SimpleDate airdate = SimpleDate.parse(getTextContent("FirstAired", node));
|
SimpleDate airdate = SimpleDate.parse(getTextContent("FirstAired", node));
|
||||||
|
@ -277,7 +274,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
|
||||||
Document dom = getXmlResource(MirrorType.NULL, "mirrors.xml");
|
Document dom = getXmlResource(MirrorType.NULL, "mirrors.xml");
|
||||||
|
|
||||||
// collect all mirror data
|
// collect all mirror data
|
||||||
Map<MirrorType, List<String>> mirrorLists = selectNodes("Mirrors/Mirror", dom).stream().flatMap(node -> {
|
Map<MirrorType, List<String>> mirrorLists = streamNodes("Mirrors/Mirror", dom).flatMap(node -> {
|
||||||
String mirror = getTextContent("mirrorpath", node);
|
String mirror = getTextContent("mirrorpath", node);
|
||||||
int typeMask = Integer.parseInt(getTextContent("typemask", 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");
|
Document dom = getXmlResource(MirrorType.XML, "series/" + series.getId() + "/banners.xml");
|
||||||
|
|
||||||
List<Node> nodes = selectNodes("//Banner", dom);
|
|
||||||
List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>();
|
List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>();
|
||||||
|
|
||||||
for (Node node : nodes) {
|
for (Node node : selectNodes("//Banner", dom)) {
|
||||||
try {
|
try {
|
||||||
Map<BannerProperty, String> item = new EnumMap<BannerProperty, String>(BannerProperty.class);
|
Map<BannerProperty, String> item = new EnumMap<BannerProperty, String>(BannerProperty.class);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue