Do not subclass YangTextSchemaSource in yang-repo-fs 70/95870/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 21 Apr 2021 05:36:22 +0000 (07:36 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 21 Apr 2021 06:27:12 +0000 (08:27 +0200)
We have a readily-available File-based implementation, reuse it instead
of rolling our own.

JIRA: YANGTOOLS-1275
Change-Id: I0a6604b83cb1aa216e61f5f5be97f93d2d1dd0e4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit d23aa17bf81deed1dc7208d00bd15400954edf9c)

yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/FilesystemSchemaSourceCache.java

index 0e521561aa6927ae7c8225e215a7903da39b961a..d21fdcb021aecd36399355f7fda36ec470e7b07d 100644 (file)
@@ -78,11 +78,24 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch
      * @param file Backing File
      * @return A new YangTextSchemaSource
      * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
-     * @throws NullPointerException if file is null
+     * @throws NullPointerException if file is {@code null}
      */
     public static @NonNull YangTextSchemaSource forFile(final File file) {
+        return forFile(file, identifierFromFilename(file.getName()));
+    }
+
+    /**
+     * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
+     *
+     * @param file Backing File
+     * @param identifier source identifier
+     * @return A new YangTextSchemaSource
+     * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
+     * @throws NullPointerException if any argument is {@code null}
+     */
+    public static @NonNull YangTextSchemaSource forFile(final File file, final SourceIdentifier identifier) {
         checkArgument(file.isFile(), "Supplied file %s is not a file", file);
-        return new YangTextFileSchemaSource(identifierFromFilename(file.getName()), file);
+        return new YangTextFileSchemaSource(identifier, file);
     }
 
     /**
index 1c6b3c51084cfdf2d5fa67101238ebc256393cdc..b6086590a1c98d555de1269b01287451599e197b 100644 (file)
@@ -12,7 +12,6 @@ import static java.util.Objects.requireNonNull;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
-import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.util.concurrent.FluentFuture;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.File;
@@ -250,18 +249,7 @@ public final class FilesystemSchemaSourceCache<T extends SchemaSourceRepresentat
 
         @Override
         public YangTextSchemaSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
-            return new YangTextSchemaSource(sourceIdentifier) {
-
-                @Override
-                protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-                    return toStringHelper;
-                }
-
-                @Override
-                public InputStream openStream() throws IOException {
-                    return Files.newInputStream(cachedSource.toPath());
-                }
-            };
+            return YangTextSchemaSource.forFile(cachedSource, sourceIdentifier);
         }
     }