use of org.neo4j.helpers.collection.NestingIterable 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.NestingIterable in project graphdb by neo4j-attic.
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();
}
};
}
};
}
Aggregations