Seal BaseNotification
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / AbstractAugmentable.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.binding;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16
17 /**
18  * Abstract base class for implementing immutable {@link Augmentable} classes. This class is provided as a convenience.
19  *
20  * @param <T> Augmentable type
21  */
22 @Beta
23 public abstract class AbstractAugmentable<T extends Augmentable<T>> implements Augmentable<T> {
24     private final @NonNull ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations;
25
26     protected AbstractAugmentable() {
27         this.augmentations = ImmutableMap.of();
28     }
29
30     protected AbstractAugmentable(final Map<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
31         this.augmentations = ImmutableMap.copyOf(augmentations);
32     }
33
34     protected AbstractAugmentable(
35             final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
36         this.augmentations = requireNonNull(augmentations);
37     }
38
39     protected AbstractAugmentable(final AbstractAugmentable<T> other) {
40         this(other.augmentations);
41     }
42
43     @Override
44     @SuppressWarnings("unchecked")
45     public final <A extends Augmentation<T>> A augmentation(final Class<A> augmentationType) {
46         return (A) augmentations.get(requireNonNull(augmentationType));
47     }
48
49     @Override
50     public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
51         return augmentations;
52     }
53 }