shz.spring.yaml.YamlHelp Maven / Gradle / Ivy
The newest version!
package shz.spring.yaml;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import shz.core.NullHelp;
import shz.core.msg.ClientFailureMsg;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@SuppressWarnings("unchecked")
public final class YamlHelp {
private YamlHelp() {
throw new IllegalStateException();
}
public static Map load(String yaml) {
if (NullHelp.isBlank(yaml)) return Collections.emptyMap();
return new Yaml().load(yaml);
}
public static Map load(InputStream is) {
if (is == null) return Collections.emptyMap();
return new Yaml().load(is);
}
public static Map load(Reader reader) {
if (reader == null) return Collections.emptyMap();
return new Yaml().load(reader);
}
public static T load(Class cls, String yaml) {
if (NullHelp.isBlank(yaml)) return NullHelp.empty(cls);
return new Yaml().loadAs(yaml, cls);
}
public static T load(Class cls, InputStream is) {
if (is == null) return NullHelp.empty(cls);
return new Yaml().loadAs(is, cls);
}
public static T load(Class cls, Reader reader) {
if (reader == null) return NullHelp.empty(cls);
return new Yaml().loadAs(reader, cls);
}
public static String dump(Object data) {
StringWriter sw = new StringWriter();
dump(data, sw);
return sw.toString();
}
public static void dump(Object data, Writer writer) {
if (NullHelp.isBlank(data)) return;
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
yaml.dump(data, writer);
}
public static T get(Class cls, Map map, String key) {
if (NullHelp.isEmpty(map)) return NullHelp.empty(cls);
if (NullHelp.isBlank(key)) return load(cls, dump(map));
ClientFailureMsg.requireNon(key.charAt(key.length() - 1) == '.', "yaml key错误");
return get0(cls, map, key);
}
private static T get0(Class cls, Map map, String key) {
if (NullHelp.isEmpty(map)) return NullHelp.empty(cls);
int idx = key.indexOf('.');
if (idx == -1) return load(cls, dump(map.get(key)));
String prefix = key.substring(0, idx);
return get0(cls, (Map) map.get(prefix), key.substring(idx + 1));
}
public static Map getMap(Map map, String key) {
return get(Map.class, map, key);
}
public static String getString(Map map, String key) {
return get(String.class, map, key);
}
public static Integer getInteger(Map map, String key) {
return get(Integer.class, map, key);
}
public static Long getLong(Map map, String key) {
return get(Long.class, map, key);
}
public static Boolean getBoolean(Map map, String key) {
return get(Boolean.class, map, key);
}
public static void set(Map map, String key, Object value) {
if (map == null) return;
ClientFailureMsg.requireNon(NullHelp.isEmpty(key) || key.charAt(key.length() - 1) == '.', "yaml key错误");
set0((Map) map, key, value);
}
private static void set0(Map map, String key, Object value) {
int idx = key.indexOf('.');
if (idx == -1) map.put(key, value);
else {
String prefix = key.substring(0, idx);
Map data = (Map) map.get(prefix);
if (data == null) {
data = new LinkedHashMap<>();
map.put(prefix, data);
}
set0(data, key.substring(idx + 1), value);
}
}
}