]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntSet.java
index 7c32b8e593e19ea7757355d3c1768c0757abe7c1..3f2e0140bda6f0a2da13dea02e7e24b7a77e8230 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 {
 
@@ -30,8 +32,7 @@ final public class IntSet implements ResourceSet {
 
     public int[] data;
 
-    /** the index after the last entry in the list */
-    public int sizeOrData;
+    private int sizeOrData;
 
     /** the default capacity for new lists */
     protected static final int DEFAULT_CAPACITY = 3;
@@ -40,7 +41,9 @@ final public class IntSet implements ResourceSet {
     
     private static final Object[] EMPTY_ARRAY = new Object[0];
 
-    public IntSet() {
+    public static IntSet EMPTY = new IntSet();
+
+    private IntSet() {
         support = null;
         data = null;
         sizeOrData = NO_DATA;
@@ -287,4 +290,36 @@ 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);
+    }
+    
+    public void serialize(QuerySerializer serializer) {
+        serializer.writeLE(size());
+        forEach(new TIntProcedure() {
+            
+            @Override
+            public boolean execute(int value) {
+                serializer.addResource(value);
+                return true;
+            }
+        });
+    }
+    
+    public static IntSet deserialize(QueryDeserializer deserializer) throws DatabaseException {
+        int size = deserializer.readLE4();
+        IntSet result = new IntSet();
+        for(int i=0;i<size;i++) {
+            result.add(deserializer.readResource());
+        }
+        return result;
+    }
+
 }
\ No newline at end of file