]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntSet.java
index a84f8fc5553aa04c31537ecedad7ec4e182a7666..813677d53265d36079395fd471f160e69237680b 100644 (file)
@@ -18,9 +18,12 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.Set;
 
+import org.simantics.db.ReadGraph;
 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;
 
 
@@ -37,13 +40,15 @@ 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 IntSet() {
-        support = null;
-        data = null;
-        sizeOrData = NO_DATA;
+    public static IntSet EMPTY = new IntSet();
+    
+    private IntSet() {
+       support = null;
     }
-
+    
     public IntSet(QuerySupport support) {
         this.support = support.getSupport();
         data = null;
@@ -241,7 +246,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
@@ -275,5 +289,17 @@ 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