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.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
19 public class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
20 private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
21 private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
23 public QNameToStatementDefinitionMap() {
24 noRevQNameToSupport = new HashMap<>();
25 qnameToSupport = new HashMap<>();
28 public QNameToStatementDefinitionMap(final int initialCapacity) {
29 noRevQNameToSupport = new HashMap<>(initialCapacity);
30 qnameToSupport = new HashMap<>(initialCapacity);
33 public void put(final QName qname, final StatementSupport<?, ?, ?> stDef) {
34 // HashMap does not guard against nulls
35 qnameToSupport.put(requireNonNull(qname), requireNonNull(stDef));
36 putNoRev(qname, stDef);
39 public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
40 qnameToSupport.putAll(qnameToStmt);
41 qnameToStmt.forEach(this::putNoRev);
44 public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
45 final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
46 if (existing != null) {
50 // XXX: we can (in theory) conflict here if we ever find ourselves needing to have multiple revisions of
51 // statements. These should be equivalent, so no harm done (?)
52 // Anyway, this is how it worked before last refactor.
53 putNoRev(qname, support);
57 private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
58 final QName norev = qname.withoutRevision();
59 noRevQNameToSupport.put(norev != qname ? norev.intern() : qname, support);
63 public StatementSupport<?, ?, ?> get(final QName identifier) {
64 return qnameToSupport.get(identifier);
68 public StatementDefinition getByNamespaceAndLocalName(final XMLNamespace namespace, final String localName) {
69 return noRevQNameToSupport.get(QName.create(namespace, localName));