Add NamespaceBehaviour.toString()
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / DerivedNamespaceBehaviour.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
15
16 /**
17  * An {@link NamespaceBehaviour} which derives keys from a different namespace.
18  *
19  * @param <K> Key type
20  * @param <V> Value type
21  * @param <N> Namespace type
22  * @param <L> Original key type
23  * @param <O> Original namespace type
24  */
25 public abstract class DerivedNamespaceBehaviour<K, V, L, N extends IdentifierNamespace<K, V>,
26        O extends IdentifierNamespace<L, ?>> extends NamespaceBehaviour<K, V, N> {
27
28     private final Class<O> derivedFrom;
29
30     protected DerivedNamespaceBehaviour(final Class<N> identifier, final Class<O> derivedFrom) {
31         super(identifier);
32         this.derivedFrom = requireNonNull(derivedFrom);
33     }
34
35     public Class<O> getDerivedFrom() {
36         return derivedFrom;
37     }
38
39     @Override
40     public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
41         throw new UnsupportedOperationException("Virtual namespaces does not support provision of all items.");
42     }
43
44     @Override
45     public abstract V getFrom(NamespaceBehaviour.NamespaceStorageNode storage, K key);
46
47     @Override
48     public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
49         // Intentional noop
50     }
51
52     public abstract L getSignificantKey(K key);
53
54     @Override
55     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
56         return helper.add("derivedFrom", derivedFrom.getName());
57     }
58 }