Util: add functions for serialising YAML maps.

soundboard
Nekojimi 8 months ago
parent ee9c435b5d
commit 2bc9cac661
  1. 28
      src/main/java/moe/nekojimi/chords/Util.java

@ -1,6 +1,14 @@
package moe.nekojimi.chords; 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.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.Matcher;
import java.util.regex.Pattern; 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 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"); private static final Pattern SAMPLE_RATE_PATTERN = Pattern.compile("\\b([0-9]+)k(b(ps?))?\\b");
public static <V> Map<String, V> yamlMappingToMap(YamlMapping mapping, Function<YamlNode, V> fromYamlFunction)
{
Map<String, V> ret = new HashMap<>();
for (YamlNode key : mapping.keys())
{
ret.put(key.asScalar().value(), fromYamlFunction.apply(mapping.value(key)));
}
return ret;
}
public static <K, V> YamlMapping mapToMapping(Map<K, V> map, Function<V, YamlNode> toYamlFunction)
{
YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
for (Entry<K, V> e : map.entrySet())
{
builder = builder.add(e.getKey().toString(), toYamlFunction.apply(e.getValue()));
}
return builder.build();
}
public static String printSamples(ByteBuffer buf) public static String printSamples(ByteBuffer buf)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

Loading…
Cancel
Save