2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.util;
11 import java.util.Collections;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
17 * Utility methods related to managing sets and maps
20 public class SetUtils {
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
28 public static <K1, K2> Set<K2>
29 getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set) {
30 Set<K2> inner = set.get(key);
32 inner = Collections.newSetFromMap(new ConcurrentHashMap<K2, Boolean>());
33 Set<K2> old = set.putIfAbsent(key, inner);