2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.rfc7952.data.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collection;
16 import java.util.HashMap;
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;
25 * Immutable implementation of {@link NormalizedMetadata}.
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;
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);
40 public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
45 private final @NonNull ImmutableMap<QName, Object> annotations;
47 ImmutableNormalizedMetadata(final @NonNull PathArgument identifier, final Map<QName, Object> annotations) {
49 this.annotations = ImmutableMap.copyOf(annotations);
53 * Return a new {@link Builder}.
55 * @return A new Builder.
57 public static final @NonNull Builder builder() {
62 public final ImmutableMap<QName, Object> getAnnotations() {
67 * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
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;
76 // Hidden to prevent instantiation
79 @SuppressWarnings("checkstyle:hiddenField")
80 public Builder withIdentifier(final PathArgument identifier) {
81 this.identifier = requireNonNull(identifier);
85 public Builder withAnnotation(final QName type, final Object value) {
86 annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
90 @SuppressWarnings("checkstyle:hiddenField")
91 public Builder withAnnotations(final Map<QName, Object> annotations) {
92 annotations.forEach(this::withAnnotation);
96 public Builder withChild(final ImmutableNormalizedMetadata child) {
97 children.put(child.getIdentifier(), child);
101 @SuppressWarnings("checkstyle:hiddenField")
102 public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
103 children.forEach(this::withChild);
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);