4d5b0491adc29f0a1a41a964fd9f917530948890
[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.checkArgument;
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.rfc7952.data.api.NormalizedMetadata;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23
24 /**
25  * Immutable implementation of {@link NormalizedMetadata}.
26  */
27 @Beta
28 public class ImmutableNormalizedMetadata extends AbstractSimpleIdentifiable<PathArgument>
29         implements NormalizedMetadata {
30     private static final class Container extends ImmutableNormalizedMetadata {
31         private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
32
33         Container(final PathArgument identifier, final Map<QName, Object> annotations,
34                 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
35             super(identifier, annotations);
36             this.children = ImmutableMap.copyOf(children);
37         }
38
39         @Override
40         public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
41             return children;
42         }
43     }
44
45     private final @NonNull ImmutableMap<QName, Object> annotations;
46
47     ImmutableNormalizedMetadata(final @NonNull PathArgument identifier, final Map<QName, Object> annotations) {
48         super(identifier);
49         this.annotations = ImmutableMap.copyOf(annotations);
50     }
51
52     /**
53      * Return a new {@link Builder}.
54      *
55      * @return A new Builder.
56      */
57     public static final @NonNull Builder builder() {
58         return new Builder();
59     }
60
61     @Override
62     public final ImmutableMap<QName, Object> getAnnotations() {
63         return annotations;
64     }
65
66     /**
67      * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
68      */
69     public static final class Builder
70             implements org.opendaylight.yangtools.concepts.Builder<ImmutableNormalizedMetadata> {
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 Builder withIdentifier(final PathArgument identifier) {
81             this.identifier = requireNonNull(identifier);
82             return this;
83         }
84
85         public 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 Builder withAnnotations(final Map<QName, Object> annotations) {
92             annotations.forEach(this::withAnnotation);
93             return this;
94         }
95
96         public Builder withChild(final ImmutableNormalizedMetadata child) {
97             children.put(child.getIdentifier(), child);
98             return this;
99         }
100
101         @SuppressWarnings("checkstyle:hiddenField")
102         public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
103             children.forEach(this::withChild);
104             return this;
105         }
106
107         @Override
108         public ImmutableNormalizedMetadata build() {
109             final PathArgument id = identifier;
110             checkArgument(id != null, "Identifier has not been set");
111             return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
112                     : new Container(id, annotations, children);
113         }
114     }
115 }