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 public ImmutableMap<QName, StatementSupport<?, ?, ?>> getDefinitions() {
30 private StatementSupportBundle(StatementSupportBundle parent,
31 ImmutableMap<QName, StatementSupport<?, ?, ?>> statements,
32 ImmutableMap<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces) {
34 this.definitions = statements;
35 this.namespaceDefinitions = namespaces;
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 @SuppressWarnings("unchecked")
58 NamespaceBehaviour<K, V, N> casted = (NamespaceBehaviour<K, V, N>) potential;
62 return parent.getNamespaceBehaviour(namespace);
67 public <K, V, N extends IdentifierNamespace<K, V>> boolean hasNamespaceBehaviour(Class<N> namespace) {
68 if (namespaceDefinitions.containsKey(namespace)) {
72 return parent.hasNamespaceBehaviour(namespace);
77 public StatementSupport<?, ?,?> getStatementDefinition(QName stmtName) {
78 StatementSupport<?,?, ?> potential = definitions.get(stmtName);
79 if (potential != null) {
83 return parent.getStatementDefinition(stmtName);
88 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<StatementSupportBundle> {
90 private final StatementSupportBundle parent;
91 private final Map<QName, StatementSupport<?,?,?>> statements = new HashMap<>();
92 private final Map<Class<?>, NamespaceBehaviour<?, ?, ?>> namespaces = new HashMap<>();
94 Builder(StatementSupportBundle parent) {
98 public Builder addSupport(StatementSupport<?, ?,?> definition) {
99 QName identifier = definition.getStatementName();
100 Preconditions.checkState(!statements.containsKey(identifier), "Statement %s already defined.",identifier);
101 Preconditions.checkState(parent.getStatementDefinition(identifier) == null, "Statement %s already defined.",identifier);
102 statements.put(identifier, definition);
106 public <K, V, N extends IdentifierNamespace<K, V>> Builder addSupport(NamespaceBehaviour<K, V, N> namespaceSupport) {
107 Class<N> identifier = namespaceSupport.getIdentifier();
108 Preconditions.checkState(!namespaces.containsKey(identifier));
109 Preconditions.checkState(!parent.hasNamespaceBehaviour(identifier));
110 namespaces.put(identifier, namespaceSupport);
115 public StatementSupportBundle build() {
116 return new StatementSupportBundle(parent, ImmutableMap.copyOf(statements), ImmutableMap.copyOf(namespaces));