Migrate yang-maven-plugin to JUnit5
[yangtools.git] / data / rfc7952-data-util / src / main / java / org / opendaylight / yangtools / rfc7952 / data / util / ImmutableNormalizedMetadata.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.rfc7952.data.util;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
20 import org.opendaylight.yangtools.concepts.Mutable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedMetadata;
24
25 /**
26  * Immutable implementation of {@link NormalizedMetadata}.
27  */
28 @Beta
29 public class ImmutableNormalizedMetadata extends AbstractSimpleIdentifiable<PathArgument>
30         implements NormalizedMetadata {
31     private static final class Container extends ImmutableNormalizedMetadata {
32         private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
33
34         Container(final PathArgument identifier, final Map<QName, Object> annotations,
35                 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
36             super(identifier, annotations);
37             this.children = ImmutableMap.copyOf(children);
38         }
39
40         @Override
41         public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
42             return children;
43         }
44     }
45
46     private final @NonNull ImmutableMap<QName, Object> annotations;
47
48     ImmutableNormalizedMetadata(final @NonNull PathArgument identifier, final Map<QName, Object> annotations) {
49         super(identifier);
50         this.annotations = ImmutableMap.copyOf(annotations);
51     }
52
53     /**
54      * Return a new {@link Builder}.
55      *
56      * @return A new Builder.
57      */
58     public static final @NonNull Builder builder() {
59         return new Builder();
60     }
61
62     @Override
63     public final ImmutableMap<QName, Object> getAnnotations() {
64         return annotations;
65     }
66
67     /**
68      * A Builder of {@link ImmutableNormalizedMetadata} instances.
69      */
70     public static final class Builder implements Mutable {
71         private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
72         private final Map<QName, Object> annotations = new HashMap<>();
73         private PathArgument identifier;
74
75         Builder() {
76             // Hidden to prevent instantiation
77         }
78
79         @SuppressWarnings("checkstyle:hiddenField")
80         public @NonNull Builder withIdentifier(final PathArgument identifier) {
81             this.identifier = requireNonNull(identifier);
82             return this;
83         }
84
85         public @NonNull Builder withAnnotation(final QName type, final Object value) {
86             annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
87             return this;
88         }
89
90         @SuppressWarnings("checkstyle:hiddenField")
91         public @NonNull Builder withAnnotations(final Map<QName, Object> annotations) {
92             annotations.forEach(this::withAnnotation);
93             return this;
94         }
95
96         public @NonNull Builder withChild(final ImmutableNormalizedMetadata child) {
97             children.put(child.getIdentifier(), child);
98             return this;
99         }
100
101         @SuppressWarnings("checkstyle:hiddenField")
102         public @NonNull Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
103             children.forEach(this::withChild);
104             return this;
105         }
106
107         /**
108          * Return an {@link ImmutableNormalizedMetadata} view of this builder's state.
109          *
110          * @return An ImmutableNormalizedMetadata instace
111          * @throws IllegalStateException if this builder does not have enough state
112          */
113         public @NonNull ImmutableNormalizedMetadata build() {
114             final PathArgument id = identifier;
115             checkState(id != null, "Identifier has not been set");
116             return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
117                     : new Container(id, annotations, children);
118         }
119     }
120 }