]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
Yet another fixing commit
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / OrderedSet.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 java.util.concurrent.atomic.AtomicInteger;
15
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.IntProcedureAdapter;
20
21 final public class OrderedSet extends CollectionUnaryQuery {
22         
23         public OrderedSet(final int r) {
24         super(r);
25     }
26
27         @Override
28         final public void removeEntry(QueryProcessor provider) {
29         provider.cache.remove(this);
30         }
31
32         private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException {
33             
34                 QueryProcessor processor = graph.processor;
35                 
36                 processor.querySupport.ensureLoaded(graph, current);
37                 
38                 AtomicInteger res = new AtomicInteger(0);
39                 
40                 processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
41
42                         @Override
43                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
44                                 if(i != orderedSet) {
45                                         procedure.execute(graph, i);
46                                 }
47                                 res.set(i);
48                         }
49
50                         @Override
51                         public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
52                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
53                                 procedure.exception(graph, t);
54                         }
55
56                         @Override
57                         public void finished(ReadGraphImpl graph) {
58                         }
59
60                 });
61
62                 if(res.get() == orderedSet) {
63                         procedure.finished(graph);
64                 }
65                 
66                 return res.get();
67
68         }
69         
70     @Override
71     public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
72         computeForEach(graph, id, this, procedure);
73     }
74
75     static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException {
76         
77         IntProcedure procedure = entry != null ? entry : procedure_;
78
79         int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
80         while(current != orderedSet) {
81                 current = nextElement(graph, current, orderedSet, entry, procedure);
82         }
83
84         if(entry != null) entry.performFromCache(graph, procedure_);
85         
86     }
87     
88     @Override
89     public String toString() {
90         return "OrderedSet[" + id + "]";
91     }
92     
93 }