01: package org.ontoware.rdfreactor.runtime;
02:
03: import java.lang.reflect.Array;
04: import java.util.ArrayList;
05: import java.util.List;
06:
07: import org.ontoware.aifbcommons.collection.ClosableIterator;
08: import org.ontoware.rdf2go.model.Model;
09: import org.ontoware.rdf2go.model.Statement;
10: import org.ontoware.rdf2go.model.impl.TriplePatternImpl;
11: import org.ontoware.rdf2go.model.node.Node;
12:
13: public class ReactorResult<T> {
14:
15: private Class<T> returnType;
16:
17: private Model model;
18:
19: private TriplePatternImpl triplePattern;
20:
21: public ReactorResult(Model model, TriplePatternImpl triplePattern,
22: Class<T> returnType) {
23: this .model = model;
24: this .triplePattern = triplePattern;
25: this .returnType = returnType;
26: }
27:
28: public ClosableIterator<T> asClosableIterator() {
29: ClosableIterator<Statement> it = model.findStatements(
30: triplePattern.getSubject(), triplePattern
31: .getPredicate(), triplePattern.getObject());
32: ProjectingIterator.projection proj = null;
33: switch (triplePattern.extract) {
34: case SUBJECT:
35: proj = ProjectingIterator.projection.Subject;
36: break;
37: case PREDICATE:
38: proj = ProjectingIterator.projection.Predicate;
39: break;
40: case OBJECT:
41: proj = ProjectingIterator.projection.Object;
42: break;
43: }
44: return new ConvertingClosableIterator<T>(
45: new ProjectingIterator<Node>(it, proj), model,
46: returnType);
47: }
48:
49: public T firstValue() {
50: ClosableIterator<T> it = asClosableIterator();
51: T first = null;
52: if (it.hasNext()) {
53: first = it.next();
54: }
55: it.close();
56: return first;
57: }
58:
59: public List<T> asList() {
60: ClosableIterator<T> it = asClosableIterator();
61: ArrayList<T> list = new ArrayList<T>();
62: while (it.hasNext()) {
63: list.add(it.next());
64: }
65: it.close();
66: return list;
67: }
68:
69: @SuppressWarnings("unchecked")
70: public T[] asArray() {
71: Object[] resultAsArray = (Object[]) Array.newInstance(
72: returnType, 0);
73: return (T[]) asList().toArray(resultAsArray);
74: }
75:
76: public long count() {
77: ClosableIterator<T> it = asClosableIterator();
78: long count = 0;
79: while (it.hasNext()) {
80: it.next();
81: count++;
82: }
83: it.close();
84: return count;
85: }
86:
87: }
|