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.of(), ImmutableMap.of());
22 private final StatementSupportBundle parent;
23 private final ImmutableMap<QName, StatementSupport<?,?,?>> definitions;
24 private final ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaceDefinitions;
26 private StatementSupportBundle(final StatementSupportBundle parent,
27 final ImmutableMap<QName, StatementSupport<?, ?, ?>> statements,
28 final ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces) {
30 this.definitions = statements;
31 this.namespaceDefinitions = namespaces;
34 public ImmutableMap<QName, StatementSupport<?, ?, ?>> getDefinitions() {
38 public ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> getNamespaceDefinitions() {
39 return namespaceDefinitions;
42 public static Builder builder() {
43 return new Builder(EMPTY);
46 public static Builder derivedFrom(final StatementSupportBundle parent) {
47 return new Builder(parent);
51 public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(final Class<N> namespace)
52 throws NamespaceNotAvailableException {
53 final NamespaceBehaviour<?, ?, ?> potential = namespaceDefinitions.get(namespace);
54 if (potential != null) {
55 Preconditions.checkState(namespace.equals(potential.getIdentifier()));
58 * Safe cast, previous checkState checks equivalence of key from
59 * which type argument are derived
61 return (NamespaceBehaviour<K, V, N>) potential;
64 return parent.getNamespaceBehaviour(namespace);
69 public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(final Class<N> namespace) {
70 if (namespaceDefinitions.containsKey(namespace)) {
74 return parent.hasNamespaceBehaviour(namespace);
79 public StatementSupport<?, ?,?> getStatementDefinition(final QName stmtName) {
80 final StatementSupport<?,?, ?> potential = definitions.get(stmtName);
81 if (potential != null) {
85 return parent.getStatementDefinition(stmtName);
90 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
92 private StatementSupportBundle parent;
93 private final Map<QName, StatementSupport<?, ?, ?>> statements = new HashMap<>();
94 private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
96 Builder(final StatementSupportBundle parent) {
100 public Builder addSupport(final StatementSupport<?, ?, ?> definition) {
101 final QName identifier = definition.getStatementName();
102 Preconditions.checkState(!statements.containsKey(identifier), "Statement %s already defined.", identifier);
103 Preconditions.checkState(parent.getStatementDefinition(identifier) == null,
104 "Statement %s already defined.", identifier);
105 statements.put(identifier, definition);
109 public <K, V, N extends IdentifierNamespace<K, V>> Builder addSupport(
110 final NamespaceBehaviour<K, V, N> namespaceSupport) {
111 final Class<N> identifier = namespaceSupport.getIdentifier();
112 Preconditions.checkState(!namespaces.containsKey(identifier));
113 Preconditions.checkState(!parent.hasNamespaceBehaviour(identifier));
114 namespaces.put(identifier, namespaceSupport);
118 public Builder setParent(final StatementSupportBundle parent) {
119 this.parent = parent;
124 public StatementSupportBundle build() {
125 return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));