X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Frepository%2FPolicies.java;h=c910dd590d42d7c8873cdbd00b9beddae0e585fc;hb=b66dcce5210e25b2571036becb6f0e7b0c23e1b2;hp=daaa193cebe87d11d2e4f6b694aa90f861078c20;hpb=8831a02bce715562f3cacce1691bf4d9d3af206b;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java index daaa193c..c910dd59 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java @@ -21,25 +21,18 @@ package org.oransc.policyagent.repository; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Vector; - +import java.util.Set; import org.oransc.policyagent.exceptions.ServiceException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class Policies { - private static final Logger logger = LoggerFactory.getLogger(Policies.class); - private Map policiesId = new HashMap<>(); private Map> policiesRic = new HashMap<>(); private Map> policiesService = new HashMap<>(); private Map> policiesType = new HashMap<>(); - public Policies() { - } - public synchronized void put(Policy policy) { policiesId.put(policy.id(), policy); multiMapPut(policiesRic, policy.ric().name(), policy); @@ -48,12 +41,7 @@ public class Policies { } private void multiMapPut(Map> multiMap, String key, Policy value) { - Map map = multiMap.get(key); - if (map == null) { - map = new HashMap<>(); - multiMap.put(key, map); - } - map.put(value.id(), value); + multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value); } private void multiMapRemove(Map> multiMap, String key, Policy value) { @@ -69,12 +57,20 @@ public class Policies { private Collection multiMapGet(Map> multiMap, String key) { Map map = multiMap.get(key); if (map == null) { - return new Vector(); + return Collections.emptyList(); } - return map.values(); + return Collections.unmodifiableCollection(map.values()); + } + + public synchronized boolean containsPolicy(String id) { + return policiesId.containsKey(id); } - public synchronized Policy get(String id) throws ServiceException { + public synchronized Policy get(String id) { + return policiesId.get(id); + } + + public synchronized Policy getPolicy(String id) throws ServiceException { Policy p = policiesId.get(id); if (p == null) { throw new ServiceException("Could not find policy: " + id); @@ -83,7 +79,7 @@ public class Policies { } public synchronized Collection getAll() { - return policiesId.values(); + return Collections.unmodifiableCollection(policiesId.values()); } public synchronized Collection getForService(String service) { @@ -117,10 +113,10 @@ public class Policies { return policiesId.size(); } - public void clear() { - for (String id : policiesId.keySet()) { - removeId(id); + public synchronized void clear() { + while (policiesId.size() > 0) { + Set keys = policiesId.keySet(); + removeId(keys.iterator().next()); } } - }