]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntSet.java
index a84f8fc5553aa04c31537ecedad7ec4e182a7666..3ef19b8f335ec9dbf60aeaf1df6bf818251d67a2 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.procedure.TIntProcedure;
-
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
@@ -20,9 +18,13 @@ import java.util.Set;
 
 import org.simantics.db.Resource;
 import org.simantics.db.ResourceSet;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.ResourceImpl;
+import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.support.ResourceSupport;
 
+import gnu.trove.procedure.TIntProcedure;
+
 
 final public class IntSet implements ResourceSet {
 
@@ -37,11 +39,13 @@ final public class IntSet implements ResourceSet {
     protected static final int DEFAULT_CAPACITY = 3;
 
     public static final int NO_DATA = -1;
+    
+    private static final Object[] EMPTY_ARRAY = new Object[0];
+
+    public static IntSet EMPTY = new IntSet();
 
-    public IntSet() {
+    private IntSet() {
         support = null;
-        data = null;
-        sizeOrData = NO_DATA;
     }
 
     public IntSet(QuerySupport support) {
@@ -241,7 +245,16 @@ final public class IntSet implements ResourceSet {
 
     @Override
     public Object[] toArray() {
-        throw new UnsupportedOperationException();
+        if(data != null) {
+            Object[] result = new Object[sizeOrData];
+            for(int i=0;i<sizeOrData;++i)
+                result[i] = new ResourceImpl(support, data[i]);
+            return result;
+        }
+        else if(sizeOrData == NO_DATA)
+            return EMPTY_ARRAY;
+        else
+            return new Object[] { new ResourceImpl(support, sizeOrData) };
     }
 
     @Override
@@ -276,4 +289,15 @@ final public class IntSet implements ResourceSet {
         }
     }
 
+    public void forEach(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
+        if (data != null) {
+            for (int i=0;i<sizeOrData;++i)
+                procedure.execute(graph, data[i]);
+        } else if(sizeOrData == NO_DATA) {
+        } else {
+            procedure.execute(graph, sizeOrData);
+        }
+        procedure.finished(graph);
+    }
+
 }
\ No newline at end of file