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.spi.meta;
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17 * Definition / implementation of specific Identifier Namespace behaviour.
19 * Namespace behaviour is build on top of tree of {@link NamespaceStorageNode}
20 * which represents local context of one of types defined in {@link StorageNodeType}.
22 * For common behaviour models please use static factories {@link #global(Class)},
23 * {@link #sourceLocal(Class)} and {@link #treeScoped(Class)}.
26 * @param <V> Value type
27 * @param <N> Namespace Type
29 public abstract class NamespaceBehaviour<K,V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>>{
31 public enum StorageNodeType {
37 public interface Registry {
38 <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
41 public interface NamespaceStorageNode {
43 StorageNodeType getStorageNodeType();
45 @Nullable NamespaceStorageNode getParentNamespaceStorage();
47 @Nullable <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
49 //<K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(Class<N> type);
51 @Nullable <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
55 private final Class<N> identifier;
57 protected NamespaceBehaviour(Class<N> identifier) {
58 this.identifier = Preconditions.checkNotNull(identifier);
63 * Creates global namespace behaviour for supplied namespace type.
65 * Global behaviour stores and loads all values from root {@link NamespaceStorageNode}
66 * with type of {@link StorageNodeType#GLOBAL}.
68 * @param identifier Namespace identifier.
69 * @return global namespace behaviour for supplied namespace type.
71 public static @Nonnull <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> global(Class<N> identifier) {
72 return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
77 * Creates source-local namespace behaviour for supplied namespace type.
79 * Source-local namespace behaviour stores and loads all values from closest
80 * {@link NamespaceStorageNode} ancestor with type of
81 * {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
83 * @param identifier Namespace identifier.
84 * @return source-local namespace behaviour for supplied namespace type.
86 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> sourceLocal(Class<N> identifier) {
87 return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
92 * Creates tree-scoped namespace behaviour for supplied namespace type.
94 * Tree-scoped namespace behaviour search for value in all storage nodes
95 * up to the root and stores values in supplied node.
97 * @param identifier Namespace identifier.
98 * @return tree-scoped namespace behaviour for supplied namespace type.
100 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> treeScoped(Class<N> identifier) {
101 return new TreeScoped<>(identifier);
104 public abstract V getFrom(NamespaceStorageNode storage, K key);
106 //public abstract Map<K, V> getAllFrom(NamespaceStorageNode storage);
108 public abstract void addTo(NamespaceStorageNode storage,K key,V value);
111 public Class<N> getIdentifier() {
115 protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
116 return storage.getFromLocalStorage(getIdentifier(), key);
119 // protected final Map<K, V> getAllFromLocalStorage(NamespaceStorageNode storage) {
120 // return storage.getAllFromLocalStorage(getIdentifier());
123 protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
124 storage.addToLocalStorage(getIdentifier(),key,value);
127 static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
129 StorageNodeType storageType;
131 public StorageSpecific(Class<N> identifier, StorageNodeType type) {
133 storageType = Preconditions.checkNotNull(type);
137 public V getFrom(final NamespaceStorageNode storage, final K key) {
138 NamespaceStorageNode current = storage;
139 while(current.getStorageNodeType() != storageType) {
140 current = current.getParentNamespaceStorage();
142 return getFromLocalStorage(current,key);
146 // public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
147 // NamespaceStorageNode current = storage;
148 // while(current.getStorageNodeType() != storageType) {
149 // current = current.getParentNamespaceStorage();
151 // return getAllFromLocalStorage(current);
155 public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
156 NamespaceStorageNode current = storage;
157 while(current.getStorageNodeType() != storageType) {
158 current = current.getParentNamespaceStorage();
160 addToStorage(current, key, value);
165 static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
167 public TreeScoped(Class<N> identifier) {
172 public V getFrom(final NamespaceStorageNode storage, final K key) {
173 NamespaceStorageNode current = storage;
174 while(current != null) {
175 final V val = getFromLocalStorage(current, key);
179 current = current.getParentNamespaceStorage();
185 // public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
186 // NamespaceStorageNode current = storage;
187 // while(current != null) {
188 // final Map<K, V> val = getAllFromLocalStorage(current);
192 // current = current.getParentNamespaceStorage();
198 public void addTo(NamespaceStorageNode storage,K key, V value) {
199 addToStorage(storage, key, value);