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
|
public static java.util.Map<String,String> unjoinToMap(String string, String keyValueSeparator, String entriesSeparator, String begin, String end) {
java.util.List<String> stringEntries = unjoin(string, entriesSeparator, begin, end);
java.util.Map<String,String> result = new java.util.HashMap<String,String>();
for (String stringEntry : stringEntries) {
int index = stringEntry.indexOf(keyValueSeparator);
result.put(stringEntry.substring(0, index), stringEntry.substring(index + keyValueSeparator.length()));
}
return result;
}
public static java.util.List<String> unjoin(String string, String separator, String begin, String end) {
return unjoin(removePrefixAndSuffix(string, begin, end), separator);
}
public static java.util.List<String> unjoin(String string, String separator) {
if (string == null) {
throw new NullPointerException("string is null");
}
return java.util.Arrays.asList(string.split(java.util.regex.Pattern.quote(separator), -1));
}
public static String removePrefixAndSuffix(String string, String begin, String end) {
if (string == null) {
throw new NullPointerException("string is null");
}
if (!string.startsWith(begin)) {
throw new IllegalArgumentException(String.format("'%s' not starts by '%s'", string, begin));
}
if (!string.endsWith(end)) {
throw new IllegalArgumentException(String.format("'%s' not finishs by '%s'", string, end));
}
int fromIndex = begin.length();
int toIndex = string.length() - end.length();
if (toIndex < fromIndex) {
throw new IllegalArgumentException(String.format("Collision in '%s' with prefix '%s' and suffix '%s'", string, begin, end));
}
return string.substring(fromIndex, toIndex);
} |
Partager