/* * 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.Map.Entry; import java.util.Set; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import java.util.HashMap; import java.util.Map; 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; 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(Class> type) { // NOOP } protected > void onNamespaceElementAdded(Class type, K key, V value) { // NOOP } //> V //public final > VT getFromNamespace(Class type, K key) public final > V getFromNamespace(Class type, KT key) throws NamespaceNotAvailableException { return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key); } public final > Map getAllFromNamespace(Class type){ return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this); } public final > Map getAllFromCurrentStmtCtxNamespace(Class type){ return (Map) namespaces.get(type); } public final > void addToNs(Class type, KT key, VT value) throws NamespaceNotAvailableException { getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value); } @SuppressWarnings({ "unchecked", "rawtypes" }) public final > void addContextToNamespace(Class type, K key, StmtContext value) throws NamespaceNotAvailableException { getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value); } @SuppressWarnings("unchecked") @Override public > V getFromLocalStorage(Class type, 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 > V getRegardlessOfRevision(ModuleIdentifier key, 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(Class type) { @SuppressWarnings("unchecked") Map localNamespace = (Map) namespaces.get(type); return localNamespace; } @Override public > void addToLocalStorage(Class type, K key, 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); } }