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.rfc7952.data.api.NormalizedMetadata;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 * Immutable implementation of {@link NormalizedMetadata}.
27 public class ImmutableNormalizedMetadata implements NormalizedMetadata {
28 private static final class Container extends ImmutableNormalizedMetadata {
29 private final ImmutableMap<PathArgument, NormalizedMetadata> children;
31 Container(final PathArgument identifier, final Map<QName, Object> annotations,
32 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
33 super(identifier, annotations);
34 this.children = ImmutableMap.copyOf(children);
38 public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
43 private final @NonNull PathArgument identifier;
44 private final @NonNull ImmutableMap<QName, Object> annotations;
46 ImmutableNormalizedMetadata(final PathArgument identifier, final Map<QName, Object> annotations) {
47 this.identifier = requireNonNull(identifier);
48 this.annotations = ImmutableMap.copyOf(annotations);
52 * Return a new {@link Builder}.
54 * @return A new Builder.
56 public static final @NonNull Builder builder() {
61 public final PathArgument getIdentifier() {
66 public final ImmutableMap<QName, Object> getAnnotations() {
71 * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
73 public static final class Builder
74 implements org.opendaylight.yangtools.concepts.Builder<ImmutableNormalizedMetadata> {
75 private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
76 private final Map<QName, Object> annotations = new HashMap<>();
77 private PathArgument identifier;
80 // Hidden to prevent instantiation
83 @SuppressWarnings("checkstyle:hiddenField")
84 public Builder withIdentifier(final PathArgument identifier) {
85 this.identifier = requireNonNull(identifier);
89 public Builder withAnnotation(final QName type, final Object value) {
90 annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
94 @SuppressWarnings("checkstyle:hiddenField")
95 public Builder withAnnotations(final Map<QName, Object> annotations) {
96 annotations.forEach(this::withAnnotation);
100 public Builder withChild(final ImmutableNormalizedMetadata child) {
101 children.put(child.getIdentifier(), child);
105 @SuppressWarnings("checkstyle:hiddenField")
106 public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
107 children.forEach(this::withChild);
112 public ImmutableNormalizedMetadata build() {
113 final PathArgument id = identifier;
114 checkArgument(id != null, "Identifier has not been set");
115 return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
116 : new Container(id, annotations, children);