2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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
8 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.types.rev171204;
10 import com.google.common.annotations.Beta;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.util.ClassLoaderUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Builder for {@link MplsLabel} instances.
25 * @author Robert Varga
29 public final class MplsLabelBuilder {
30 private static final Logger LOG = LoggerFactory.getLogger(MplsLabelBuilder.class);
32 @SuppressWarnings("null")
33 private static final LoadingCache<Entry<ClassLoader, String>, Optional<MplsLabel>> CLASS_CACHE =
34 CacheBuilder.newBuilder()
35 .weakKeys().build(new CacheLoader<Entry<ClassLoader, String>, Optional<MplsLabel>>() {
37 public Optional<MplsLabel> load(final Entry<ClassLoader, String> key) {
38 return loadClass(key.getKey(), key.getValue());
42 private MplsLabelBuilder() {
46 public static MplsLabel getDefaultInstance(final String defaultValue) {
47 if (defaultValue.startsWith("interface ")) {
48 final Optional<MplsLabel> optStatic = CLASS_CACHE.getUnchecked(
49 new SimpleImmutableEntry<>(MplsLabelBuilder.class.getClassLoader(), defaultValue));
50 if (optStatic.isPresent()) {
51 return optStatic.get();
54 final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
56 final Optional<MplsLabel> optThreadLocal = CLASS_CACHE.getUnchecked(
57 new SimpleImmutableEntry<>(tccl, defaultValue));
58 if (optThreadLocal.isPresent()) {
59 return optThreadLocal.get();
64 return new MplsLabel(new MplsLabelGeneralUse(Long.valueOf(defaultValue)));
67 static Optional<MplsLabel> loadClass(final ClassLoader loader, final String key) {
70 cls = ClassLoaderUtils.loadClass(loader, key);
71 } catch (ClassNotFoundException e) {
72 LOG.debug("%s not found in classloader of %s", key, loader);
73 return Optional.empty();
76 final Class<? extends MplsLabelSpecialPurposeValue> cast;
78 cast = cls.asSubclass(MplsLabelSpecialPurposeValue.class);
79 } catch (ClassCastException e) {
80 LOG.warn("%s does not implement %s", MplsLabelSpecialPurposeValue.class);
81 return Optional.empty();
84 return Optional.of(new MplsLabel(cast));