diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-09-01 12:59:06 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-09-01 12:59:06 +0200 |
commit | 86a39ab21db4a92b46944709251ccab58ef39370 (patch) | |
tree | 5cac1fc0065019c5161b4000ba28f4382322a664 /config-lib/src | |
parent | 0652206123f2a65156bf21663aac0e75b4f43b73 (diff) |
Use Path as user type for models
Diffstat (limited to 'config-lib/src')
4 files changed, 22 insertions, 18 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java b/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java index bdabc045e18..ddbed4258dc 100644 --- a/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java +++ b/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java @@ -78,11 +78,11 @@ public class LeafNodeVector<REAL, NODE extends LeafNode<REAL>> extends NodeVecto return new LeafNodeVector<>(files, new UrlNode()); } - public static LeafNodeVector<ModelReference, ModelNode> createModelNodeVector(Collection<ModelReference> values) { - List<ModelReference> models = new ArrayList<>(); + public static LeafNodeVector<Path, ModelNode> createModelNodeVector(Collection<ModelReference> values) { + List<Path> modelPaths = new ArrayList<>(); for (ModelReference modelReference : values) - models.add(modelReference); - return new LeafNodeVector<>(models, new ModelNode()); + modelPaths.add(modelReference.value()); + return new LeafNodeVector<>(modelPaths, new ModelNode()); } } diff --git a/config-lib/src/main/java/com/yahoo/config/ModelNode.java b/config-lib/src/main/java/com/yahoo/config/ModelNode.java index 775c4a12e82..2748ef8c7e9 100644 --- a/config-lib/src/main/java/com/yahoo/config/ModelNode.java +++ b/config-lib/src/main/java/com/yahoo/config/ModelNode.java @@ -12,19 +12,19 @@ import java.util.Map; * * @author bratseth */ -public class ModelNode extends LeafNode<ModelReference> { +public class ModelNode extends LeafNode<Path> { + + private final ModelReference reference; public ModelNode() { this.value = null; + this.reference = null; } public ModelNode(ModelReference modelReference) { super(true); - this.value = modelReference; - } - - public ModelReference value() { - return value; + this.value = modelReference.value(); + this.reference = modelReference; } @Override @@ -43,7 +43,7 @@ public class ModelNode extends LeafNode<ModelReference> { } public ModelReference getModelReference() { - return value; + return reference; } public static List<ModelReference> toModelReferences(List<ModelNode> modelNodes) { 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 c0b610e4df3..7f7082384af 100644 --- a/config-lib/src/main/java/com/yahoo/config/ModelReference.java +++ b/config-lib/src/main/java/com/yahoo/config/ModelReference.java @@ -2,6 +2,7 @@ package com.yahoo.config; import java.io.File; +import java.nio.file.Path; import java.util.Objects; import java.util.Optional; @@ -45,13 +46,13 @@ public class ModelReference { return new ModelReference(modelId, url, path); } - /** Returns the path to the file containing this model. */ - public String value() { + /** Returns the path to the file containing this model, or null if not available. */ + public Path value() { if (url.isPresent() && new File(url.get().value()).exists()) - return new File(url.get().value()).getAbsolutePath(); + return Path.of(url.get().value()); if (path.isPresent()) - return path.get().value(); - throw new IllegalStateException("No url or path is available"); + return Path.of(path.get().value()); + return null; } @Override @@ -91,6 +92,11 @@ public class ModelReference { return new ModelReference(Optional.empty(), Optional.empty(), Optional.of(new FileReference(path))); } + /** Creates a model reference having a path only. */ + public static ModelReference valueOf(Path path) { + return fromPath(path.toString()); + } + /** * Creates a model reference from a three-part string on the form * <code>modelId url path</code> diff --git a/config-lib/src/main/java/com/yahoo/config/PathNode.java b/config-lib/src/main/java/com/yahoo/config/PathNode.java index b757e569ffe..10100d374d6 100644 --- a/config-lib/src/main/java/com/yahoo/config/PathNode.java +++ b/config-lib/src/main/java/com/yahoo/config/PathNode.java @@ -23,8 +23,6 @@ public class PathNode extends LeafNode<Path> { public PathNode(FileReference fileReference) { super(true); this.value = Path.of(fileReference.value()); - if (value.normalize().toString().startsWith("..")) - throw new IllegalArgumentException("path may not start with '..', but got: " + value); this.fileReference = fileReference; } |