Java ConcurrentMap getNestedSet(K1 key, ConcurrentMap> set)

Here you can find the source of getNestedSet(K1 key, ConcurrentMap> set)

Description

Get and/or allocate as needed a nested concurrent set inside a concurrent map in a threadsafe way.

License

Open Source License

Parameter

Parameter Description
key the key to the concurrent map
set the concurrent map

Return

the nested concurrent set

Declaration

public static <K1, K2> Set<K2> getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set) 

Method Source Code


//package com.java2s;
/*//  w  w w. j a  v  a  2s  . c om
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Main {
    /**
     * Get and/or allocate as needed a nested concurrent set inside a concurrent
     * map in a threadsafe way.
     * @param key the key to the concurrent map
     * @param set the concurrent map
     * @return the nested concurrent set
     */
    public static <K1, K2> Set<K2> getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set) {
        Set<K2> inner = set.get(key);
        if (inner == null) {
            inner = Collections.newSetFromMap(new ConcurrentHashMap<K2, Boolean>());
            Set<K2> old = set.putIfAbsent(key, inner);
            if (old != null)
                inner = old;
        }
        return inner;
    }
}

Related

  1. get(Class clazz)
  2. getCache(String cacheName)
  3. getConcurrentMap(int... size)
  4. getContextPath()
  5. getFilesystem(String id)
  6. getNewLogFactor(String factorName)
  7. getSingleInstance(Class classType)
  8. getTypeFromKind(String kind)
  9. is(Class clazz, Collection> clazzes, ConcurrentMap, Boolean> cache)