com.github.simy4.xpath.view.NodeSetView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xpath-to-xml-core Show documentation
Show all versions of xpath-to-xml-core Show documentation
Convenient utility to build XML models by evaluating XPath expressions
package com.github.simy4.xpath.view;
import com.github.simy4.xpath.XmlBuilderException;
import com.github.simy4.xpath.navigator.Node;
import com.github.simy4.xpath.util.FilteringIterator;
import com.github.simy4.xpath.util.Function;
import com.github.simy4.xpath.util.Predicate;
import com.github.simy4.xpath.util.TransformingIterator;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public final class NodeSetView implements IterableNodeView {
private static final NodeSetView> EMPTY_NODE_SET = new NodeSetView(Collections.>emptySet());
@SuppressWarnings("unchecked")
public static NodeSetView empty() {
return (NodeSetView) EMPTY_NODE_SET;
}
private final Iterable> nodeSet;
/**
* Creates NodeSetView from given nodes and predicate.
*
* @param nodes nodes to wap in a view
* @param predicate predicate to apply to nodes
*/
public NodeSetView(final Iterable extends N> nodes, final Predicate super N> predicate) {
this(new Iterable>() {
@Override
public Iterator> iterator() {
return new TransformingIterator>(new FilteringIterator(nodes.iterator(), predicate),
new NodeWrapper());
}
});
}
public NodeSetView(Iterable> nodeSet) {
this.nodeSet = nodeSet;
}
@Override
public int compareTo(View other) {
final Iterator> iterator = iterator();
if (iterator.hasNext()) {
return iterator.next().compareTo(other);
} else {
return other.toBoolean() ? -1 : 0;
}
}
@Override
public boolean toBoolean() {
return iterator().hasNext();
}
@Override
public double toNumber() {
final Iterator> iterator = iterator();
return iterator.hasNext() ? iterator.next().toNumber() : Double.NaN;
}
@Override
public String toString() {
final Iterator> iterator = iterator();
return iterator.hasNext() ? iterator.next().toString() : "";
}
@Override
public T visit(ViewVisitor visitor) throws XmlBuilderException {
return visitor.visit(this);
}
@Override
public Iterator> iterator() {
return new FilteringIterator>(nodeSet.iterator(), new Distinct());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof View)) {
return false;
}
final Iterator> iterator = iterator();
if (iterator.hasNext()) {
return iterator.next().equals(o);
} else {
return !((View) o).toBoolean();
}
}
@Override
public int hashCode() {
final Iterator> iterator = iterator();
return iterator.hasNext() ? iterator.next().hashCode() : 0;
}
private static final class NodeWrapper implements Function> {
@Override
public NodeView apply(T node) {
return new NodeView(node);
}
}
private static final class Distinct implements Predicate> {
private final Set visited = new HashSet();
@Override
public boolean test(NodeView node) {
return visited.add(node.getNode());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy