Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / NamespaceBehaviour.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
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;
15
16 /**
17  * Definition / implementation of specific Identifier Namespace behaviour.
18  *
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}.
21  *
22  * For common behaviour models please use static factories {@link #global(Class)},
23  * {@link #sourceLocal(Class)} and {@link #treeScoped(Class)}.
24  *
25  * @param <K> Key type
26  * @param <V> Value type
27  * @param <N> Namespace Type
28  */
29 public abstract class NamespaceBehaviour<K,V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>>{
30
31     public enum StorageNodeType {
32         GLOBAL,
33         SOURCE_LOCAL_SPECIAL,
34         STATEMENT_LOCAL,
35     }
36
37     public interface Registry {
38         <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
39     }
40
41     public interface NamespaceStorageNode {
42
43         StorageNodeType getStorageNodeType();
44
45         @Nullable NamespaceStorageNode getParentNamespaceStorage();
46
47         @Nullable  <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
48
49         @Nullable  <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
50
51     }
52
53     private final Class<N> identifier;
54
55     protected NamespaceBehaviour(Class<N> identifier) {
56         this.identifier = Preconditions.checkNotNull(identifier);
57     }
58
59     /**
60      *
61      * Creates global namespace behaviour for supplied namespace type.
62      *
63      * Global behaviour stores and loads all values from root {@link NamespaceStorageNode}
64      * with type of {@link StorageNodeType#GLOBAL}.
65      *
66      * @param identifier Namespace identifier.
67      * @return global namespace behaviour for supplied namespace type.
68      */
69     public static @Nonnull <K,V, N extends IdentifierNamespace<K, V>>  NamespaceBehaviour<K,V,N> global(Class<N> identifier) {
70         return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
71     }
72
73     /**
74      *
75      * Creates source-local namespace behaviour for supplied namespace type.
76      *
77      * Source-local namespace behaviour stores and loads all values from closest
78      * {@link NamespaceStorageNode} ancestor with type of
79      * {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
80      *
81      * @param identifier Namespace identifier.
82      * @return source-local namespace behaviour for supplied namespace type.
83      */
84     public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> sourceLocal(Class<N> identifier) {
85         return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
86     }
87
88     /**
89     *
90     * Creates tree-scoped namespace behaviour for supplied namespace type.
91     *
92     * Tree-scoped namespace behaviour search for value in all storage nodes
93     * up to the root and stores values in supplied node.
94     *
95     * @param identifier Namespace identifier.
96     * @return tree-scoped namespace behaviour for supplied namespace type.
97     */
98     public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> treeScoped(Class<N> identifier) {
99         return new TreeScoped<>(identifier);
100     }
101
102     public abstract V getFrom(NamespaceStorageNode storage, K key);
103
104     public abstract void addTo(NamespaceStorageNode storage,K key,V value);
105
106     @Override
107     public Class<N> getIdentifier() {
108         return identifier;
109     }
110
111     protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
112         return storage.getFromLocalStorage(getIdentifier(), key);
113     }
114
115     protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
116         storage.addToLocalStorage(getIdentifier(),key,value);
117     }
118
119     static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
120
121         StorageNodeType storageType;
122
123         public StorageSpecific(Class<N> identifier, StorageNodeType type) {
124             super(identifier);
125             storageType = Preconditions.checkNotNull(type);
126         }
127
128         @Override
129         public V getFrom(final NamespaceStorageNode storage, final K key) {
130             NamespaceStorageNode current = storage;
131             while(current.getStorageNodeType() != storageType) {
132                 current = current.getParentNamespaceStorage();
133             }
134             return getFromLocalStorage(current,key);
135         }
136
137         @Override
138         public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
139             NamespaceStorageNode current = storage;
140             while(current.getStorageNodeType() != storageType) {
141                 current = current.getParentNamespaceStorage();
142             }
143             addToStorage(current, key, value);
144         }
145
146     }
147
148     static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
149
150         public TreeScoped(Class<N> identifier) {
151             super(identifier);
152         }
153
154         @Override
155         public V getFrom(final NamespaceStorageNode storage, final K key) {
156             NamespaceStorageNode current = storage;
157             while(current != null) {
158                 final V val = getFromLocalStorage(current, key);
159                 if(val != null) {
160                     return val;
161                 }
162                 current = current.getParentNamespaceStorage();
163             }
164             return null;
165         }
166
167 //        @Override
168 //        public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
169 //            NamespaceStorageNode current = storage;
170 //            while(current != null) {
171 //                final Map<K, V> val = getAllFromLocalStorage(current);
172 //                if(val != null) {
173 //                    return val;
174 //                }
175 //                current = current.getParentNamespaceStorage();
176 //            }
177 //            return null;
178 //        }
179
180         @Override
181         public void addTo(NamespaceStorageNode storage,K key, V value) {
182             addToStorage(storage, key, value);
183         }
184
185     }
186 }