diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /component/src/main/java/com/yahoo/component/SpecSplitter.java |
Publish
Diffstat (limited to 'component/src/main/java/com/yahoo/component/SpecSplitter.java')
-rw-r--r-- | component/src/main/java/com/yahoo/component/SpecSplitter.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/component/src/main/java/com/yahoo/component/SpecSplitter.java b/component/src/main/java/com/yahoo/component/SpecSplitter.java new file mode 100644 index 00000000000..0517bbaceef --- /dev/null +++ b/component/src/main/java/com/yahoo/component/SpecSplitter.java @@ -0,0 +1,44 @@ +// Copyright 2016 Yahoo Inc. 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); + } + } +} |