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;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
27 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
30 public abstract NamespaceStorageNode getParentNamespaceStorage();
33 * Return the registry of a source context.
35 * @return registry of source context
37 public abstract Registry getBehaviourRegistry();
39 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
44 * Occurs when an item is added to model namespace.
46 * @throws SourceException instance of SourceException
48 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
53 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<Entry<K, V>> getFromNamespace(
54 final Class<N> type, final NamespaceKeyCriterion<K> criterion) {
55 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, criterion);
58 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getNamespace(final Class<N> type) {
59 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
62 @SuppressWarnings("unchecked")
63 final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getLocalNamespace(final Class<N> type) {
64 return (Map<K, V>) namespaces.get(type);
67 final <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNamespace(
68 final Class<N> type, final T key, final U value) {
69 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this, key, value);
72 final <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNamespace(
73 final Class<N> type, final Map<T, U> map) {
74 final NamespaceBehaviour<K, V, N> behavior = getBehaviourRegistry().getNamespaceBehaviour(type);
75 for (final Entry<T, U> validationBundle : map.entrySet()) {
76 behavior.addTo(this, validationBundle.getKey(), validationBundle.getValue());
81 * Associate a context with a key within a namespace.
83 * @param type Namespace type
85 * @param value Context value
86 * @param <K> namespace key type
87 * @param <N> namespace type
88 * @throws NamespaceNotAvailableException when the namespace is not available.
90 @SuppressWarnings({ "unchecked", "rawtypes" })
91 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
92 final StmtContext<?, ?, ?> value) {
93 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
96 @SuppressWarnings("unchecked")
98 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
99 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
100 return localNamespace == null ? null : localNamespace.get(key);
104 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
105 @SuppressWarnings("unchecked")
106 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
107 return localNamespace;
110 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
111 @SuppressWarnings("unchecked")
112 Map<K, V> ret = (Map<K,V>) namespaces.get(type);
114 checkLocalNamespaceAllowed(type);
115 ret = new HashMap<>(1);
117 switch (namespaces.size()) {
119 // We typically have small population of namespaces, use a singleton map
120 namespaces = ImmutableMap.of(type, ret);
123 // Alright, time to grow to a full HashMap
124 final Map<Class<?>, Map<?,?>> newNamespaces = new HashMap<>(4);
125 final Entry<Class<?>, Map<?, ?>> entry = namespaces.entrySet().iterator().next();
126 newNamespaces.put(entry.getKey(), entry.getValue());
127 namespaces = newNamespaces;
130 // Already expanded, just put the new namespace
131 namespaces.put(type, ret);
139 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
141 final V ret = ensureLocalNamespace(type).put(key, value);
142 onNamespaceElementAdded(type, key, value);
147 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
149 final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
151 onNamespaceElementAdded(type, key, value);