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();
33 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
35 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
39 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
45 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
46 final KT key) throws NamespaceNotAvailableException {
47 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
50 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
51 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
54 @SuppressWarnings("unchecked")
55 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
56 final Class<N> type) {
57 return (Map<K, V>) namespaces.get(type);
60 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type,
61 final KT key, final VT value) throws NamespaceNotAvailableException {
62 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
65 @SuppressWarnings({ "unchecked", "rawtypes" })
66 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
67 final StmtContext<?, ?, ?> value) throws NamespaceNotAvailableException {
68 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
71 @SuppressWarnings("unchecked")
73 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
74 final Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
77 if (localNamespace != null) {
78 potential = localNamespace.get(key);
81 if (potential == null && isModuleIdentifierWithoutSpecifiedRevision(key)) {
82 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
88 private static boolean isModuleIdentifierWithoutSpecifiedRevision(final Object obj) {
89 if (!(obj instanceof ModuleIdentifier)) {
93 final Date rev = ((ModuleIdentifier) obj).getRevision();
94 return rev == SimpleDateFormatUtil.DEFAULT_DATE_IMP || rev == SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE;
97 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
98 final Map<ModuleIdentifier, V> localNamespace) {
100 if (localNamespace == null) {
104 final Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
105 for (final Entry<ModuleIdentifier, V> entry : entrySet) {
106 final ModuleIdentifier moduleIdentifierInMap = entry.getKey();
107 if (moduleIdentifierInMap.getName().equals(key.getName())) {
108 return entry.getValue();
116 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
117 @SuppressWarnings("unchecked")
118 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
119 return localNamespace;
123 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
125 @SuppressWarnings("unchecked")
126 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
127 if (localNamespace == null) {
128 checkLocalNamespaceAllowed(type);
129 localNamespace = new HashMap<>(1);
131 if (namespaces.isEmpty()) {
132 namespaces = new HashMap<>(1);
134 namespaces.put(type, localNamespace);
136 localNamespace.put(key,value);
137 onNamespaceElementAdded(type,key,value);