package org.oransc.enrichment.repository;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.oransc.enrichment.exceptions.ServiceException;
/**
- * Dynamic representation of all EI Jobs in the system.
+ * Dynamic representation of all existing EI jobs.
*/
public class EiJobs {
private Map<String, EiJob> allEiJobs = new HashMap<>();
- private Map<String, Map<String, EiJob>> jobsByType = new HashMap<>();
+
+ private MultiMap<EiJob> jobsByType = new MultiMap<>();
+ private MultiMap<EiJob> jobsByOwner = new MultiMap<>();
public synchronized void put(EiJob job) {
allEiJobs.put(job.id(), job);
- multiMapPut(this.jobsByType, job.typeId(), job);
+ jobsByType.put(job.typeId(), job.id(), job);
+ jobsByOwner.put(job.owner(), job.id(), job);
}
public synchronized Collection<EiJob> getJobs() {
public synchronized EiJob getJob(String id) throws ServiceException {
EiJob ric = allEiJobs.get(id);
if (ric == null) {
- throw new ServiceException("Could not find EI Job: " + id);
+ throw new ServiceException("Could not find EI job: " + id);
}
return ric;
}
public synchronized Collection<EiJob> getJobsForType(String typeId) {
- return multiMapGet(this.jobsByType, typeId);
+ return jobsByType.get(typeId);
+ }
+
+ public synchronized Collection<EiJob> getJobsForType(EiType type) {
+ return jobsByType.get(type.getId());
+ }
+
+ public synchronized Collection<EiJob> getJobsForOwner(String owner) {
+ return jobsByOwner.get(owner);
}
public synchronized EiJob get(String id) {
public synchronized void remove(EiJob job) {
this.allEiJobs.remove(job.id());
- multiMapRemove(this.jobsByType, job.typeId(), job);
+ jobsByType.remove(job.typeId(), job.id());
+ jobsByOwner.remove(job.owner(), job.id());
}
public synchronized int size() {
public synchronized void clear() {
this.allEiJobs.clear();
- }
-
- private void multiMapPut(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
- multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value);
- }
-
- private void multiMapRemove(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
- Map<String, EiJob> map = multiMap.get(key);
- if (map != null) {
- map.remove(value.id());
- if (map.isEmpty()) {
- multiMap.remove(key);
- }
- }
- }
-
- private Collection<EiJob> multiMapGet(Map<String, Map<String, EiJob>> multiMap, String key) {
- Map<String, EiJob> map = multiMap.get(key);
- if (map == null) {
- return Collections.emptyList();
- }
- return new Vector<>(map.values());
+ this.jobsByType.clear();
+ jobsByOwner.clear();
}
}