/** * Print an iterator, return a copy of the iterator. Printing * occurs as the returned iterator is used. */ public static <T> Iterator<T> debug(final PrintStream out, Iterator<T> stream) { try { return map(stream, item -> {out.println(item); return item;}) ; } finally { out.flush() ; } }
protected Iterator<Node> stepExcludeBackwards(Node node, List<Node> excludedNodes) { Iter<Triple> iter1 = backwardLinks(node, excludedNodes) ; Iter<Node> r1 = iter1.map(Triple::getSubject) ; return r1 ; }
/** Project quads to triples */ public static Iter<Triple> quads2triples(Iterator<Quad> iter) { return Iter.iter(iter).map(Quad::asTriple) ; }
protected Iterator<Node> stepExcludeForwards(Node node, List<Node> excludedNodes) { Iter<Triple> iter1 = forwardLinks(node, excludedNodes) ; Iter<Node> r1 = iter1.map(Triple::getObject) ; return r1 ; }
/** Convert from Iterator<BindingNodeId> to Iterator<Binding>, conversion "on demand" * (in convToBinding(BindingNodeId, NodeTable) */ public static Iterator<Binding> convertToNodes(Iterator<BindingNodeId> iterBindingIds, NodeTable nodeTable) { return Iter.map(iterBindingIds, bindingNodeIds -> convToBinding(bindingNodeIds, nodeTable)) ; }
/** Transform a list of elements to a new list of the function applied to each element. * Using a stream is often better. This operation preserves the order of the list. * @deprecated Use Java8 Streams */ @Deprecated public static <T, R> List<R> map(List<? extends T> list, Function<T, R> converter) { return toList(map(list.iterator(), converter)) ; }
@Override public Iterator<Tuple<NodeId>> all() { Iterator<Record> iter = index.iterator(); return Iter.map(iter, item -> TupleLib.tuple(item, colMap)); }
protected final Iterator<Node> doOne(Node node, Node property) { Iterator<Node> iter2 = null ; if ( direction() ) { Iter<Triple> iter1 = Iter.iter(graphFind(node, property, Node.ANY)) ; iter2 = iter1.map(Triple::getObject) ; } else { Iter<Triple> iter1 = Iter.iter(graphFind(Node.ANY, property, node)) ; iter2 = iter1.map(Triple::getSubject) ; } return iter2 ; }
@Override public Iterator<Node> listGraphNodes() { Iter<Quad> iter = Iter.iter(find(Node.ANY, Node.ANY, Node.ANY, Node.ANY)) ; return iter.map(Quad::getGraph).distinct() ; }
/** List the predicates in a graph (no duplicates) */ public static Iterator<Node> listPredicates(Graph graph) { ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ; return Iter.iter(iter).map(Triple::getPredicate).distinct() ; }
private static Iterator<Triple> projectQuadsToTriples(Node graphNode, Iterator<Quad> iter) { // Checking. Function<Quad, Triple> f = (q) -> { if ( graphNode != null && !q.getGraph().equals(graphNode) ) throw new InternalError("projectQuadsToTriples: Quads from unexpected graph (expected=" + graphNode + ", got=" + q.getGraph() + ")"); return q.asTriple(); }; // Without checking //Function<Quad, Triple> f = (q) -> q.asTriple(); return Iter.map(iter, f); }
/** List the subjects in a graph (no duplicates) */ public static Iterator<Node> listSubjects(Graph graph) { ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ; return Iter.iter(iter).map(Triple::getSubject).distinct() ; }
/** List the objects in a graph (no duplicates) */ public static Iterator<Node> listObjects(Graph graph) { ExtendedIterator<Triple> iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ; return Iter.iter(iter).map(Triple::getObject).distinct() ; }
private Iterator<Pair<NodeId, Node>> all2() { Iterator<Pair<Long, ByteBuffer>> objs = objects.all() ; Function<Pair<Long, ByteBuffer>, Pair<NodeId, Node>> transform = item -> { NodeId id = NodeId.create(item.car().longValue()); ByteBuffer bb = item.cdr(); Node n = NodeLib.decode(bb); return new Pair<>(id, n); }; return Iter.map(objs, transform) ; }
private static Iterator<Pair<NodeId, Node>> all(ObjectFile objFile) { Iterator<Pair<Long, ByteBuffer>> objs = objFile.all() ; Function<Pair<Long, ByteBuffer>, Pair<NodeId, Node>> transform = item -> { NodeId id = NodeId.create(item.car().longValue()); ByteBuffer bb = item.cdr(); Node n = NodeLib.decode(bb); return new Pair<>(id, n); }; return Iter.map(objs, transform) ; }
@Test public void map_01() { Iterator<String> it = Iter.map(data2.iterator(), item -> item + item); test(it, "xx", "yy", "zz"); }
@Override public Iterator<Quad> findNG(Node g, Node s, Node p, Node o) { return g.isConcrete() ? findInOneGraph(g, s, p, o) : concat(map(listGraphNodes(), gn -> findInOneGraph(gn, s, p, o))); }
private static ExtendedIterator<Triple> quadsToTriples(DatasetGraph dsg, Node graphName, Triple m) { Iterator<Quad> qIter = dsg.findNG(graphName, m.getSubject(), m.getPredicate(), m.getObject()); Iterator<Triple> tIter = Iter.map(qIter, quad->quad.asTriple()); return WrappedIterator.createNoRemove(tIter) ; }
@Override public Iterator<Node> listGraphNodes() { Iterator<Tuple<NodeId>> x = quadTable.getNodeTupleTable().findAll() ; Iterator<NodeId> z = Iter.iter(x).map(t -> t.get(0)).distinct() ; return NodeLib.nodes(quadTable.getNodeTupleTable().getNodeTable(), z) ; }
@Test public void find_union_03() { DatasetGraphBaseFind dsgx = (DatasetGraphBaseFind)dsg ; assertNotNull(dsgx.getUnionGraph()); Set<Triple> x1 = toSet(dsgx.getUnionGraph().find(null, null, null)) ; Set<Triple> x2 = Iter.iter(dsg.find(Quad.unionGraph, null, null, null)).map(Quad::asTriple).toSet(); assertEquals(x1, x2); }