Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / QNameCacheNamespace.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.meta;
9
10 import java.util.Map;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
14
15 /**
16  * An {@link IdentifierNamespace} implementing a {@link QName} internment interface. Lookups in this namespace always
17  * return a non-null object. They capture the first object instance and return that on subsequent lookups.
18  */
19 public final class QNameCacheNamespace extends NamespaceBehaviour<QName, QName, QNameCacheNamespace>
20     implements IdentifierNamespace<QName, QName> {
21
22     private static final QNameCacheNamespace INSTANCE = new QNameCacheNamespace();
23
24     private QNameCacheNamespace() {
25         super(QNameCacheNamespace.class);
26     }
27
28     public static QNameCacheNamespace getInstance() {
29         return INSTANCE;
30     }
31
32     @Override
33     public QName get(@Nonnull final QName identifier) {
34         throw new UnsupportedOperationException("Identifier/implementation API borkage");
35     }
36
37     private static NamespaceStorageNode getRoot(final NamespaceStorageNode storage) {
38         NamespaceStorageNode wlk = storage;
39
40         while (wlk.getParentNamespaceStorage() != null) {
41             wlk = wlk.getParentNamespaceStorage();
42         }
43
44         return wlk;
45     }
46
47     @Override
48     public QName getFrom(final NamespaceStorageNode storage, final QName key) {
49         final NamespaceStorageNode root = getRoot(storage);
50         final QName stored = root.getFromLocalStorage(QNameCacheNamespace.class, key);
51         if (stored != null) {
52             return stored;
53         }
54
55         root.addToLocalStorage(QNameCacheNamespace.class, key, key);
56         return key;
57     }
58
59     @Override
60     public Map<QName, QName> getAllFrom(final NamespaceStorageNode storage) {
61         return getRoot(storage).getAllFromLocalStorage(QNameCacheNamespace.class);
62     }
63
64     @Override
65     public void addTo(final NamespaceStorageNode storage, final QName key, final QName value) {
66         throw new UnsupportedOperationException("Automagically populated");
67     }
68 }