aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/config/model/builder/xml/ConfigModelId.java
blob: e1970b001e1420d166b92d6eec2f67a9b7b164b2 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.builder.xml;

import com.yahoo.component.Version;

/**
 * A {@link ConfigModelId} describes an element handled by a {@link ConfigModelBuilder}.
 *
 * @author Ulf Lilleengen
 */
public class ConfigModelId implements Comparable<ConfigModelId> {

    private final String name;
    private final Version version;
    private final String stringValue;

    private ConfigModelId(String name, Version version) {
        this.name = name;
        this.version = version;
        this.stringValue = toStringValue();
    }

    /**
     * Create id with a name and version
     * @param tagName Name of the id
     * @param tagVersion Version of the id
     * @return A ConfigModelId instance
     */
    public static ConfigModelId fromNameAndVersion(String tagName, String tagVersion) {
        return new ConfigModelId(tagName, Version.fromString(tagVersion));
    }

    /**
     * Create id with given name, using default version 1.
     *
     * @param tagName Name of the id
     * @return A ConfigModelId instance
     */
    public static ConfigModelId fromName(String tagName) {
        return new ConfigModelId(tagName, new Version(1));
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ConfigModelId other)) return false;
        return this.name.equals(other.name) && this.version.equals(other.version);
    }

    @Override
    public int compareTo(ConfigModelId other) {
        if (other == this) return 0;
        int cmp = this.name.compareTo(other.name);
        if (cmp == 0) {
            cmp = this.version.compareTo(other.version);
        }
        return cmp;
    }

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

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

    /**
     * Return the XML element name.
     * @return the name of the config model
     */
    public String getName() {
        return name;
    }

    /**
     * Return the XML element version.
     * @return the version of the config model
     */
    Version getVersion() {
        return version;
    }

    private String toStringValue() {
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        sb.append(".");
        sb.append(version);
        return sb.toString();
    }
}