use of org.neo4j.helpers.collection.IterableWrapper in project neo4j-mobile-android by neo4j-contrib.
the class Service method ourOwnLoader.
private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
Collection<URL> urls = new HashSet<URL>();
try {
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
while (resources.hasMoreElements()) {
urls.add(resources.nextElement());
}
} catch (IOException e) {
return null;
}
return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {
@Override
protected BufferedReader underlyingObjectToObject(URL url) {
try {
return new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException e) {
return null;
}
}
})) {
@Override
protected Iterator<T> createNestedIterator(final BufferedReader input) {
return new PrefetchingIterator<T>() {
@Override
protected T fetchNextOrNull() {
try {
String line;
while (null != (line = input.readLine())) {
try {
return type.cast(Class.forName(line).newInstance());
} catch (Exception e) {
} catch (LinkageError e) {
}
}
input.close();
return null;
} catch (IOException e) {
return null;
}
}
/* Finalizer - close the input stream.
* Prevent leakage of open files. Finalizers impact GC performance,
* but there are expected to be few of these objects.
*/
@Override
protected void finalize() throws Throwable {
input.close();
}
};
}
};
}
use of org.neo4j.helpers.collection.IterableWrapper in project neo4j-mobile-android by neo4j-contrib.
the class ShortestPath method getPaths.
private static Iterable<LinkedList<Relationship>> getPaths(Hit hit, DirectionData data) {
LevelData levelData = data.visitedNodes.get(hit.connectingNode);
if (levelData.depth == 0) {
Collection<LinkedList<Relationship>> result = new ArrayList<LinkedList<Relationship>>();
result.add(new LinkedList<Relationship>());
return result;
}
Collection<PathData> set = new ArrayList<PathData>();
GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
for (long rel : levelData.relsToHere) {
set.add(new PathData(hit.connectingNode, new LinkedList<Relationship>(Arrays.asList(graphDb.getRelationshipById(rel)))));
}
for (int i = 0; i < levelData.depth - 1; i++) {
// One level
Collection<PathData> nextSet = new ArrayList<PathData>();
for (PathData entry : set) {
// One path...
Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
LevelData otherLevelData = data.visitedNodes.get(otherNode);
int counter = 0;
for (long rel : otherLevelData.relsToHere) {
// ...may split into several paths
LinkedList<Relationship> rels = ++counter == otherLevelData.relsToHere.length ? // lists being copied
entry.rels : new LinkedList<Relationship>(entry.rels);
rels.addFirst(graphDb.getRelationshipById(rel));
nextSet.add(new PathData(otherNode, rels));
}
}
set = nextSet;
}
return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {
@Override
protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
return object.rels;
}
};
}
use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.
the class RecordAccessStub method populateCache.
public void populateCache() {
CacheTask action = new CacheTask.CacheNextRel(CheckStage.Stage3_NS_NextRel, cacheAccess, resourceIterable(new IterableWrapper<NodeRecord, Delta<NodeRecord>>(nodes.values()) {
@Override
protected NodeRecord underlyingObjectToObject(Delta<NodeRecord> node) {
return node.newRecord;
}
}));
action.run();
}
use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.
the class ShortestPath method getPaths.
private static Iterable<LinkedList<Relationship>> getPaths(Node connectingNode, DirectionData data, boolean stopAsap) {
LevelData levelData = data.visitedNodes.get(connectingNode);
if (levelData.depth == 0) {
Collection<LinkedList<Relationship>> result = new ArrayList<>();
result.add(new LinkedList<>());
return result;
}
Collection<PathData> set = new ArrayList<>();
GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
for (long rel : levelData.relsToHere) {
set.add(new PathData(connectingNode, new LinkedList<>(Arrays.asList(graphDb.getRelationshipById(rel)))));
if (stopAsap) {
break;
}
}
for (int i = 0; i < levelData.depth - 1; i++) {
// One level
Collection<PathData> nextSet = new ArrayList<>();
for (PathData entry : set) {
// One path...
Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
LevelData otherLevelData = data.visitedNodes.get(otherNode);
int counter = 0;
for (long rel : otherLevelData.relsToHere) {
// ...may split into several paths
LinkedList<Relationship> rels = ++counter == otherLevelData.relsToHere.length ? // lists being copied
entry.rels : new LinkedList<>(entry.rels);
rels.addFirst(graphDb.getRelationshipById(rel));
nextSet.add(new PathData(otherNode, rels));
if (stopAsap) {
break;
}
}
}
set = nextSet;
}
return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {
@Override
protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
return object.rels;
}
};
}
use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.
the class TestTraversalWithIterable method useTraverserInsideTraverser.
@Test
public void useTraverserInsideTraverser() throws Exception {
/*
* (a)-->(b)-->(c)
* |
* \/
* (d)-->(e)-->(f)
*
*/
createGraph("a FIRST d", "a TO b", "b TO c", "d TO e", "e TO f");
try (Transaction tx = beginTx()) {
TraversalDescription firstTraverser = getGraphDb().traversalDescription().relationships(RelationshipType.withName("FIRST")).evaluator(Evaluators.toDepth(1));
final Iterable<Path> firstResult = firstTraverser.traverse(getNodeWithName("a"));
Iterable<Node> startNodesForNestedTraversal = new IterableWrapper<Node, Path>(firstResult) {
@Override
protected Node underlyingObjectToObject(Path path) {
return path.endNode();
}
};
TraversalDescription nestedTraversal = getGraphDb().traversalDescription().evaluator(Evaluators.atDepth(2));
expectPaths(nestedTraversal.traverse(startNodesForNestedTraversal), "a,b,c", "d,e,f");
tx.success();
}
}
Aggregations