Cleanup use of Guava library
[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 static java.util.Objects.requireNonNull;
11
12 import java.net.URI;
13 import java.util.HashMap;
14 import java.util.Map;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
20
21 public class QNameToStatementDefinitionMap implements QNameToStatementDefinition {
22     private final Map<QName, StatementSupport<?, ?, ?>> noRevQNameToSupport;
23     private final Map<QName, StatementSupport<?, ?, ?>> qnameToSupport;
24
25     public QNameToStatementDefinitionMap() {
26         noRevQNameToSupport = new HashMap<>();
27         qnameToSupport = new HashMap<>();
28     }
29
30     public QNameToStatementDefinitionMap(final int initialCapacity) {
31         noRevQNameToSupport = new HashMap<>(initialCapacity);
32         qnameToSupport = new HashMap<>(initialCapacity);
33     }
34
35     public void put(final QName qname, final StatementSupport<?, ?, ?> stDef) {
36         // HashMap does not guard against nulls
37         qnameToSupport.put(requireNonNull(qname), requireNonNull(stDef));
38         putNoRev(qname, stDef);
39     }
40
41     public void putAll(final Map<QName, StatementSupport<?, ?, ?>> qnameToStmt) {
42         qnameToSupport.putAll(qnameToStmt);
43         qnameToStmt.forEach(this::putNoRev);
44     }
45
46     public StatementSupport<?, ?, ?> putIfAbsent(final QName qname, final StatementSupport<?, ?, ?> support) {
47         final StatementSupport<?, ?, ?> existing = qnameToSupport.putIfAbsent(qname, support);
48         if (existing != null) {
49             return existing;
50         }
51
52         // XXX: we can (in theory) conflict here if we ever find ourselves needing to have multiple revisions of
53         //      statements. These should be equivalent, so no harm done (?)
54         //      Anyway, this is how it worked before last refactor.
55         putNoRev(qname, support);
56         return null;
57     }
58
59     private void putNoRev(final QName qname, final StatementSupport<?, ?, ?> support) {
60         final QName norev;
61         if (qname.getRevision() != null) {
62             norev = QName.create(qname.getNamespace(), null, qname.getLocalName()).intern();
63         } else {
64             norev = qname;
65         }
66         noRevQNameToSupport.put(norev, support);
67     }
68
69     @Nullable
70     @Override
71     public StatementSupport<?, ?, ?> get(@Nonnull final QName identifier) {
72         return qnameToSupport.get(identifier);
73     }
74
75     @Nullable
76     @Override
77     public StatementDefinition getByNamespaceAndLocalName(@Nonnull final URI namespace,
78             @Nonnull final String localName) {
79         return noRevQNameToSupport.get(QName.create(namespace, null, localName));
80     }
81
82 }