Separate out FileStateSet 88/104588/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 26 Feb 2023 17:31:56 +0000 (18:31 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 26 Feb 2023 17:57:29 +0000 (18:57 +0100)
YangToSourcesState is mucking with maps, where this functionality will
be shared with another set of files. Split the functionality out to a
separate construct.

JIRA: YANGTOOLS-1166
Change-Id: I20c824eb5d1d9eabeaaf67ae3945f8da016d985c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/FileStateSet.java [new file with mode: 0644]
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesState.java

diff --git a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/FileStateSet.java b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/FileStateSet.java
new file mode 100644 (file)
index 0000000..c587cd6
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2023 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.yang2sources.plugin;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.concepts.WritableObject;
+
+/**
+ * A collection of {@link FileState} objects indexed by their {@link FileState#path()}.
+ */
+record FileStateSet(@NonNull ImmutableMap<String, FileState> fileStates) implements WritableObject {
+    FileStateSet {
+        requireNonNull(fileStates);
+    }
+
+    static @NonNull FileStateSet readFrom(final DataInput in) throws IOException {
+        final int size = in.readInt();
+        final var fileStates = ImmutableMap.<String, FileState>builderWithExpectedSize(size);
+        for (int i = 0; i < size; ++i) {
+            final var fileState = FileState.read(in);
+            fileStates.put(fileState.path(), fileState);
+        }
+        return new FileStateSet(fileStates.build());
+    }
+
+    @Override
+    public void writeTo(final DataOutput out) throws IOException {
+        out.writeInt(fileStates.size());
+        for (var fileState : fileStates.values()) {
+            // TODO: discover common prefix and serialize it just once -- but that will complicate things a log, as
+            //       we really maintain a hierarchy, which means we want the Map sorted in a certain way.
+            fileState.writeTo(out);
+        }
+    }
+}
index 31f3e028aa85b1852fcc428258f29a4ccfe8c4a7..ebfcba18265fc10b7b03ce5c575b04c5531a8780 100644 (file)
@@ -245,7 +245,7 @@ class YangToSourcesProcessor {
         }
 
         // FIXME: store these files into state, so that we can verify/clean up
-        final var outputState = new YangToSourcesState(ImmutableMap.copyOf(uniqueOutputFiles));
+        final var outputState = new YangToSourcesState(new FileStateSet(ImmutableMap.copyOf(uniqueOutputFiles)));
         buildContext.setValue(BUILD_CONTEXT_STATE_NAME, outputState);
         if (buildContext.getValue(BUILD_CONTEXT_STATE_NAME) == null) {
             LOG.debug("{} BuildContext did not retain state, persisting", LOG_PREFIX);
index be9bf4de99b49a5bef3e5ae326200e6537de77b9..99cb35dc3f1b6515bf3c070af148be07b7ef1ada 100644 (file)
@@ -9,14 +9,9 @@ package org.opendaylight.yangtools.yang2sources.plugin;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.WritableObject;
 
@@ -26,47 +21,17 @@ import org.opendaylight.yangtools.concepts.WritableObject;
 // FIXME: expand to capture:
 //        - input YANG files
 //        - code generators and their config
-record YangToSourcesState(@NonNull ImmutableMap<String, FileState> outputFiles) implements WritableObject {
+record YangToSourcesState(@NonNull FileStateSet outputFiles) implements WritableObject {
     YangToSourcesState {
         requireNonNull(outputFiles);
     }
 
     static @NonNull YangToSourcesState readFrom(final DataInput in) throws IOException {
-        final ImmutableMap<String, FileState> outputStateMap = readToMap(in, FileState::read, FileState::path);
-        return new YangToSourcesState(outputStateMap);
-
+        return new YangToSourcesState(FileStateSet.readFrom(in));
     }
 
     @Override
     public void writeTo(final DataOutput out) throws IOException {
-        write(out, outputFiles.values());
-    }
-
-    private static <T extends WritableObject> void write(final DataOutput out, final Collection<T> items)
-            throws IOException {
-        out.writeInt(items.size());
-        for (var item : items) {
-            // TODO: discover common prefix and serialize it just once -- but that will complicate things a log, as
-            //       we really maintain a hierarchy, which means we want the Map sorted in a certain way.
-            item.writeTo(out);
-        }
-    }
-
-    private static <T extends WritableObject> ImmutableMap<String, T> readToMap(final DataInput in,
-            final DataReader<T> reader, final Function<T, String> keyExtractor) throws IOException {
-        final int size = in.readInt();
-        if (size == 0) {
-            ImmutableMap.of();
-        }
-        final var outputFiles = new ArrayList<T>(size);
-        for (int i = 0; i < size; ++i) {
-            outputFiles.add(reader.read(in));
-        }
-        return Maps.uniqueIndex(outputFiles, keyExtractor::apply);
-    }
-
-    @FunctionalInterface
-    interface DataReader<T extends WritableObject> {
-        T read(DataInput in) throws IOException;
+        outputFiles.writeTo(out);
     }
 }