2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.enrichment.repository;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
27 import java.util.Vector;
29 import org.oransc.enrichment.exceptions.ServiceException;
32 * Dynamic representation of all EI Jobs in the system.
35 private Map<String, EiJob> allEiJobs = new HashMap<>();
36 private Map<String, Map<String, EiJob>> jobsByType = new HashMap<>();
38 public synchronized void put(EiJob job) {
39 allEiJobs.put(job.id(), job);
40 multiMapPut(this.jobsByType, job.typeId(), job);
43 public synchronized Collection<EiJob> getJobs() {
44 return new Vector<>(allEiJobs.values());
47 public synchronized EiJob getJob(String id) throws ServiceException {
48 EiJob ric = allEiJobs.get(id);
50 throw new ServiceException("Could not find EI Job: " + id);
55 public synchronized Collection<EiJob> getJobsForType(String typeId) {
56 return multiMapGet(this.jobsByType, typeId);
59 public synchronized EiJob get(String id) {
60 return allEiJobs.get(id);
63 public synchronized EiJob remove(String id) {
64 EiJob job = allEiJobs.get(id);
71 public synchronized void remove(EiJob job) {
72 this.allEiJobs.remove(job.id());
73 multiMapRemove(this.jobsByType, job.typeId(), job);
76 public synchronized int size() {
77 return allEiJobs.size();
80 public synchronized void clear() {
81 this.allEiJobs.clear();
84 private void multiMapPut(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
85 multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value);
88 private void multiMapRemove(Map<String, Map<String, EiJob>> multiMap, String key, EiJob value) {
89 Map<String, EiJob> map = multiMap.get(key);
91 map.remove(value.id());
98 private Collection<EiJob> multiMapGet(Map<String, Map<String, EiJob>> multiMap, String key) {
99 Map<String, EiJob> map = multiMap.get(key);
101 return Collections.emptyList();
103 return new Vector<>(map.values());