Class KeyValueCodec
java.lang.Object
cloud.opencode.base.string.codec.KeyValueCodec
Codec for encoding/decoding
Map<String,String> to/from key-value string format.
用于将 Map<String,String> 编码/解码为键值字符串格式的编解码器。
Default format: key1=value1;key2=value2. Separators are configurable.
默认格式:key1=value1;key2=value2。分隔符可配置。
Features | 主要功能:
- Encode Map to key=value;key=value format - 将Map编码为键值字符串格式
- Decode key=value string back to Map - 将键值字符串解码为Map
- Configurable entry and key-value separators - 可配置条目和键值分隔符
- Preserves insertion order - 保持插入顺序
Usage Examples | 使用示例:
Map<String, String> map = Map.of("host", "localhost", "port", "8080");
String encoded = KeyValueCodec.encode(map);
// "host=localhost;port=8080"
Map<String, String> decoded = KeyValueCodec.decode(encoded);
// {host=localhost, port=8080}
// Custom separators / 自定义分隔符
String custom = KeyValueCodec.encode(map, "&", ":");
// "host:localhost&port:8080"
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (returns null/empty for null input) - 空值安全: 是
- Since:
- JDK 25, opencode-base-string V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
-
Method Details
-
encode
-
encode
public static String encode(Map<String, String> map, String entrySeparator, String keyValueSeparator) Encode a map with configurable separators. 使用可配置的分隔符将映射编码。- Parameters:
map- the map to encode / 要编码的映射entrySeparator- separator between entries (e.g.";") / 条目之间的分隔符keyValueSeparator- separator between key and value (e.g."=") / 键值之间的分隔符- Returns:
- the encoded string, or
nullif the map is null or empty / 编码后的字符串,如果映射为 null 或空则返回 null - Throws:
NullPointerException- if entrySeparator or keyValueSeparator is null / 如果分隔符为 null
-
decode
Decode a string in the defaultkey=value;key=valueformat to a map. 将默认key=value;key=value格式的字符串解码为映射。Returns an unmodifiable map preserving insertion order. Returns an empty map if the input is null or blank.
返回保持插入顺序的不可修改映射。如果输入为 null 或空白则返回空映射。
- Parameters:
encoded- the encoded string / 编码后的字符串- Returns:
- decoded map (unmodifiable) / 解码后的映射(不可修改)
-
decode
public static Map<String,String> decode(String encoded, String entrySeparator, String keyValueSeparator) Decode a string with configurable separators to a map. 使用可配置的分隔符将字符串解码为映射。Returns an unmodifiable map preserving insertion order. Returns an empty map if the input is null or blank. Entries without a key-value separator are silently skipped.
返回保持插入顺序的不可修改映射。如果输入为 null 或空白则返回空映射。 没有键值分隔符的条目将被静默跳过。
- Parameters:
encoded- the encoded string / 编码后的字符串entrySeparator- separator between entries / 条目之间的分隔符keyValueSeparator- separator between key and value / 键值之间的分隔符- Returns:
- decoded map (unmodifiable) / 解码后的映射(不可修改)
- Throws:
NullPointerException- if entrySeparator or keyValueSeparator is null / 如果分隔符为 null
-