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.Date;
12 import java.util.HashMap;
14 import java.util.Map.Entry;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
18 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
28 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
31 public abstract NamespaceStorageNode getParentNamespaceStorage();
34 * @return registry of source context
36 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
38 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
42 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
48 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
49 final KT key) throws NamespaceNotAvailableException {
50 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
53 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
54 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
57 @SuppressWarnings("unchecked")
58 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
59 final Class<N> type) {
60 return (Map<K, V>) namespaces.get(type);
63 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type,
64 final KT key, final VT value) throws NamespaceNotAvailableException {
65 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
68 @SuppressWarnings({ "unchecked", "rawtypes" })
69 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
70 final StmtContext<?, ?, ?> value) throws NamespaceNotAvailableException {
71 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
74 @SuppressWarnings("unchecked")
76 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
77 final Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
80 if (localNamespace != null) {
81 potential = localNamespace.get(key);
84 if (potential == null && isModuleIdentifierWithoutSpecifiedRevision(key)) {
85 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
91 private static boolean isModuleIdentifierWithoutSpecifiedRevision(final Object obj) {
92 if (!(obj instanceof ModuleIdentifier)) {
96 final Date rev = ((ModuleIdentifier) obj).getRevision();
97 return rev == SimpleDateFormatUtil.DEFAULT_DATE_IMP || rev == SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE;
100 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
101 final Map<ModuleIdentifier, V> localNamespace) {
103 if (localNamespace == null) {
107 final Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
108 for (final Entry<ModuleIdentifier, V> entry : entrySet) {
109 final ModuleIdentifier moduleIdentifierInMap = entry.getKey();
110 if (moduleIdentifierInMap.getName().equals(key.getName())) {
111 return entry.getValue();
119 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
120 @SuppressWarnings("unchecked")
121 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
122 return localNamespace;
126 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
128 @SuppressWarnings("unchecked")
129 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
130 if (localNamespace == null) {
131 checkLocalNamespaceAllowed(type);
132 localNamespace = new HashMap<>(1);
134 if (namespaces.isEmpty()) {
135 namespaces = new HashMap<>(1);
137 namespaces.put(type, localNamespace);
139 localNamespace.put(key,value);
140 onNamespaceElementAdded(type,key,value);