Add ReusableImmutableNormalizedNodeStreamWriter
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / ReusableImmutableNormalizedNodeStreamWriter.java
diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/ReusableImmutableNormalizedNodeStreamWriter.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/ReusableImmutableNormalizedNodeStreamWriter.java
new file mode 100644 (file)
index 0000000..c9f7974
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.schema;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * A reusable variant of {@link ImmutableNormalizedNodeStreamWriter}. It can be reset into its base state and used for
+ * multiple streaming sessions.
+ */
+@Beta
+public final class ReusableImmutableNormalizedNodeStreamWriter extends ImmutableNormalizedNodeStreamWriter {
+    private final NormalizedNodeResultBuilder builder;
+
+    private ReusableImmutableNormalizedNodeStreamWriter(final NormalizedNodeResultBuilder builder) {
+        super(builder);
+        this.builder = requireNonNull(builder);
+    }
+
+    public static @NonNull ReusableImmutableNormalizedNodeStreamWriter create() {
+        return new ReusableImmutableNormalizedNodeStreamWriter(new NormalizedNodeResultBuilder());
+    }
+
+    public void reset() {
+        builder.result().reset();
+        reset(builder);
+    }
+
+    public NormalizedNode<?, ?> getResult() {
+        return builder.result().getResult();
+    }
+}