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 com.google.common.base.Preconditions;
12 import java.util.HashMap;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
20 public 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 Preconditions.checkNotNull(qname);
37 Preconditions.checkNotNull(stDef);
39 qnameToSupport.put(qname, stDef);
40 putNoRev(qname, stDef);
43 public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
44 qnameToSupport.putAll(qnameToStmt);
45 qnameToStmt.forEach((this::putNoRev));
48 public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
49 final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
50 if (existing != null) {
54 // XXX: we can (in theory) conflict here if we ever find ourselves needing to have multiple revisions of
55 // statements. These should be equivalent, so no harm done (?)
56 // Anyway, this is how it worked before last refactor.
57 putNoRev(qname, support);
61 private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
63 if (qname.getRevision() != null) {
64 norev = QName.create(qname.getNamespace(), null, qname.getLocalName()).intern();
68 noRevQNameToSupport.put(norev, support);
73 public StatementSupport<?, ?, ?> get(@Nonnull final QName identifier) {
74 return qnameToSupport.get(identifier);
79 public StatementDefinition getByNamespaceAndLocalName(@Nonnull final URI namespace, @Nonnull final String localName) {
80 return noRevQNameToSupport.get(QName.create(namespace, null, localName));