3fbf513f5f055b88d476414d0288511719e47b4c
[yangtools.git] / yang / 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 com.google.common.base.Preconditions;
11 import java.net.URI;
12 import java.util.HashMap;
13 import java.util.Map;
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;
19
20 public 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         Preconditions.checkNotNull(qname);
37         Preconditions.checkNotNull(stDef);
38
39         qnameToSupport.put(qname, stDef);
40         putNoRev(qname, stDef);
41     }
42
43     public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
44         qnameToSupport.putAll(qnameToStmt);
45         qnameToStmt.forEach((this::putNoRev));
46     }
47
48     public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
49         final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
50         if (existing != null) {
51             return existing;
52         }
53
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);
58         return null;
59     }
60
61     private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
62         final QName norev;
63         if (qname.getRevision() != null) {
64             norev = QName.create(qname.getNamespace(), null, qname.getLocalName()).intern();
65         } else {
66             norev = qname;
67         }
68         noRevQNameToSupport.put(norev, support);
69     }
70
71     @Nullable
72     @Override
73     public StatementSupport<?, ?, ?> get(@Nonnull final QName identifier) {
74         return qnameToSupport.get(identifier);
75     }
76
77     @Nullable
78     @Override
79     public StatementDefinition getByNamespaceAndLocalName(@Nonnull final URI namespace,
80             @Nonnull final String localName) {
81         return noRevQNameToSupport.get(QName.create(namespace, null, localName));
82     }
83
84 }