]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/PossibleObjectProcedure.java
066e61055098dd8b89bb57ea9728d66806979982
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / PossibleObjectProcedure.java
1 package org.simantics.db.impl.graph;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.exception.DatabaseException;
5 import org.simantics.db.exception.NoSingleResultException;
6 import org.simantics.db.impl.query.IntProcedure;
7 import org.simantics.db.impl.query.QueryProcessor;
8
9 public class PossibleObjectProcedure implements IntProcedure {
10
11         private static DatabaseException DUMMY = new DatabaseException();
12         
13         final private QueryProcessor processor;
14         private DatabaseException exception;
15         private Resource result;
16         
17         public PossibleObjectProcedure(QueryProcessor processor) {
18                 this.processor = processor;
19         }
20         
21         @Override
22         public void execute(ReadGraphImpl graph, int i) {
23                 Resource resource = processor.querySupport.getResource(i);
24                 if(result != null) {
25                         exception = DUMMY;
26                 } else {
27                         result = resource;
28                 }
29         }
30
31         @Override
32         public void finished(ReadGraphImpl graph) {
33         }
34
35         @Override
36         public void exception(ReadGraphImpl graph, Throwable throwable) {
37                 if(throwable instanceof DatabaseException) exception = (DatabaseException)throwable;
38                 else throwable = new DatabaseException(throwable);
39         }
40         
41         public Resource get() throws DatabaseException {
42                 if(exception == DUMMY) return null;
43                 else if(exception != null) throw exception;
44                 else return result;
45         }
46
47 }