]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DirectSuperRelations.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DirectSuperRelations.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 java.util.ArrayList;
18
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21 import org.simantics.db.procedure.ListenerBase;
22
23 final public class DirectSuperRelations extends UnaryQuery<IntProcedure> {
24
25         public ArrayList<InternalProcedure<IntSet>> procs = null;
26
27         private DirectSuperRelations(final int resource) {
28                 super(resource);
29         }
30
31         final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final IntProcedure procedure) {
32
33                 new DirectSuperRelations(r).computeForEach(graph, provider, procedure, false);
34
35         }
36
37         @Override
38         public UnaryQuery<IntProcedure> getEntry(QueryProcessor provider) {
39                 return null;
40         }
41
42         @Override
43         public void putEntry(QueryProcessor provider) {
44         }
45
46         @Override
47         final public void removeEntry(QueryProcessor provider) {
48         }
49
50         class Koss {
51
52                 private TIntHashSet set = null;
53                 public int single = 0;
54
55                 public boolean add(int val) {
56                         if(single == val) return false;
57                         if(single == 0) {
58                                 single = val;
59                                 return true;
60                         }
61                         if(set == null) set = new TIntHashSet(4);
62                         return set.add(val);
63                 }
64
65                 public int size() {
66
67                         if(single == 0) return 0;
68                         if(set == null) return 1;
69                         return set.size() + 1;
70
71                 }
72
73                 //        public int[] toArray() {
74                         //            
75                         //            int[] result = Arrays.copyOf(set.toArray(), set.size() + 1);
76                         //            result[set.size()] = single;
77                         //            return result;
78                         //            
79                         //        }
80                 //        
81
82                 public void forEach(TIntProcedure proc) {
83                         if(single > 0) proc.execute(single);
84                         if(set != null) set.forEach(proc);
85                 }
86
87         }
88
89         @Override
90         public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor provider, final IntProcedure procedure, final boolean store) {
91
92                 provider.querySupport.ensureLoaded(graph, id);
93                 
94                 int single = provider.querySupport.getSingleSuperrelation(id);
95                 if(single > 0) {
96                         procedure.execute(graph, single);
97                         procedure.finished(graph);
98                         return single;
99                 }
100
101                 final int subrelationOf = provider.getSubrelationOf();
102
103                 final IntSet result = new IntSet(provider.querySupport);
104
105                 final class DirectProcedure extends Koss implements IntProcedure, TIntProcedure {
106                         @Override
107                         final public boolean execute(int r) {
108                                 result.add(r);
109                                 return true;
110                         }
111                         @Override
112                         final public void execute(ReadGraphImpl graph, int r) {
113                                 if(single == 0) {
114                                         single = r;
115                                         return;
116                                 }
117                                 add(r);
118                         }
119                         @Override
120                         public void finished(ReadGraphImpl graph) {
121                         }
122                         @Override
123                         public void exception(ReadGraphImpl graph, Throwable t) {
124                                 throw new Error("Errors are not supported.", t);
125                         }
126
127                 }
128
129                 final DirectProcedure directProc = new DirectProcedure();
130
131                 provider.querySupport.getObjects(graph, id, subrelationOf, directProc);
132
133                 int size = directProc.size();
134
135                 if(size == 0) {
136
137                         procedure.finished(graph);
138
139                 } else if (size == 1) {
140
141                         procedure.execute(graph, directProc.single);
142                         procedure.finished(graph);
143
144                 } else {
145
146                         directProc.forEach(new TIntProcedure() {
147
148                                 @Override
149                                 public boolean execute(int arg0) {
150
151                                         procedure.execute(graph, arg0);
152                                         return true;
153
154                                 }
155
156                         });
157
158                         procedure.finished(graph);
159
160                 }
161                 
162                 return getResult();
163
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 }