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 ImmutableMap<QName, StatementSupport<?, ?, ?>> getDefinitions() {
38 public static Builder builder() {
39 return new Builder(EMPTY);
42 public static Builder derivedFrom(StatementSupportBundle parent) {
43 return new Builder(parent);
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()));
54 * Safe cast, previous checkState checks equivalence of key from
55 * which type argument are derived
57 return (NamespaceBehaviour<K, V, N>) potential;
60 return parent.getNamespaceBehaviour(namespace);
65 public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(Class<N> namespace) {
66 if (namespaceDefinitions.containsKey(namespace)) {
70 return parent.hasNamespaceBehaviour(namespace);
75 public StatementSupport<?, ?,?> getStatementDefinition(QName stmtName) {
76 StatementSupport<?,?, ?> potential = definitions.get(stmtName);
77 if (potential != null) {
81 return parent.getStatementDefinition(stmtName);
86 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
88 private final StatementSupportBundle parent;
89 private final Map<QName, StatementSupport<?,?,?>> statements = new HashMap<>();
90 private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
92 Builder(StatementSupportBundle parent) {
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);
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);
113 public StatementSupportBundle build() {
114 return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));