/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.stmt.reactor; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; abstract class NamespaceStorageSupport implements NamespaceStorageNode { private final Map,Map> namespaces = new HashMap<>(); @Override public abstract NamespaceStorageNode getParentNamespaceStorage(); public abstract NamespaceBehaviour.Registry getBehaviourRegistry(); protected void checkLocalNamespaceAllowed(final Class> type) { // NOOP } protected > void onNamespaceElementAdded(final Class type, final K key, final V value) { // NOOP } //> V //public final > VT getFromNamespace(Class type, K key) public final > V getFromNamespace(final Class type, final KT key) throws NamespaceNotAvailableException { return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key); } public final > Map getAllFromNamespace(final Class type){ return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this); } public final > Map getAllFromCurrentStmtCtxNamespace(final Class type){ return (Map) namespaces.get(type); } public final > void addToNs(final Class type, final KT key, final VT value) throws NamespaceNotAvailableException { getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value); } @SuppressWarnings({ "unchecked", "rawtypes" }) public final > void addContextToNamespace(final Class type, final K key, final StmtContext value) throws NamespaceNotAvailableException { getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value); } @SuppressWarnings("unchecked") @Override public > V getFromLocalStorage(final Class type, final K key) { Map localNamespace = (Map) namespaces.get(type); V potential = null; if(localNamespace != null) { potential = localNamespace.get(key); } if(potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) { potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map)localNamespace); } return potential; } private static > V getRegardlessOfRevision(final ModuleIdentifier key, final Map localNamespace) { if (localNamespace == null) { return null; } Set> entrySet = localNamespace.entrySet(); for (Entry entry : entrySet) { ModuleIdentifier moduleIdentifierInMap = entry.getKey(); if (moduleIdentifierInMap.getName().equals(key.getName())) { return entry.getValue(); } } return null; } @Override public > Map getAllFromLocalStorage(final Class type) { @SuppressWarnings("unchecked") Map localNamespace = (Map) namespaces.get(type); return localNamespace; } @Override public > void addToLocalStorage(final Class type, final K key, final V value) { @SuppressWarnings("unchecked") Map localNamespace = (Map) namespaces.get(type); if(localNamespace == null) { checkLocalNamespaceAllowed(type); localNamespace = new HashMap<>(); namespaces.put(type, localNamespace); } localNamespace.put(key,value); onNamespaceElementAdded(type,key,value); } }