Stream refactoring

This commit is contained in:
Reinhard Pointner 2016-03-09 05:58:36 +00:00
parent 1e7fa00ef4
commit 2ee0e3cd8d
3 changed files with 22 additions and 53 deletions

View File

@ -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<Node> 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<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) {
List<String> values = new ArrayList<String>();
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<Node> getChildren(String nodeName, Node parentNode) {
List<Node> children = new ArrayList<Node>();
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<Node> streamChildren(Node parent) {
return stream(parent.getChildNodes());
public static Stream<Node> streamElements(Node parent) {
return stream(parent.getChildNodes()).filter(n -> n.getNodeType() == Node.ELEMENT_NODE);
}
public static Stream<Node> stream(NodeList nodes) {
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() {
throw new UnsupportedOperationException();
}

View File

@ -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<String, Integer>(name, weight);

View File

@ -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<Node> nodes = selectNodes("Data/Series", dom);
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));
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<Node> nodes = selectNodes("Data/Episode", dom);
List<Episode> episodes = new ArrayList<Episode>(nodes.size());
List<Episode> episodes = new ArrayList<Episode>(50);
List<Episode> specials = new ArrayList<Episode>(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<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);
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<Node> nodes = selectNodes("//Banner", dom);
List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>();
for (Node node : nodes) {
for (Node node : selectNodes("//Banner", dom)) {
try {
Map<BannerProperty, String> item = new EnumMap<BannerProperty, String>(BannerProperty.class);