71df459b86fe5df290c87a6c01c3f9a2d74a7c2b
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizationResultHolder.java
1 /*
2  * Copyright (c) 2023 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.yang.data.impl.schema;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.Mutable;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedMetadata;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedMountpoints;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizationResult;
19
20 /**
21  * Client-allocated result holder for {@link ImmutableNormalizedNodeStreamWriter} operation. The operation can result
22  * in multiple distinct, but related items, to be produced -- as captured by {@link NormalizationResult}.
23  */
24 public final class NormalizationResultHolder implements Mutable {
25     private NormalizedNode data;
26     private NormalizedMetadata metadata;
27     private NormalizedMountpoints mountPoints;
28
29     public @Nullable NormalizationResult result() {
30         final var localData = data;
31         return localData == null ? null : new NormalizationResult(localData, metadata, mountPoints);
32     }
33
34     public @NonNull NormalizationResult getResult() {
35         final var localData = data;
36         if (localData == null) {
37             throw new IllegalStateException("Holder " + this + " has not been completed");
38         }
39         return new NormalizationResult(localData, metadata, mountPoints);
40     }
41
42     void setData(final NormalizedNode data) {
43         if (this.data != null) {
44             throw new ResultAlreadySetException("Normalized Node result was already set.", this.data);
45         }
46         this.data = requireNonNull(data);
47     }
48
49     void setMetadata(final NormalizedMetadata metadata) {
50         this.metadata = metadata;
51     }
52
53     void setMountPoints(final NormalizedMountpoints mountPoints) {
54         this.mountPoints = mountPoints;
55     }
56
57     void reset() {
58         data = null;
59         metadata = null;
60         mountPoints = null;
61     }
62 }