]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PossibleSuperRelation.java
51c6c68d3da238342828090d5a93cca1a0cb2548
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PossibleSuperRelation.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import gnu.trove.procedure.TIntProcedure;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 import org.simantics.db.Resource;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.ListenerBase;
21
22 final public class PossibleSuperRelation extends UnaryQuery<IntProcedure> {
23
24         private PossibleSuperRelation(final int resource) {
25                 super(resource);
26         }
27
28         final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final AsyncProcedure<Resource> procedure) {
29
30                 int result = provider.querySupport.getSingleSuperrelation(r);
31                 if(result != 0) {
32                         procedure.execute(graph, provider.querySupport.getResource(result));
33                 } else {
34                         procedure.execute(graph, null);
35                 }
36
37         }
38
39         @Override
40         public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
41                 return null;
42         }
43
44         @Override
45         public void putEntry(QueryProcessor provider) {
46         }
47
48         @Override
49         final public void removeEntry(QueryProcessor provider) {
50         }
51
52         class Koss {
53
54                 private TIntHashSet set = null;
55                 public int single = 0;
56
57                 public boolean add(int val) {
58                         if(single == val) return false;
59                         if(single == 0) {
60                                 single = val;
61                                 return true;
62                         }
63                         if(set == null) set = new TIntHashSet(4);
64                         return set.add(val);
65                 }
66
67                 public int size() {
68
69                         if(single == 0) return 0;
70                         if(set == null) return 1;
71                         return set.size() + 1;
72
73                 }
74
75                 //        public int[] toArray() {
76                         //            
77                         //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
78                         //            result[set.size()] = single;
79                         //            return result;
80                         //            
81                         //        }
82                 //        
83
84                 public void forEach(TIntProcedure proc) {
85                         if(single > 0) proc.execute(single);
86                         if(set != null) set.forEach(proc);
87                 }
88
89         }
90
91         @Override
92         public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
93
94                 provider.querySupport.ensureLoaded(graph, id);
95                 int single = provider.querySupport.getSingleSuperrelation(id);
96                 if(single > 0) {
97                         procedure.execute(graph, single);
98                         procedure.finished(graph);
99                         return single;
100                 }
101
102                 final int subrelationOf = provider.getSubrelationOf();
103
104                 final IntSet result = new IntSet(provider.querySupport);
105
106                 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
107                         @Override
108                         final public boolean execute(int r) {
109                                 result.add(r);
110                                 return true;
111                         }
112                         @Override
113                         final public void execute(ReadGraphImpl graph, int r) {
114                                 if(single == 0) {
115                                         single = r;
116                                         return;
117                                 }
118                                 add(r);
119                         }
120                         @Override
121                         public void finished(ReadGraphImpl graph) {
122                         }
123                         @Override
124                         public void exception(ReadGraphImpl graph, Throwable t) {
125                                 throw new Error("Errors are not supported.", t);
126                         }
127
128                 }
129
130                 final DirectProcedure directProc = new DirectProcedure();
131
132                 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
133
134                 int size = directProc.size();
135
136                 if(size == 0) {
137
138                         procedure.finished(graph);
139
140                 } else if (size == 1) {
141
142                         procedure.execute(graph, directProc.single);
143                         procedure.finished(graph);
144
145                 } else {
146
147                         directProc.forEach(new TIntProcedure() {
148
149                                 @Override
150                                 public boolean execute(int arg0) {
151
152                                         procedure.execute(graph, arg0);
153                                         return true;
154
155                                 }
156
157                         });
158
159                         procedure.finished(graph);
160
161                 }
162                 
163                 return result;
164
165         }
166
167         @Override
168         public String toString() {
169                 return "SuperRelations2[" + id + "]";
170         }
171
172         @Override
173         public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, IntProcedure procedure) {
174                 throw new UnsupportedOperationException();
175         }
176
177         @Override
178         public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
179
180         }
181
182 }