Use generic key mapping function

This commit is contained in:
Astrash 2022-11-25 14:10:07 +11:00
parent 8670c4cdf3
commit 4fdef98bd9
2 changed files with 12 additions and 9 deletions

View File

@ -17,6 +17,7 @@ public class DefinedBiomeColorMappingTemplate implements ObjectTemplate<ColorMap
@Override @Override
public ColorMapping<Biome> get() { public ColorMapping<Biome> get() {
return () -> MapUtil.convertKeysToInt(map); var map = MapUtil.mapKeys(this.map, Integer::decode);
return () -> map;
} }
} }

View File

@ -2,6 +2,7 @@ package com.dfsek.terra.addons.image.util;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -10,13 +11,14 @@ public class MapUtil {
private MapUtil() {} private MapUtil() {}
/** /**
* Utility method for converting config keys into integers, * Utility method for applying transformations on a map's keys.
* used because Tectonic does not accept non string keys.
*/ */
public static <T> Map<Integer, T> convertKeysToInt(Map<String, T> map) { public static <O, N, T> Map<N, T> mapKeys(Map<O, T> map, Function<O, N> mappingFunction) {
return map.entrySet().stream() return map
.entrySet()
.stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
e -> Integer.decode(e.getKey()), e -> mappingFunction.apply(e.getKey()),
Entry::getValue Entry::getValue
)); ));
} }