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;
13 import java.util.HashMap;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
21 public class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
22 private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
23 private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
25 public QNameToStatementDefinitionMap() {
26 noRevQNameToSupport = new HashMap<>();
27 qnameToSupport = new HashMap<>();
30 public QNameToStatementDefinitionMap(final int initialCapacity) {
31 noRevQNameToSupport = new HashMap<>(initialCapacity);
32 qnameToSupport = new HashMap<>(initialCapacity);
35 public void put(final QName qname, final StatementSupport<?, ?, ?> stDef) {
36 // HashMap does not guard against nulls
37 qnameToSupport.put(requireNonNull(qname), requireNonNull(stDef));
38 putNoRev(qname, stDef);
41 public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
42 qnameToSupport.putAll(qnameToStmt);
43 qnameToStmt.forEach(this::putNoRev);
46 public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
47 final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
48 if (existing != null) {
52 // XXX: we can (in theory) conflict here if we ever find ourselves needing to have multiple revisions of
53 // statements. These should be equivalent, so no harm done (?)
54 // Anyway, this is how it worked before last refactor.
55 putNoRev(qname, support);
59 private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
61 if (qname.getRevision() != null) {
62 norev = QName.create(qname.getNamespace(), null, qname.getLocalName()).intern();
66 noRevQNameToSupport.put(norev, support);
71 public StatementSupport<?, ?, ?> get(@Nonnull final QName identifier) {
72 return qnameToSupport.get(identifier);
77 public StatementDefinition getByNamespaceAndLocalName(@Nonnull final URI namespace,
78 @Nonnull final String localName) {
79 return noRevQNameToSupport.get(QName.create(namespace, null, localName));