Do not format QNames to string on input
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / QNameFactory.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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.controller.cluster.datastore.node.utils;
9
10 import static java.util.Objects.requireNonNull;
11
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;
20
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;
26
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;
31         }
32
33         @Override
34         public int hashCode() {
35             return Objects.hash(localName, namespace, revision);
36         }
37
38         @Override
39         public boolean equals(final Object obj) {
40             if (this == obj) {
41                 return true;
42             }
43             if (!(obj instanceof Key)) {
44                 return false;
45             }
46             final Key other = (Key) obj;
47             return localName.equals(other.localName) && namespace.equals(other.namespace)
48                     && Objects.equals(revision, other.revision);
49         }
50
51         QName toQName() {
52             return revision != null ? QName.create(namespace, revision, localName) : QName.create(namespace, localName);
53         }
54     }
55
56     private static final int MAX_QNAME_CACHE_SIZE = Integer.getInteger(
57         "org.opendaylight.controller.cluster.datastore.node.utils.qname-cache.max-size", 10000);
58
59     private static final LoadingCache<String, QName> STRING_CACHE = CacheBuilder.newBuilder()
60             .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader<String, QName>() {
61                 @Override
62                 public QName load(final String key) {
63                     return QName.create(key).intern();
64                 }
65             });
66
67     private static final LoadingCache<Key, QName> KEY_CACHE = CacheBuilder.newBuilder()
68             .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader<Key, QName>() {
69                 @Override
70                 public QName load(final Key key) {
71                     return key.toQName().intern();
72                 }
73             });
74
75     private QNameFactory() {
76
77     }
78
79     @Deprecated
80     public static QName create(final String name) {
81         return STRING_CACHE.getUnchecked(name);
82     }
83
84     public static QName create(final Key key) {
85         return KEY_CACHE.getUnchecked(key);
86     }
87 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.