2 * Copyright (c) 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.yangtools.yang.parser.spi.source;
10 import static java.util.Objects.requireNonNull;
12 import java.util.HashMap;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.XMLNamespace;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
20 public final class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
21 private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
22 private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
24 public QNameToStatementDefinitionMap() {
25 noRevQNameToSupport = new HashMap<>();
26 qnameToSupport = new HashMap<>();
29 public QNameToStatementDefinitionMap(final int initialCapacity) {
30 noRevQNameToSupport = new HashMap<>(initialCapacity);
31 qnameToSupport = new HashMap<>(initialCapacity);
34 public void put(final QName qname, final StatementSupport<?, ?, ?> stDef) {
35 // HashMap does not guard against nulls
36 qnameToSupport.put(requireNonNull(qname), requireNonNull(stDef));
37 putNoRev(qname, stDef);
40 public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
41 qnameToSupport.putAll(qnameToStmt);
42 qnameToStmt.forEach(this::putNoRev);
45 public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
46 final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
47 if (existing != null) {
51 // XXX: we can (in theory) conflict here if we ever find ourselves needing to have multiple revisions of
52 // statements. These should be equivalent, so no harm done (?)
53 // Anyway, this is how it worked before last refactor.
54 putNoRev(qname, support);
58 private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
59 final QName norev = qname.withoutRevision();
60 noRevQNameToSupport.put(norev != qname ? norev.intern() : qname, support);
64 public StatementDefinition get(final QName identifier) {
65 return definitionOf(getSupport(identifier));
69 public StatementDefinition getByNamespaceAndLocalName(final XMLNamespace namespace, final String localName) {
70 return definitionOf(noRevQNameToSupport.get(QName.create(namespace, localName)));
74 * Returns StatementSupport with specified QName.
76 * @param identifier QName of requested statement
77 * @return StatementSupport
79 public @Nullable StatementSupport<?, ?, ?> getSupport(final QName identifier) {
80 return qnameToSupport.get(requireNonNull(identifier));
83 private static @Nullable StatementDefinition definitionOf(final @Nullable StatementSupport<?, ?, ?> support) {
84 return support != null ? support.definition() : null;