]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentHistoryCollector.java
Removed graph compilation nature from org.simantics.jdbc
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / DocumentHistoryCollector.java
1 package org.simantics.document.server;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.Map;
6 import java.util.Map.Entry;
7
8 import org.simantics.db.procedure.Listener;
9 import org.simantics.utils.datastructures.Pair;
10
11 import gnu.trove.map.hash.THashMap;
12
13 public class DocumentHistoryCollector {
14
15         static Map<String, DocumentHistory> histories = new THashMap<String, DocumentHistory>();
16
17         private static DocumentHistory getHistory(String location, boolean create) {
18
19                 synchronized(histories) {
20
21                         DocumentHistory history =  histories.get(location);
22                         if(history == null && create) {
23                                 history = new DocumentHistory();
24                                 pruneHistories();
25                                 histories.put(location, history);
26                         }
27                         return history;
28                         
29                 }
30
31         }
32         
33         private static void pruneHistories() {
34                 synchronized (histories) {
35                         // remove histories with no listeners
36                         Iterator<Map.Entry<String, DocumentHistory>> iter = histories.entrySet().iterator();
37                         while (iter.hasNext()) {
38                         Entry<String, DocumentHistory> entry = iter.next();
39                         DocumentHistory entryHistory = entry.getValue();
40                         synchronized(entryHistory) {
41                                 if (!entryHistory.hasListeners()) {
42                                         iter.remove();
43                                 }
44                         }
45                 }
46                 }
47         }
48
49         public static Pair<Integer, Collection<JSONObject>> readChanges(String location, int sequenceNumber) {
50                 DocumentHistory history = getHistory(location, false);
51                 if (history != null) {
52                         return history.readChanges(sequenceNumber);
53                 } else {
54                         return null;
55                 }
56         }
57
58         public static Pair<Integer, Collection<JSONObject>> getContent(Listener<Integer> listener, String location, int sequenceNumber) {
59                 DocumentHistory history = getHistory(location, true);
60                 return history.getContent(listener, location, sequenceNumber);
61         }
62 }