aboutsummaryrefslogtreecommitdiffstats
path: root/component/src/main/java/com/yahoo/component/SpecSplitter.java
blob: 450fb22c1ba5a739f332792da066bbeee3fea307 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component;

/**
 * Splits and component id or component specification string into their constituent parts.
 * 
 * @author Tony Vaagenes
 */
class SpecSplitter {

    final String name;
    final String version;
    final ComponentId namespace;

    SpecSplitter(String spec) {
        int indexOfAlpha = spec.indexOf('@');
        String nameAndVersion = spec;
        if (indexOfAlpha != -1) {
            if (indexOfAlpha == spec.length() - 1) {
                throw new RuntimeException("Expected characters after '@'");
            }
            nameAndVersion = spec.substring(0, indexOfAlpha);
            namespace = ComponentId.fromString(spec.substring(indexOfAlpha + 1));
        } else {
            namespace = null;
        }

        int indexOfColon = nameAndVersion.indexOf(':');
        if (indexOfColon != -1) {
            if (indexOfColon == nameAndVersion.length() - 1) {
                throw new RuntimeException("Expected characters after ':'");
            }
            name = nameAndVersion.substring(0, indexOfColon);
            version = nameAndVersion.substring(indexOfColon+1);
        } else {
            name = nameAndVersion;
            version = null;
        }
    }

}