Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Utility class that helps with configuration key manipulation.
Expand All @@ -30,20 +28,14 @@
*/
public final class ConfigKeyUtil {

/**
* Split by {@code ;} with optional space symbols (space, tab, new line, etc.) before and after.
*/
private static Pattern ENTRY_SEPARATOR_PATTERN = Pattern.compile("\\s*;\\s*");
/**
* Split by {@code =} with optional space symbols (space, tab, new line, etc.) before and after.
*/
private static Pattern KEY_VALUE_SEPARATOR_PATTERN = Pattern.compile("\\s*=\\s*");

private ConfigKeyUtil() {
}

/**
* Convert configuration value of format {@code key1=value1;key2=value2;...} to {@link Map<String, String>}.
* <p>
* Parsing notes: surrounding whitespace is stripped from every key and value, entries with an empty
* key are skipped, and when the same key appears more than once the last occurrence wins.
*
* @param configValue configuration value string
* @return configuration values map
Expand All @@ -53,9 +45,26 @@ public static Map<String, String> toMap(String configValue) {
return Map.of();
}

return Arrays.stream(ENTRY_SEPARATOR_PATTERN.split(configValue))
.map(pair -> KEY_VALUE_SEPARATOR_PATTERN.split(pair, 2))
.filter(keyValue -> keyValue.length == 2)
.collect(Collectors.toMap(keyValue -> keyValue[0], keyValue -> keyValue[1]));
Map<String, String> result = new HashMap<>();
int start = 0;
int len = configValue.length();

// indexOf(char) is a JVM intrinsic (SIMD scan), avoiding Matcher allocation and regex engine overhead per call.
// strip() is a no-op when there is no surrounding whitespace, which is the common case for machine-generated values.
while (start < len) {
int end = configValue.indexOf(';', start);
if (end == -1) end = len;

int eq = configValue.indexOf('=', start);
if (eq != -1 && eq < end) {
String key = configValue.substring(start, eq).strip();
String value = configValue.substring(eq + 1, end).strip();
if (!key.isEmpty()) {
result.put(key, value);
}
}
start = end + 1;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.framework.config;

import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ConfigKeyUtilTest {

private static final String KEY = "key";
private static final String VALUE = "value";
private static final String KEY_1 = "key1";
private static final String KEY_2 = "key2";
private static final String VALUE_1 = "val1";
private static final String VALUE_2 = "val2";

@Test
public void toMapNullReturnsEmpty() {
assertTrue(ConfigKeyUtil.toMap(null).isEmpty());
}

@Test
public void toMapEmptyStringReturnsEmpty() {
assertTrue(ConfigKeyUtil.toMap("").isEmpty());
}

@Test
public void toMapSingleEntry() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=%s", KEY, VALUE));
assertEquals(1, result.size());
assertEquals(VALUE, result.get(KEY));
}

@Test
public void toMapMultipleEntries() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=%s;%s=%s;key3=val3", KEY_1, VALUE_1, KEY_2, VALUE_2));
assertEquals(3, result.size());
assertEquals(VALUE_1, result.get(KEY_1));
assertEquals(VALUE_2, result.get(KEY_2));
assertEquals("val3", result.get("key3"));
}

@Test
public void toMapWhitespaceAroundSeparators() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s = %s ; %s = %s", KEY_1, VALUE_1, KEY_2, VALUE_2));
assertEquals(2, result.size());
assertEquals(VALUE_1, result.get(KEY_1));
assertEquals(VALUE_2, result.get(KEY_2));
}

@Test
public void toMapTrailingSemicolon() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=%s;", KEY, VALUE));
assertEquals(1, result.size());
assertEquals(VALUE, result.get(KEY));
}

@Test
public void toMapValueContainsEquals() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=val=extra", KEY));
assertEquals(1, result.size());
assertEquals("val=extra", result.get(KEY));
}

@Test
public void toMapEmptyValueAllowed() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=", KEY));
assertEquals(1, result.size());
assertEquals("", result.get(KEY));
}

@Test
public void toMapEntryWithoutEqualsSkipped() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("noequals;%s=%s", KEY, VALUE));
assertEquals(1, result.size());
assertEquals(VALUE, result.get(KEY));
}

@Test
public void toMapEmptyKeySkipped() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("=%s;%s=val", VALUE, KEY));
assertEquals(1, result.size());
assertEquals("val", result.get(KEY));
}

@Test
public void toMapDuplicateKeyLastValueWins() {
Map<String, String> result = ConfigKeyUtil.toMap(String.format("%s=first;%s=second", KEY, KEY));
assertEquals(1, result.size());
assertEquals("second", result.get(KEY));
}
}