2002cf513527a53ecd52c97c8c5439b4f1a68c9e
[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 static Builder builder() {
35         return new Builder(EMPTY);
36     }
37
38     public static Builder derivedFrom(StatementSupportBundle parent) {
39         return new Builder(parent);
40     }
41
42     @Override
43     public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> namespace)
44             throws NamespaceNotAvailableException {
45         NamespaceBehaviour<?, ?, ?> potential = namespaceDefinitions.get(namespace);
46         if (potential != null) {
47             Preconditions.checkState(namespace.equals(potential.getIdentifier()));
48
49             /*
50              * Safe cast, previous checkState checks equivalence of key from
51              * which type argument are derived
52              */
53             @SuppressWarnings("unchecked")
54             NamespaceBehaviour<K, V, N> casted = (NamespaceBehaviour<K, V, N>) potential;
55             return casted;
56         }
57         if (parent != null) {
58             return parent.getNamespaceBehaviour(namespace);
59         }
60         return null;
61     }
62
63     public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(Class<N> namespace) {
64         if (namespaceDefinitions.containsKey(namespace)) {
65             return true;
66         }
67         if (parent != null) {
68             return parent.hasNamespaceBehaviour(namespace);
69         }
70         return false;
71     }
72
73     public StatementSupport<?, ?,?> getStatementDefinition(QName stmtName) {
74         StatementSupport<?,?, ?> potential = definitions.get(stmtName);
75         if (potential != null) {
76             return potential;
77         }
78         if (parent != null) {
79             return parent.getStatementDefinition(stmtName);
80         }
81         return null;
82     }
83
84     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
85
86         private final StatementSupportBundle parent;
87         private final Map<QName, StatementSupport<?,?,?>> statements = new HashMap<>();
88         private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
89
90         Builder(StatementSupportBundle parent) {
91             this.parent = parent;
92         }
93
94         public Builder addSupport(StatementSupport<?, ?,?> definition) {
95             QName identifier = definition.getStatementName();
96             Preconditions.checkState(!statements.containsKey(identifier), "Statement %s already defined.",identifier);
97             Preconditions.checkState(parent.getStatementDefinition(identifier) == null, "Statement %s already defined.",identifier);
98             statements.put(identifier, definition);
99             return this;
100         }
101
102        public <K, V, N extends IdentifierNamespace<K, V>> Builder addSupport(NamespaceBehaviour<K, V, N> namespaceSupport) {
103             Class<N> identifier = namespaceSupport.getIdentifier();
104             Preconditions.checkState(!namespaces.containsKey(identifier));
105             Preconditions.checkState(!parent.hasNamespaceBehaviour(identifier));
106             namespaces.put(identifier, namespaceSupport);
107             return this;
108         }
109
110        @Override
111         public StatementSupportBundle build() {
112             return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));
113         }
114
115     }
116
117 }