1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import java.util.concurrent.atomic.AtomicInteger;
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
20 final public class OrderedSet extends CollectionUnaryQuery {
22 public OrderedSet(final int r) {
27 final public void removeEntry(QueryProcessor provider) {
28 provider.cache.remove(this);
31 private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException {
33 QueryProcessor processor = graph.processor;
35 processor.querySupport.ensureLoaded(graph, current);
37 AtomicInteger res = new AtomicInteger(0);
39 processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
42 public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
44 procedure.execute(graph, i);
50 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
51 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
52 procedure.exception(graph, t);
56 public void finished(ReadGraphImpl graph) {
61 if(res.get() == orderedSet) {
62 procedure.finished(graph);
70 public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
71 computeForEach(graph, id, this, procedure);
74 static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException {
76 IntProcedure procedure = entry != null ? entry : procedure_;
78 int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
79 while(current != orderedSet) {
80 current = nextElement(graph, current, orderedSet, entry, procedure);
83 if(entry != null) entry.performFromCache(graph, procedure_);
88 public String toString() {
89 return "OrderedSet[" + id + "]";