fa57186f351765440be836cb8a6978f51caf0438
[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  * combining {@link AugmentationHolder} and providing {@link Augmentable#augmentation(Class)} implementation on top of
20  * held augmentations.
21  *
22  * @param <T> Augmentable type
23  */
24 @Beta
25 public abstract class AbstractAugmentable<T extends Augmentable<T>> implements Augmentable<T>, AugmentationHolder<T> {
26     private final @NonNull ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations;
27
28     protected AbstractAugmentable() {
29         this.augmentations = ImmutableMap.of();
30     }
31
32     protected AbstractAugmentable(final Map<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
33         this.augmentations = ImmutableMap.copyOf(augmentations);
34     }
35
36     protected AbstractAugmentable(
37             final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
38         this.augmentations = requireNonNull(augmentations);
39     }
40
41     protected AbstractAugmentable(final AbstractAugmentable<T> other) {
42         this(other.augmentations);
43     }
44
45     @SuppressWarnings({ "unchecked", "checkstyle:methodTypeParameterName"})
46     @Override
47     public final <E$$ extends Augmentation<T>> E$$ augmentation(final Class<E$$> augmentationType) {
48         return (E$$) augmentations.get(CodeHelpers.nonNullValue(augmentationType, "augmentationType"));
49     }
50
51     @Override
52     public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
53         return augmentations;
54     }
55 }