From 2bc9cac661136f8f30ae08b97398871b6c56d913 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Sun, 21 Apr 2024 16:49:21 +0100 Subject: [PATCH] Util: add functions for serialising YAML maps. --- src/main/java/moe/nekojimi/chords/Util.java | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/moe/nekojimi/chords/Util.java b/src/main/java/moe/nekojimi/chords/Util.java index 9966140..4b17ddf 100644 --- a/src/main/java/moe/nekojimi/chords/Util.java +++ b/src/main/java/moe/nekojimi/chords/Util.java @@ -1,6 +1,14 @@ package moe.nekojimi.chords; +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlMapping; +import com.amihaiemil.eoyaml.YamlMappingBuilder; +import com.amihaiemil.eoyaml.YamlNode; import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -31,6 +39,26 @@ public class Util private static final Pattern SIZE_PATTERN = Pattern.compile("\\b([0-9]+\\.?[0-9]*)([kkMmGg])i?[bB]\\b"); private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b"); + public static Map yamlMappingToMap(YamlMapping mapping, Function fromYamlFunction) + { + Map ret = new HashMap<>(); + for (YamlNode key : mapping.keys()) + { + ret.put(key.asScalar().value(), fromYamlFunction.apply(mapping.value(key))); + } + return ret; + } + + public static YamlMapping mapToMapping(Map map, Function toYamlFunction) + { + YamlMappingBuilder builder = Yaml.createYamlMappingBuilder(); + for (Entry e : map.entrySet()) + { + builder = builder.add(e.getKey().toString(), toYamlFunction.apply(e.getValue())); + } + return builder.build(); + } + public static String printSamples(ByteBuffer buf) { StringBuilder sb = new StringBuilder();