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