diff options
Diffstat (limited to 'config-lib/src')
4 files changed, 73 insertions, 59 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/ModelReference.java b/config-lib/src/main/java/com/yahoo/config/ModelReference.java index 13bb5737c6f..25caad55b84 100644 --- a/config-lib/src/main/java/com/yahoo/config/ModelReference.java +++ b/config-lib/src/main/java/com/yahoo/config/ModelReference.java @@ -22,57 +22,41 @@ public class ModelReference { // Or: If resolved, this is set private final Path resolved; - public ModelReference(Path resolved) { - this.modelId = Optional.empty(); - this.url = Optional.empty(); - this.path = Optional.empty(); - this.resolved = resolved; - } - - public ModelReference(Optional<String> modelId, - Optional<UrlReference> url, - Optional<FileReference> path) { - if (modelId.isEmpty() && url.isEmpty() && path.isEmpty()) - throw new IllegalArgumentException("A model reference must have either a model id, url or path"); + private ModelReference(Optional<String> modelId, + Optional<UrlReference> url, + Optional<FileReference> path, + Path resolved) { this.modelId = modelId; this.url = url; this.path = path; - this.resolved = null; + this.resolved = resolved; } + /** Returns the id specified for this model, oor null if it is resolved. */ public Optional<String> modelId() { return modelId; } - public Optional<UrlReference> url() { return url; } - public Optional<FileReference> path() { return path; } - public ModelReference withModelId(Optional<String> modelId) { - return new ModelReference(modelId, url, path); - } - - public ModelReference withUrl(Optional<UrlReference> url) { - return new ModelReference(modelId, url, path); - } + /** Returns the url specified for this model, or null if it is resolved. */ + public Optional<UrlReference> url() { return url; } - public ModelReference withPath(Optional<FileReference> path) { - return new ModelReference(modelId, url, path); - } + /** Returns the path specified for this model, oor null if it is resolved. */ + public Optional<FileReference> path() { return path; } - /** Returns the path to the file containing this model, or null if not available. */ - public Path value() { - return resolved; - } + /** Returns the path to the file containing this model, or null if this is unresolved. */ + public Path value() { return resolved; } @Override public boolean equals(Object o) { if ( ! (o instanceof ModelReference other)) return false; - if ( ! this.modelId.equals(other.modelId)) return false; - if ( ! this.url.equals(other.url)) return false; - if ( ! this.path.equals(other.path)) return false; + if ( ! Objects.equals(this.modelId, other.modelId)) return false; + if ( ! Objects.equals(this.url, other.url)) return false; + if ( ! Objects.equals(this.path, other.path)) return false; + if ( ! Objects.equals(this.resolved, other.resolved)) return false; return true; } @Override public int hashCode() { - return Objects.hash(modelId, url, path); + return Objects.hash(modelId, url, path, resolved); } /** Returns this on the format accepted by valueOf */ @@ -84,26 +68,50 @@ public class ModelReference { path.map(v -> v.value()).orElse("\"\""); } - /** Creates a model reference resolved to a Path to the local file. */ - public static ModelReference valueOf(Path path) { - return new ModelReference(path); - } - /** - * Creates a model reference from a three-part string on the form - * <code>modelId url path</code> - * Each of the elements is either a value not containing space, or empty represented by "". + * Creates a model reference which is either a single string with no spaces if resolved, or if unresolved + * a three-part string on the form <code>modelId url path</code>, where + * each of the elements is either a value not containing space, or empty represented by "". */ public static ModelReference valueOf(String s) { String[] parts = s.split(" "); if (parts.length == 1) - return new ModelReference(Path.of(s)); + return resolved(Path.of(s)); else if (parts.length == 3) - return new ModelReference(parts[0].equals("\"\"") ? Optional.empty() : Optional.of(parts[0]), - parts[1].equals("\"\"") ? Optional.empty() : Optional.of(new UrlReference(parts[1])), - parts[2].equals("\"\"") ? Optional.empty() : Optional.of(new FileReference(parts[2]))); + return unresolved(parts[0].equals("\"\"") ? Optional.empty() : Optional.of(parts[0]), + parts[1].equals("\"\"") ? Optional.empty() : Optional.of(new UrlReference(parts[1])), + parts[2].equals("\"\"") ? Optional.empty() : Optional.of(new FileReference(parts[2]))); else - throw new IllegalArgumentException("Unexpected model string '" + s + "'"); + throw new IllegalArgumentException("Unexpected model reference string '" + s + "'"); + } + + /** Creates an unresolved reference from a model id only. */ + public static ModelReference unresolved(String modelId) { + return new ModelReference(Optional.of(modelId), Optional.empty(), Optional.empty(), null); + } + + /** Creates an unresolved reference from an url only. */ + public static ModelReference unresolved(UrlReference url) { + return new ModelReference(Optional.empty(), Optional.of(url), Optional.empty(), null); + } + + /** Creates an unresolved reference from a path only. */ + public static ModelReference unresolved(FileReference path) { + return new ModelReference(Optional.empty(), Optional.empty(), Optional.of(path), null); + } + + /** Creates an unresolved reference. */ + public static ModelReference unresolved(Optional<String> modelId, + Optional<UrlReference> url, + Optional<FileReference> path) { + if (modelId.isEmpty() && url.isEmpty() && path.isEmpty()) + throw new IllegalArgumentException("A model reference must have either a model id, url or path"); + return new ModelReference(modelId, url, path, null); + } + + /** Creates a nresolved reference. */ + public static ModelReference resolved(Path path) { + return new ModelReference(null, null, null, Objects.requireNonNull(path)); } } diff --git a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java index d1cd7678911..76871aaca42 100644 --- a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java +++ b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java @@ -169,9 +169,7 @@ public class ConfigInstanceBuilderTest { fileVal("etc"). pathVal(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))). urlVal(new UrlReference("http://docs.vespa.ai")). - modelVal(new ModelReference(Optional.empty(), - Optional.empty(), - Optional.of(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))))). + modelVal(ModelReference.unresolved(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml")))). boolarr(false). longarr(9223372036854775807L). longarr(-9223372036854775808L). diff --git a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java index 1a05c08d8f2..db1509fba93 100644 --- a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java +++ b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java @@ -132,9 +132,9 @@ public class ConfigInstanceEqualsTest { fileVal("etc"). pathVal(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))). urlVal(new UrlReference("http://docs.vespa.ai")). - modelVal(new ModelReference(Optional.of("my-model-id"), - Optional.of(new UrlReference("http://docs.vespa.ai")), - Optional.empty())). + modelVal(ModelReference.unresolved(Optional.of("my-model-id"), + Optional.of(new UrlReference("http://docs.vespa.ai")), + Optional.empty())). boolarr(false). longarr(9223372036854775807L). longarr(-9223372036854775808L). @@ -145,9 +145,9 @@ public class ConfigInstanceEqualsTest { refarr(Arrays.asList(":parent:", ":parent", "parent:")). // test collection based setter fileArr("bin"). urlArr(new UrlReference("http://docs.vespa.ai")). - modelArr(new ModelReference(Optional.empty(), - Optional.of(new UrlReference("http://docs.vespa.ai")), - Optional.of(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))))). + modelArr(ModelReference.unresolved(Optional.empty(), + Optional.of(new UrlReference("http://docs.vespa.ai")), + Optional.of(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))))). basicStruct(new BasicStruct.Builder(). foo("basicFoo"). diff --git a/config-lib/src/test/java/com/yahoo/config/ModelNodeTest.java b/config-lib/src/test/java/com/yahoo/config/ModelNodeTest.java index 696e0722714..328b27bf4c8 100644 --- a/config-lib/src/test/java/com/yahoo/config/ModelNodeTest.java +++ b/config-lib/src/test/java/com/yahoo/config/ModelNodeTest.java @@ -3,6 +3,7 @@ package com.yahoo.config; import org.junit.jupiter.api.Test; +import java.nio.file.Path; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -18,12 +19,19 @@ public class ModelNodeTest { } @Test - void testReference() { - var reference = new ModelReference(Optional.of("myModelId"), - Optional.of(new UrlReference("https://host:my/path")), - Optional.of(new FileReference("foo.txt"))); + void testUnresolvedReference() { + var reference = ModelReference.unresolved(Optional.of("myModelId"), + Optional.of(new UrlReference("https://host:my/path")), + Optional.of(new FileReference("foo.txt"))); assertEquals("myModelId https://host:my/path foo.txt", reference.toString()); assertEquals(reference, ModelReference.valueOf(reference.toString())); } + @Test + void testResolvedReference() { + var reference = ModelReference.resolved(Path.of("dir/resolvedFile.txt")); + assertEquals("dir/resolvedFile.txt", reference.toString()); + assertEquals(reference, ModelReference.valueOf(reference.toString())); + } + } |