StatementSupport is not a StatementDefinition
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / QNameToStatementDefinitionMap.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.source;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.HashMap;
13 import java.util.Map;
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;
19
20 public final class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
21     private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
22     private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
23
24     public QNameToStatementDefinitionMap() {
25         noRevQNameToSupport = new HashMap<>();
26         qnameToSupport = new HashMap<>();
27     }
28
29     public QNameToStatementDefinitionMap(final int initialCapacity) {
30         noRevQNameToSupport = new HashMap<>(initialCapacity);
31         qnameToSupport = new HashMap<>(initialCapacity);
32     }
33
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);
38     }
39
40     public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
41         qnameToSupport.putAll(qnameToStmt);
42         qnameToStmt.forEach(this::putNoRev);
43     }
44
45     public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
46         final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
47         if (existing != null) {
48             return existing;
49         }
50
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);
55         return null;
56     }
57
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);
61     }
62
63     @Override
64     public StatementDefinition get(final QName identifier) {
65         return definitionOf(getSupport(identifier));
66     }
67
68     @Override
69     public StatementDefinition getByNamespaceAndLocalName(final XMLNamespace namespace, final String localName) {
70         return definitionOf(noRevQNameToSupport.get(QName.create(namespace, localName)));
71     }
72
73     /**
74      * Returns StatementSupport with specified QName.
75      *
76      * @param identifier QName of requested statement
77      * @return StatementSupport
78      */
79     public @Nullable StatementSupport<?, ?, ?> getSupport(final QName identifier) {
80         return qnameToSupport.get(requireNonNull(identifier));
81     }
82
83     private static @Nullable StatementDefinition definitionOf(final @Nullable StatementSupport<?, ?, ?> support) {
84         return support != null ? support.definition() : null;
85     }
86 }