X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2FQNameFactory.java;h=f1b51ce2617db4ebee53d3765fd96bc9d9449b50;hp=c63266ff08e218fc6180fdf1cae61e990677eace;hb=a69604185f71923bdfc16d2d056490fff9a8d90e;hpb=48904394c82c33384d9da9f0bf233ca950a832fb diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java index c63266ff08..f1b51ce261 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/QNameFactory.java @@ -1,38 +1,87 @@ /* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. * - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.datastore.node.utils; +import static java.util.Objects.requireNonNull; + import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; +import java.util.Objects; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.common.QName; -public class QNameFactory { +public final class QNameFactory { + public static final class Key implements Immutable { + private final @NonNull String localName; + private final @NonNull String namespace; + private final @Nullable String revision; + + public Key(final String localName, final String namespace, final String revision) { + this.localName = requireNonNull(localName); + this.namespace = requireNonNull(namespace); + this.revision = revision; + } - private static final int MAX_QNAME_CACHE_SIZE = 10000; + @Override + public int hashCode() { + return Objects.hash(localName, namespace, revision); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof Key)) { + return false; + } + final Key other = (Key) obj; + return localName.equals(other.localName) && namespace.equals(other.namespace) + && Objects.equals(revision, other.revision); + } + + QName toQName() { + return revision != null ? QName.create(namespace, revision, localName) : QName.create(namespace, localName); + } + } - private static final LoadingCache CACHE = CacheBuilder.newBuilder() - .maximumSize(MAX_QNAME_CACHE_SIZE) - .softValues() - .build( - new CacheLoader() { + private static final int MAX_QNAME_CACHE_SIZE = Integer.getInteger( + "org.opendaylight.controller.cluster.datastore.node.utils.qname-cache.max-size", 10000); + + private static final LoadingCache STRING_CACHE = CacheBuilder.newBuilder() + .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader() { @Override - public QName load(String key) { - return QName.create(key); + public QName load(final String key) { + return QName.create(key).intern(); } - } - ); + }); + + private static final LoadingCache KEY_CACHE = CacheBuilder.newBuilder() + .maximumSize(MAX_QNAME_CACHE_SIZE).weakValues().build(new CacheLoader() { + @Override + public QName load(final Key key) { + return key.toQName().intern(); + } + }); + private QNameFactory() { + + } + + @Deprecated + public static QName create(final String name) { + return STRING_CACHE.getUnchecked(name); + } - public static QName create(String name){ - return CACHE.getUnchecked(name); + public static QName create(final Key key) { + return KEY_CACHE.getUnchecked(key); } }