]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentHistoryCollector.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / DocumentHistoryCollector.java
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentHistoryCollector.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentHistoryCollector.java
new file mode 100644 (file)
index 0000000..b759d38
--- /dev/null
@@ -0,0 +1,62 @@
+package org.simantics.document.server;\r
+\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+public class DocumentHistoryCollector {\r
+\r
+       static Map<String, DocumentHistory> histories = new THashMap<String, DocumentHistory>();\r
+\r
+       private static DocumentHistory getHistory(String location, boolean create) {\r
+\r
+               synchronized(histories) {\r
+\r
+                       DocumentHistory history =  histories.get(location);\r
+                       if(history == null && create) {\r
+                               history = new DocumentHistory();\r
+                               pruneHistories();\r
+                               histories.put(location, history);\r
+                       }\r
+                       return history;\r
+                       \r
+               }\r
+\r
+       }\r
+       \r
+       private static void pruneHistories() {\r
+               synchronized (histories) {\r
+                       // remove histories with no listeners\r
+                       Iterator<Map.Entry<String, DocumentHistory>> iter = histories.entrySet().iterator();\r
+                       while (iter.hasNext()) {\r
+                       Entry<String, DocumentHistory> entry = iter.next();\r
+                       DocumentHistory entryHistory = entry.getValue();\r
+                       synchronized(entryHistory) {\r
+                               if (!entryHistory.hasListeners()) {\r
+                                       iter.remove();\r
+                               }\r
+                       }\r
+               }\r
+               }\r
+       }\r
+\r
+       public static Pair<Integer, Collection<JSONObject>> readChanges(String location, int sequenceNumber) {\r
+               DocumentHistory history = getHistory(location, false);\r
+               if (history != null) {\r
+                       return history.readChanges(sequenceNumber);\r
+               } else {\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       public static Pair<Integer, Collection<JSONObject>> getContent(Listener<Integer> listener, String location, int sequenceNumber) {\r
+               DocumentHistory history = getHistory(location, true);\r
+               return history.getContent(listener, location, sequenceNumber);\r
+       }\r
+}
\ No newline at end of file