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.AbstractIdentifiable;
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 AbstractIdentifiable<PathArgument> implements NormalizedMetadata {
29 private static final class Container extends ImmutableNormalizedMetadata {
30 private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
32 Container(final PathArgument identifier, final Map<QName, Object> annotations,
33 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
34 super(identifier, annotations);
35 this.children = ImmutableMap.copyOf(children);
39 public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
44 private final @NonNull ImmutableMap<QName, Object> annotations;
46 ImmutableNormalizedMetadata(final @NonNull PathArgument identifier, final Map<QName, Object> annotations) {
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 ImmutableMap<QName, Object> getAnnotations() {
66 * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
68 public static final class Builder
69 implements org.opendaylight.yangtools.concepts.Builder<ImmutableNormalizedMetadata> {
70 private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
71 private final Map<QName, Object> annotations = new HashMap<>();
72 private PathArgument identifier;
75 // Hidden to prevent instantiation
78 @SuppressWarnings("checkstyle:hiddenField")
79 public Builder withIdentifier(final PathArgument identifier) {
80 this.identifier = requireNonNull(identifier);
84 public Builder withAnnotation(final QName type, final Object value) {
85 annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
89 @SuppressWarnings("checkstyle:hiddenField")
90 public Builder withAnnotations(final Map<QName, Object> annotations) {
91 annotations.forEach(this::withAnnotation);
95 public Builder withChild(final ImmutableNormalizedMetadata child) {
96 children.put(child.getIdentifier(), child);
100 @SuppressWarnings("checkstyle:hiddenField")
101 public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
102 children.forEach(this::withChild);
107 public ImmutableNormalizedMetadata build() {
108 final PathArgument id = identifier;
109 checkArgument(id != null, "Identifier has not been set");
110 return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
111 : new Container(id, annotations, children);