]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/SingleObjectProcedure.java
Direct and synchronization-free access to Layer0 resource class for DB
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / SingleObjectProcedure.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 SingleObjectProcedure implements IntProcedure {
10
11         final private QueryProcessor processor;
12         private DatabaseException exception;
13         private Resource result;
14         
15         public SingleObjectProcedure(QueryProcessor processor) {
16                 this.processor = processor;
17         }
18         
19         @Override
20         public void execute(ReadGraphImpl graph, int i) {
21                 Resource resource = processor.querySupport.getResource(i);
22                 if(result != null) {
23                         exception = new NoSingleResultException("", 2);
24                 } else {
25                         result = resource;
26                 }
27         }
28
29         @Override
30         public void finished(ReadGraphImpl graph) {
31                 if(result == null) exception = new NoSingleResultException("", 0);
32         }
33
34         @Override
35         public void exception(ReadGraphImpl graph, Throwable throwable) {
36                 if(throwable instanceof DatabaseException) exception = (DatabaseException)throwable;
37                 else throwable = new DatabaseException(throwable);
38         }
39         
40         public Resource get() throws DatabaseException {
41                 if(exception != null) throw exception;
42                 else return result;
43         }
44
45 }