2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
13 import java.util.Map.Entry;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
28 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
31 public abstract NamespaceStorageNode getParentNamespaceStorage();
34 * Return the registry of a source context.
36 * @return registry of source context
38 public abstract Registry getBehaviourRegistry();
40 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
45 * Occurs when an item is added to model namespace.
47 * @throws SourceException instance of SourceException
49 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
55 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
56 final KT key) throws NamespaceNotAvailableException {
57 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
60 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
61 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
64 @SuppressWarnings("unchecked")
65 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
66 final Class<N> type) {
67 return (Map<K, V>) namespaces.get(type);
70 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type,
71 final KT key, final VT value) throws NamespaceNotAvailableException {
72 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
75 @SuppressWarnings({ "unchecked", "rawtypes" })
76 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
77 final StmtContext<?, ?, ?> value) throws NamespaceNotAvailableException {
78 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
81 @SuppressWarnings("unchecked")
83 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
84 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
87 if (localNamespace != null) {
88 potential = localNamespace.get(key);
91 if (potential == null && isModuleIdentifierWithoutSpecifiedRevision(key)) {
92 potential = getRegardlessOfRevision((ModuleIdentifier)key, (Map<ModuleIdentifier,V>)localNamespace);
98 private static boolean isModuleIdentifierWithoutSpecifiedRevision(final Object obj) {
99 return obj instanceof ModuleIdentifier
100 && ((ModuleIdentifier) obj).getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP;
103 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
104 final Map<ModuleIdentifier, V> localNamespace) {
106 if (localNamespace == null) {
110 final Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
111 for (final Entry<ModuleIdentifier, V> entry : entrySet) {
112 final ModuleIdentifier moduleIdentifierInMap = entry.getKey();
113 if (moduleIdentifierInMap.getName().equals(key.getName())) {
114 return entry.getValue();
122 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
123 @SuppressWarnings("unchecked")
124 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
125 return localNamespace;
128 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
129 @SuppressWarnings("unchecked")
130 Map<K, V> ret = (Map<K,V>) namespaces.get(type);
132 checkLocalNamespaceAllowed(type);
133 ret = new HashMap<>(1);
135 if (namespaces.isEmpty()) {
136 namespaces = new HashMap<>(1);
138 namespaces.put(type, ret);
145 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
147 final V ret = ensureLocalNamespace(type).put(key, value);
148 onNamespaceElementAdded(type, key, value);
153 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
155 final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
157 onNamespaceElementAdded(type, key, value);