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 com.google.common.collect.ImmutableMap;
12 import java.util.HashMap;
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;
18 public final class StatementSupportBundle implements Immutable,NamespaceBehaviour.Registry {
20 private static final StatementSupportBundle EMPTY = new StatementSupportBundle(null, ImmutableMap.<QName, StatementSupport<?, ?, ?>>of(), ImmutableMap.<Class<?>, NamespaceBehaviour<?, ?, ?>>of());
22 private final StatementSupportBundle parent;
23 private final ImmutableMap<QName, StatementSupport<?,?,?>> definitions;
24 private final ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaceDefinitions;
26 private StatementSupportBundle(StatementSupportBundle parent,
27 ImmutableMap<QName, StatementSupport<?, ?, ?>> statements,
28 ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces) {
30 this.definitions = statements;
31 this.namespaceDefinitions = namespaces;
34 public static Builder builder() {
35 return new Builder(EMPTY);
38 public static Builder derivedFrom(StatementSupportBundle parent) {
39 return new Builder(parent);
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()));
50 * Safe cast, previous checkState checks equivalence of key from
51 * which type argument are derived
53 @SuppressWarnings("unchecked")
54 NamespaceBehaviour<K, V, N> casted = (NamespaceBehaviour<K, V, N>) potential;
58 return parent.getNamespaceBehaviour(namespace);
63 public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(Class<N> namespace) {
64 if (namespaceDefinitions.containsKey(namespace)) {
68 return parent.hasNamespaceBehaviour(namespace);
73 public StatementSupport<?, ?,?> getStatementDefinition(QName stmtName) {
74 StatementSupport<?,?, ?> potential = definitions.get(stmtName);
75 if (potential != null) {
79 return parent.getStatementDefinition(stmtName);
84 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
86 private final StatementSupportBundle parent;
87 private final Map<QName, StatementSupport<?,?,?>> statements = new HashMap<>();
88 private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
90 Builder(StatementSupportBundle parent) {
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);
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);
111 public StatementSupportBundle build() {
112 return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));