From cefba4b9227c8aeb96e290f70689960541755f0d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 22 Apr 2015 23:00:35 +1200 Subject: Add latest position cache --- src/org/traccar/database/DataCache.java | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/org/traccar/database/DataCache.java (limited to 'src/org/traccar/database/DataCache.java') diff --git a/src/org/traccar/database/DataCache.java b/src/org/traccar/database/DataCache.java new file mode 100644 index 000000000..f5aa3b1e9 --- /dev/null +++ b/src/org/traccar/database/DataCache.java @@ -0,0 +1,74 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.database; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.traccar.model.Position; + +public class DataCache { + + private final Map positions = new HashMap(); + private final Map> listeners = new HashMap>(); + + public DataCache(DataManager dataManager) { + // TODO: load latest data from datavase + } + + public void update(Position position) { + long device = position.getDeviceId(); + positions.put(device, position); + if (listeners.containsKey(device)) { + for (DataCacheListener listener : listeners.get(device)) { + listener.onUpdate(position); + } + } + } + + public static interface DataCacheListener { + public void onUpdate(Position position); + } + + public void addListener(Collection devices, DataCacheListener listener) { + for (long device : devices) { + addListener(device, listener); + } + } + + public void addListener(long device, DataCacheListener listener) { + if (!listeners.containsKey(device)) { + listeners.put(device, new HashSet()); + } + listeners.get(device).add(listener); + } + + public void removeListener(Collection devices, DataCacheListener listener) { + for (long device : devices) { + removeListener(device, listener); + } + } + + public void removeListener(long device, DataCacheListener listener) { + if (!listeners.containsKey(device)) { + listeners.put(device, new HashSet()); + } + listeners.get(device).remove(listener); + } + +} -- cgit v1.2.3