blob: f21850ac387a0349e1725e09f34021237a0446e4 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component;
import java.util.Arrays;
import java.util.List;
/**
* Splits and component id or component specification string
* into their constituent parts.
* @author tonytv
*/
class SpecSplitter {
final String name;
final String version;
final ComponentId namespace;
SpecSplitter(String spec) {
List<String> idAndNamespace = splitFirst(spec, '@');
List<String> nameAndVersion = splitFirst(idAndNamespace.get(0), ':');
name = nameAndVersion.get(0);
version = second(nameAndVersion);
namespace = ComponentId.fromString(second(idAndNamespace));
}
private String second(List<String> components) {
return components.size() == 2?
components.get(1) :
null;
}
private static List<String> splitFirst(String string, char c) {
int index = string.indexOf(c);
if (index != -1) {
if (index == string.length() - 1) {
throw new RuntimeException("Expected characters after '" + c + "'");
}
return Arrays.asList(string.substring(0, index),
string.substring(index + 1));
} else {
return Arrays.asList(string, null);
}
}
}
|