aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/ml/ModelName.java
blob: 1e5e95f06d563d923260279f2168799c828987c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.ml;

import com.yahoo.path.Path;

/**
 * Models used in a rank profile has the rank profile name as name space while global model names have no namespace
 *
 * @author bratseth
 */
public class ModelName {

    /** The namespace, or null if none */
    private final String namespace;
    private final String name;
    private final String fullName;

    public ModelName(String name) {
        this(null, name);
    }

    public ModelName(String namespace, Path modelPath, boolean pathIsFile) {
        this(namespace,
             stripFileEndingIfFile(modelPath, pathIsFile).toString().replace("/", "_"));
    }

    private ModelName(String namespace, String name) {
        this.namespace = namespace;
        this.name = name;
        this.fullName = (namespace != null ? namespace + "." : "") + name;
    }

    private static Path stripFileEndingIfFile(Path path, boolean pathIsFile) {
        if ( ! pathIsFile) return path;
        int dotIndex = path.last().lastIndexOf(".");
        if (dotIndex <= 0) return path;
        return path.withLast(path.last().substring(0, dotIndex));
    }

    /** Returns true if the local name of this is not in a namespace */
    public boolean isGlobal() { return namespace == null; }

    /** Returns the namespace, or null if this is global */
    public String namespace() { return namespace; }
    public String localName() { return name; }
    public String fullName() { return fullName; }


    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof ModelName)) return false;
        return ((ModelName)o).fullName.equals(this.fullName);
    }

    @Override
    public int hashCode() { return fullName.hashCode(); }

    @Override
    public String toString() { return fullName; }

}