2f960c034dbe30e91bd6e639c5b32ef09649cfa4
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractBindingLoadingAdapter.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14
15 @NonNullByDefault
16 abstract class AbstractBindingLoadingAdapter<D, K, V> extends AbstractBindingAdapter<D> {
17     private final LoadingCache<K, V> proxies = CacheBuilder.newBuilder().weakKeys().weakValues().build(
18         new CacheLoader<K, V>() {
19             @Override
20             public V load(final K key) {
21                 return loadAdapter(key);
22             }
23         });
24
25     AbstractBindingLoadingAdapter(final BindingToNormalizedNodeCodec codec, final D delegate) {
26         super(codec, delegate);
27     }
28
29     final V getAdapter(final K key) {
30         return proxies.getUnchecked(key);
31     }
32
33     abstract V loadAdapter(K key);
34 }