Integrating FAAS renderer with the faas fabric mapping services. Also,
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / util / SetUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.util;
10
11 import java.util.Collections;
12 import java.util.Set;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15
16 /**
17  * Utility methods related to managing sets and maps
18  * @author readams
19  */
20 public class SetUtils {
21     /**
22      * Get and/or allocate as needed a nested concurrent set inside a concurrent
23      * map in a threadsafe way.
24      * @param key the key to the concurrent map
25      * @param set the concurrent map
26      * @return the nested concurrent set
27      */
28     public static <K1, K2> Set<K2> 
29             getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set) {
30         Set<K2> inner = set.get(key);
31         if (inner == null) {
32             inner = Collections.newSetFromMap(new ConcurrentHashMap<K2, Boolean>());
33             Set<K2> old = set.putIfAbsent(key, inner);
34             if (old != null)
35                 inner = old;
36         }
37         return inner;
38     }
39 }