Expose DelegatedYangTextSource 58/109658/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 14:13:40 +0000 (15:13 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 14:37:14 +0000 (15:37 +0100)
Eliminate YangTextSource static methods in favor of making
DelegatedYangTextSource visible. This also eliminates the slew of
possibilitiess down to the bare bones -- users can cope with the trivial
conversions from ByteSource and file name.

JIRA: YANGTOOLS-1561
Change-Id: I2321e9d936489fefe059399a750393b76730e297
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/DelegatedYangTextSource.java
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java
parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java

index 1f89a736002991ef10c24e5e169e7e63ab12c039..d965c8243afa3c31a7d02df9a1b8e4b8bc770525 100644 (file)
@@ -14,29 +14,40 @@ import com.google.common.io.CharSource;
 import java.io.IOException;
 import java.io.Reader;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.concepts.Delegator;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 
-final class DelegatedYangTextSource extends YangTextSource implements Delegator<CharSource> {
-    private final @NonNull CharSource delegate;
-
-    DelegatedYangTextSource(final SourceIdentifier sourceId, final CharSource delegate) {
+/**
+ * A {@link YangTextSource} delegating to a {@link CharSource}.
+ */
+@NonNullByDefault
+public class DelegatedYangTextSource extends YangTextSource implements Delegator<CharSource> {
+    private final CharSource delegate;
+
+    /**
+     * Default constructor.
+     *
+     * @param sourceId {@link SourceIdentifier} of the resulting schema source
+     * @param delegate Backing {@link CharSource} instance
+     */
+    public DelegatedYangTextSource(final SourceIdentifier sourceId, final CharSource delegate) {
         super(sourceId);
         this.delegate = requireNonNull(delegate);
     }
 
     @Override
-    public CharSource getDelegate() {
+    public final CharSource getDelegate() {
         return delegate;
     }
 
     @Override
-    public Reader openStream() throws IOException {
+    public final Reader openStream() throws IOException {
         return delegate.openStream();
     }
 
     @Override
-    public String symbolicName() {
+    public final @NonNull String symbolicName() {
         return "[" + delegate.toString() + "]";
     }
 
index 6c565eb90f6a71c60f756aa5afe43adcd7e441cc..9fbdb35a839e3a2c3bba791f5129764916905b32 100644 (file)
@@ -11,7 +11,6 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.io.ByteSource;
 import com.google.common.io.CharSource;
 import com.google.common.io.Resources;
 import java.io.File;
@@ -35,61 +34,6 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep
         this.sourceId = requireNonNull(sourceId);
     }
 
-    /**
-     * Create a new YangTextSchemaSource with a specific source identifier and backed
-     * by ByteSource, which provides the actual InputStreams.
-     *
-     * @param identifier SourceIdentifier of the resulting schema source
-     * @param delegate Backing ByteSource instance
-     * @param charset Expected character set
-     * @return A new YangTextSchemaSource
-     */
-    public static @NonNull YangTextSource delegateForByteSource(final SourceIdentifier identifier,
-            final ByteSource delegate, final Charset charset) {
-        return delegateForCharSource(identifier, delegate.asCharSource(charset));
-    }
-
-    /**
-     * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
-     * by ByteSource, which provides the actual InputStreams.
-     *
-     * @param fileName File name
-     * @param delegate Backing ByteSource instance
-     * @return A new YangTextSchemaSource
-     * @throws IllegalArgumentException if the file name has invalid format
-     */
-    public static @NonNull YangTextSource delegateForByteSource(final String fileName,
-            final ByteSource delegate, final Charset charset) {
-        return delegateForCharSource(fileName, delegate.asCharSource(charset));
-    }
-
-    /**
-     * Create a new YangTextSchemaSource with a specific source identifier and backed
-     * by ByteSource, which provides the actual InputStreams.
-     *
-     * @param identifier SourceIdentifier of the resulting schema source
-     * @param delegate Backing CharSource instance
-     * @return A new YangTextSchemaSource
-     */
-    public static @NonNull YangTextSource delegateForCharSource(final SourceIdentifier identifier,
-            final CharSource delegate) {
-        return new DelegatedYangTextSource(identifier, delegate);
-    }
-
-    /**
-     * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
-     * by ByteSource, which provides the actual InputStreams.
-     *
-     * @param fileName File name
-     * @param delegate Backing CharSource instance
-     * @return A new YangTextSchemaSource
-     * @throws IllegalArgumentException if the file name has invalid format
-     */
-    public static @NonNull YangTextSource delegateForCharSource(final String fileName,
-            final CharSource delegate) {
-        return new DelegatedYangTextSource(SourceIdentifier.ofYangFileName(fileName), delegate);
-    }
-
     /**
      * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
      * name.
@@ -229,7 +173,7 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep
      * @param toStringHelper ToStringHelper onto the attributes can be added
      * @return ToStringHelper supplied as input argument.
      */
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+    protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
         return toStringHelper.add("identifier", sourceId);
     }
 }
index 6667210efffc32d929ef7690134e8738d333a87a..3e64b0027a56a14921898a35006338705c74d0c0 100644 (file)
@@ -49,6 +49,7 @@ import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
@@ -131,7 +132,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
                 }
             }
 
-            text = YangTextSource.delegateForCharSource(parsedId, source);
+            text = new DelegatedYangTextSource(parsedId, source);
         } else {
             text = source;
         }
index efa1db1ea5bc93bc6ef9fe71cfa4109a21e22b17..8bd3f194dbb6b17103e7504565673b824e3386e8 100644 (file)
@@ -16,7 +16,6 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.io.ByteSource;
-import com.google.common.io.ByteStreams;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -28,6 +27,8 @@ import java.util.zip.ZipFile;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.project.MavenProject;
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -57,15 +58,14 @@ abstract class ScannedDependency {
         ImmutableList<YangTextSource> sources() throws IOException {
             final var builder = ImmutableList.<YangTextSource>builderWithExpectedSize(entryNames.size());
 
-            try (ZipFile zip = new ZipFile(file())) {
-                for (String entryName : entryNames) {
-                    final ZipEntry entry = requireNonNull(zip.getEntry(entryName));
+            try (var zip = new ZipFile(file())) {
+                for (var entryName : entryNames) {
+                    final var entry = requireNonNull(zip.getEntry(entryName));
 
-                    builder.add(YangTextSource.delegateForByteSource(
-                        entryName.substring(entryName.lastIndexOf('/') + 1),
-                        // FIXME: can we reasonable make this a CharSource?
-                        ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry))),
-                        StandardCharsets.UTF_8));
+                    builder.add(new DelegatedYangTextSource(
+                        SourceIdentifier.ofYangFileName(entryName.substring(entryName.lastIndexOf('/') + 1)),
+                        ByteSource.wrap(zip.getInputStream(entry).readAllBytes())
+                            .asCharSource(StandardCharsets.UTF_8)));
                 }
             }
 
index bd06dd3a6b9075002ae95661055a7fcefb159ece..8297a392e2e04fe863f52773ff3fa0daf20b3703 100644 (file)
@@ -39,6 +39,7 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
 import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
@@ -400,8 +401,7 @@ class YangToSourcesProcessor {
 
                 if (!astSource.sourceId().equals(textSource.sourceId())) {
                     // AST indicates a different source identifier, make sure we use that
-                    sourcesInProject.add(YangTextSource.delegateForCharSource(astSource.sourceId(),
-                        textSource));
+                    sourcesInProject.add(new DelegatedYangTextSource(astSource.sourceId(), textSource));
                 } else {
                     sourcesInProject.add(textSource);
                 }