diff options
Diffstat (limited to 'src/org/traccar/database/DataCache.java')
-rw-r--r-- | src/org/traccar/database/DataCache.java | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/org/traccar/database/DataCache.java b/src/org/traccar/database/DataCache.java index f5aa3b1e9..f28f1b838 100644 --- a/src/org/traccar/database/DataCache.java +++ b/src/org/traccar/database/DataCache.java @@ -18,6 +18,8 @@ package org.traccar.database; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; import org.traccar.model.Position; @@ -31,7 +33,7 @@ public class DataCache { // TODO: load latest data from datavase } - public void update(Position position) { + public synchronized void update(Position position) { long device = position.getDeviceId(); positions.put(device, position); if (listeners.containsKey(device)) { @@ -41,6 +43,19 @@ public class DataCache { } } + public synchronized Collection<Position> getInitialState(Collection<Long> devices) { + + List<Position> result = new LinkedList<Position>(); + + for (long device : devices) { + if (positions.containsKey(device)) { + result.add(positions.get(device)); + } + } + + return result; + } + public static interface DataCacheListener { public void onUpdate(Position position); } @@ -51,7 +66,7 @@ public class DataCache { } } - public void addListener(long device, DataCacheListener listener) { + public synchronized void addListener(long device, DataCacheListener listener) { if (!listeners.containsKey(device)) { listeners.put(device, new HashSet<DataCacheListener>()); } @@ -64,7 +79,7 @@ public class DataCache { } } - public void removeListener(long device, DataCacheListener listener) { + public synchronized void removeListener(long device, DataCacheListener listener) { if (!listeners.containsKey(device)) { listeners.put(device, new HashSet<DataCacheListener>()); } |