/* * 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 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 } public final > VT getFromNamespace(Class type, K key) throws NamespaceNotAvailableException { return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key); } public final > void addToNs(Class type, K 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); } @Override public > V getFromLocalStorage(Class type, K key) { @SuppressWarnings("unchecked") Map localNamespace = (Map) namespaces.get(type); if(localNamespace != null) { return localNamespace.get(key); } return null; } @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); } }