blob: 3b44ad415b0ca766d404b5ecd96fc431d1f873bc [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070016package org.onlab.util;
17
18import java.util.concurrent.ConcurrentHashMap;
19import java.util.concurrent.ConcurrentMap;
20
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070021import org.apache.commons.lang3.concurrent.ConcurrentInitializer;
22
23/**
24 * Creates an instance of new ConcurrentHashMap on each {@link #get()} call.
25 * <p>
26 * To be used with
27 * {@link org.apache.commons.lang3.concurrent.ConcurrentUtils#createIfAbsent()
28 * ConcurrentUtils#createIfAbsent}
29 *
30 * @param <K> ConcurrentHashMap key type
31 * @param <V> ConcurrentHashMap value type
32 */
33public final class NewConcurrentHashMap<K, V>
34 implements ConcurrentInitializer<ConcurrentMap<K, V>> {
35
36 public static final NewConcurrentHashMap<?, ?> INSTANCE = new NewConcurrentHashMap<>();
37
38 @SuppressWarnings("unchecked")
39 public static <K, V> NewConcurrentHashMap<K, V> ifNeeded() {
40 return (NewConcurrentHashMap<K, V>) INSTANCE;
41 }
42
43 @Override
Yuta HIGUCHIe66d10e2014-10-14 16:06:13 -070044 public ConcurrentMap<K, V> get() {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070045 return new ConcurrentHashMap<>();
46 }
47}