Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StatementSupportBundle.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 com.google.common.collect.ImmutableMap;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17
18 public final class StatementSupportBundle implements Immutable,NamespaceBehaviour.Registry {
19
20     private static final StatementSupportBundle EMPTY = new StatementSupportBundle(null, ImmutableMap.<QName, StatementSupport<?, ?, ?>>of(), ImmutableMap.<Class<?>, NamespaceBehaviour<?, ?, ?>>of());
21
22     private final StatementSupportBundle parent;
23     private final ImmutableMap<QName, StatementSupport<?,?,?>> definitions;
24     private final ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaceDefinitions;
25
26     private StatementSupportBundle(StatementSupportBundle parent,
27                                    ImmutableMap<QName, StatementSupport<?, ?, ?>> statements,
28                                    ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces) {
29         this.parent = parent;
30         this.definitions = statements;
31         this.namespaceDefinitions = namespaces;
32     }
33
34     public ImmutableMap<QName, StatementSupport<?, ?, ?>> getDefinitions() {
35         return definitions;
36     }
37
38     public static Builder builder() {
39         return new Builder(EMPTY);
40     }
41
42     public static Builder derivedFrom(StatementSupportBundle parent) {
43         return new Builder(parent);
44     }
45
46     @Override
47     public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> namespace)
48             throws NamespaceNotAvailableException {
49         NamespaceBehaviour<?, ?, ?> potential = namespaceDefinitions.get(namespace);
50         if (potential != null) {
51             Preconditions.checkState(namespace.equals(potential.getIdentifier()));
52
53             /*
54              * Safe cast, previous checkState checks equivalence of key from
55              * which type argument are derived
56              */
57             return (NamespaceBehaviour<K, V, N>) potential;
58         }
59         if (parent != null) {
60             return parent.getNamespaceBehaviour(namespace);
61         }
62         return null;
63     }
64
65     public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(Class<N> namespace) {
66         if (namespaceDefinitions.containsKey(namespace)) {
67             return true;
68         }
69         if (parent != null) {
70             return parent.hasNamespaceBehaviour(namespace);
71         }
72         return false;
73     }
74
75     public StatementSupport<?, ?,?> getStatementDefinition(QName stmtName) {
76         StatementSupport<?,?, ?> potential = definitions.get(stmtName);
77         if (potential != null) {
78             return potential;
79         }
80         if (parent != null) {
81             return parent.getStatementDefinition(stmtName);
82         }
83         return null;
84     }
85
86     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
87
88         private final StatementSupportBundle parent;
89         private final Map<QName, StatementSupport<?,?,?>> statements = new HashMap<>();
90         private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
91
92         Builder(StatementSupportBundle parent) {
93             this.parent = parent;
94         }
95
96         public Builder addSupport(StatementSupport<?, ?,?> definition) {
97             QName identifier = definition.getStatementName();
98             Preconditions.checkState(!statements.containsKey(identifier), "Statement %s already defined.",identifier);
99             Preconditions.checkState(parent.getStatementDefinition(identifier) == null, "Statement %s already defined.",identifier);
100             statements.put(identifier, definition);
101             return this;
102         }
103
104        public <K, V, N extends IdentifierNamespace<K, V>> Builder addSupport(NamespaceBehaviour<K, V, N> namespaceSupport) {
105             Class<N> identifier = namespaceSupport.getIdentifier();
106             Preconditions.checkState(!namespaces.containsKey(identifier));
107             Preconditions.checkState(!parent.hasNamespaceBehaviour(identifier));
108             namespaces.put(identifier, namespaceSupport);
109             return this;
110         }
111
112        @Override
113         public StatementSupportBundle build() {
114             return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));
115         }
116
117     }
118
119 }