9d7ac23c74e688614d677de8f2f5cd2868fc6e83
[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.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;
18
19 public class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
20     private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
21     private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
22
23     public QNameToStatementDefinitionMap() {
24         noRevQNameToSupport = new HashMap<>();
25         qnameToSupport = new HashMap<>();
26     }
27
28     public QNameToStatementDefinitionMap(final int initialCapacity) {
29         noRevQNameToSupport = new HashMap<>(initialCapacity);
30         qnameToSupport = new HashMap<>(initialCapacity);
31     }
32
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);
37     }
38
39     public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
40         qnameToSupport.putAll(qnameToStmt);
41         qnameToStmt.forEach(this::putNoRev);
42     }
43
44     public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
45         final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
46         if (existing != null) {
47             return existing;
48         }
49
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);
54         return null;
55     }
56
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);
60     }
61
62     @Override
63     public StatementSupport<?, ?, ?> get(final QName identifier) {
64         return qnameToSupport.get(identifier);
65     }
66
67     @Override
68     public StatementDefinition getByNamespaceAndLocalName(final XMLNamespace namespace, final String localName) {
69         return noRevQNameToSupport.get(QName.create(namespace, localName));
70     }
71 }