2 * Copyright (c) 2014, 2015 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
8 package org.opendaylight.controller.cluster.datastore.node.utils;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import java.util.Objects;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.common.QName;
21 public final class QNameFactory {
22 public static final class Key implements Immutable {
23 private final @NonNull String localName;
24 private final @NonNull String namespace;
25 private final @Nullable String revision;
27 public Key(final String localName, final String namespace, final String revision) {
28 this.localName = requireNonNull(localName);
29 this.namespace = requireNonNull(namespace);
30 this.revision = revision;
34 public int hashCode() {
35 return Objects.hash(localName, namespace, revision);
39 public boolean equals(final Object obj) {
43 if (!(obj instanceof Key)) {
46 final Key other = (Key) obj;
47 return localName.equals(other.localName) && namespace.equals(other.namespace)
48 && Objects.equals(revision, other.revision);
52 return revision != null ? QName.create(namespace, revision, localName) : QName.create(namespace, localName);
56 private static final int MAX_QNAME_CACHE_SIZE = Integer.getInteger(
57 "org.opendaylight.controller.cluster.datastore.node.utils.qname-cache.max-size", 10000);
59 private static final LoadingCache<String, QName> STRING_CACHE = CacheBuilder.newBuilder()
60 .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader<String, QName>() {
62 public QName load(final String key) {
63 return QName.create(key).intern();
67 private static final LoadingCache<Key, QName> KEY_CACHE = CacheBuilder.newBuilder()
68 .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader<Key, QName>() {
70 public QName load(final Key key) {
71 return key.toQName().intern();
75 private QNameFactory() {
80 public static QName create(final String name) {
81 return STRING_CACHE.getUnchecked(name);
84 public static QName create(final Key key) {
85 return KEY_CACHE.getUnchecked(key);