+ public synchronized void put(Policy policy) {
+ policiesId.put(policy.id(), policy);
+ multiMapPut(policiesRic, policy.ric().name(), policy);
+ multiMapPut(policiesService, policy.ownerServiceName(), policy);
+ multiMapPut(policiesType, policy.type().name(), policy);
+ }
+
+ private void multiMapPut(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
+ Map<String, Policy> map = multiMap.get(key);
+ if (map == null) {
+ map = new HashMap<>();
+ multiMap.put(key, map);
+ }
+ map.put(value.id(), value);
+ }
+
+ private void multiMapRemove(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
+ Map<String, Policy> map = multiMap.get(key);
+ if (map != null) {
+ map.remove(value.id());
+ if (map.isEmpty()) {
+ multiMap.remove(key);
+ }
+ }
+ }
+
+ private Collection<Policy> multiMapGet(Map<String, Map<String, Policy>> multiMap, String key) {
+ Map<String, Policy> map = multiMap.get(key);
+ if (map == null) {
+ return new Vector<Policy>();
+ }
+ return map.values();
+ }
+
+ public synchronized Policy get(String id) throws ServiceException {
+ Policy p = policiesId.get(id);
+ if (p == null) {
+ throw new ServiceException("Could not find policy: " + id);
+ }
+ return p;
+ }
+
+ public synchronized Collection<Policy> getAll() {
+ return policiesId.values();
+ }
+
+ public synchronized Collection<Policy> getForService(String service) {
+ return multiMapGet(policiesService, service);
+ }
+
+ public synchronized Collection<Policy> getForRic(String ric) {
+ return multiMapGet(policiesRic, ric);
+ }
+
+ public synchronized Collection<Policy> getForType(String type) {
+ return multiMapGet(policiesType, type);
+ }
+
+ public synchronized Policy removeId(String id) {
+ Policy p = policiesId.get(id);
+ if (p != null) {
+ remove(p);
+ }
+ return p;
+ }
+
+ public synchronized void remove(Policy policy) {
+ policiesId.remove(policy.id());
+ multiMapRemove(policiesRic, policy.ric().name(), policy);
+ multiMapRemove(policiesService, policy.ownerServiceName(), policy);
+ multiMapRemove(policiesType, policy.type().name(), policy);
+ }
+
+ public synchronized int size() {
+ return policiesId.size();