diff options
author | Abyss777 <abyss@fox5.ru> | 2017-07-26 17:52:27 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2017-07-26 17:52:27 +0500 |
commit | d31f68d80a8e3ae75fa0c2f02b9ca258486a3cad (patch) | |
tree | d44c13177de39aad1d0907a4150dfa6e440ab02f /src/org/traccar/database/BaseObjectManager.java | |
parent | 82edf57e1b93d5a5ca34294dc62e3620889c2341 (diff) | |
download | trackermap-server-d31f68d80a8e3ae75fa0c2f02b9ca258486a3cad.tar.gz trackermap-server-d31f68d80a8e3ae75fa0c2f02b9ca258486a3cad.tar.bz2 trackermap-server-d31f68d80a8e3ae75fa0c2f02b9ca258486a3cad.zip |
- Implement base manager classes as generics
- Remame getDeviceById and getDeviceByUniqueId functions
Diffstat (limited to 'src/org/traccar/database/BaseObjectManager.java')
-rw-r--r-- | src/org/traccar/database/BaseObjectManager.java | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/org/traccar/database/BaseObjectManager.java b/src/org/traccar/database/BaseObjectManager.java index 92cfd9b6b..cc1dbde5f 100644 --- a/src/org/traccar/database/BaseObjectManager.java +++ b/src/org/traccar/database/BaseObjectManager.java @@ -27,14 +27,14 @@ import java.util.concurrent.ConcurrentHashMap; import org.traccar.helper.Log; import org.traccar.model.BaseModel; -public class BaseObjectManager { +public class BaseObjectManager<T extends BaseModel> { private final DataManager dataManager; - private Map<Long, BaseModel> items; - private Class<? extends BaseModel> baseClass; + private Map<Long, T> items; + private Class<T> baseClass; - protected BaseObjectManager(DataManager dataManager, Class<? extends BaseModel> baseClass) { + protected BaseObjectManager(DataManager dataManager, Class<T> baseClass) { this.dataManager = dataManager; this.baseClass = baseClass; refreshItems(); @@ -44,23 +44,23 @@ public class BaseObjectManager { return dataManager; } - protected final Class<? extends BaseModel> getBaseClass() { + protected final Class<T> getBaseClass() { return baseClass; } - public BaseModel getById(long itemId) { + public T getById(long itemId) { return items.get(itemId); } public void refreshItems() { if (dataManager != null) { try { - Collection<? extends BaseModel> databaseItems = dataManager.getObjects(baseClass); + Collection<T> databaseItems = dataManager.getObjects(baseClass); if (items == null) { items = new ConcurrentHashMap<>(databaseItems.size()); } Set<Long> databaseItemIds = new HashSet<>(); - for (BaseModel item : databaseItems) { + for (T item : databaseItems) { databaseItemIds.add(item.getId()); if (items.containsKey(item.getId())) { updateCachedItem(item); @@ -79,20 +79,20 @@ public class BaseObjectManager { } } - protected void addNewItem(BaseModel item) { + protected void addNewItem(T item) { items.put(item.getId(), item); } - public void addItem(BaseModel item) throws SQLException { + public void addItem(T item) throws SQLException { dataManager.addObject(item); addNewItem(item); } - protected void updateCachedItem(BaseModel item) { + protected void updateCachedItem(T item) { items.put(item.getId(), item); } - public void updateItem(BaseModel item) throws SQLException { + public void updateItem(T item) throws SQLException { dataManager.updateObject(item); updateCachedItem(item); } @@ -109,10 +109,10 @@ public class BaseObjectManager { } } - public final <T> Collection<T> getItems(Class<T> clazz, Set<Long> itemIds) { + public final Collection<T> getItems(Set<Long> itemIds) { Collection<T> result = new LinkedList<>(); for (long itemId : itemIds) { - result.add((T) getById(itemId)); + result.add(getById(itemId)); } return result; } |