1 package org.simantics.document.server;
3 import java.util.Collection;
4 import java.util.Iterator;
6 import java.util.Map.Entry;
8 import org.simantics.db.procedure.Listener;
9 import org.simantics.utils.datastructures.Pair;
11 import gnu.trove.map.hash.THashMap;
13 public class DocumentHistoryCollector {
15 static Map<String, DocumentHistory> histories = new THashMap<String, DocumentHistory>();
17 private static DocumentHistory getHistory(String location, boolean create) {
19 synchronized(histories) {
21 DocumentHistory history = histories.get(location);
22 if(history == null && create) {
23 history = new DocumentHistory();
25 histories.put(location, history);
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()) {
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);
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);