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.yang.data.impl.schema;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.HashMap;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedMetadata;
22 * Immutable implementation of {@link NormalizedMetadata}.
24 public sealed class ImmutableNormalizedMetadata implements NormalizedMetadata {
25 private static final class Container extends ImmutableNormalizedMetadata {
26 private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
28 Container(final Map<QName, Object> annotations,
29 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
31 this.children = ImmutableMap.copyOf(children);
35 public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
40 private final @NonNull ImmutableMap<QName, Object> annotations;
42 ImmutableNormalizedMetadata(final Map<QName, Object> annotations) {
43 this.annotations = ImmutableMap.copyOf(annotations);
47 * Return a new {@link Builder}.
49 * @return A new Builder.
51 public static final @NonNull Builder builder() {
56 public final ImmutableMap<QName, Object> getAnnotations() {
61 * A Builder of {@link ImmutableNormalizedMetadata} instances.
63 public static final class Builder implements Mutable {
64 private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
65 private final Map<QName, Object> annotations = new HashMap<>();
68 // Hidden to prevent instantiation
71 public @NonNull Builder withAnnotation(final QName type, final Object value) {
72 annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
76 @SuppressWarnings("checkstyle:hiddenField")
77 public @NonNull Builder withAnnotations(final Map<QName, Object> annotations) {
78 annotations.forEach(this::withAnnotation);
82 public @NonNull Builder withChild(final PathArgument pathArgument, final ImmutableNormalizedMetadata child) {
83 children.put(requireNonNull(pathArgument, "pathArgument"), requireNonNull(child, "child"));
87 @SuppressWarnings("checkstyle:hiddenField")
88 public @NonNull Builder withChildren(final Map<PathArgument, ImmutableNormalizedMetadata> children) {
89 children.forEach(this::withChild);
94 * Return an {@link ImmutableNormalizedMetadata} view of this builder's state.
96 * @return An ImmutableNormalizedMetadata instace
97 * @throws IllegalStateException if this builder does not have enough state
99 public @NonNull ImmutableNormalizedMetadata build() {
100 return children.isEmpty() ? new ImmutableNormalizedMetadata(annotations)
101 : new Container(annotations, children);