diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-08-16 12:31:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-16 12:31:43 +0200 |
commit | 2355a9c77781463ec98c8ba2144b2a5ee72b34e6 (patch) | |
tree | adeccd9790d7d9ee000493efc4cf94237e36b02e /src/main/java/gregtech/api/objects | |
parent | 01f2951352ea3ab9b32e3d9e2343d57cf22c73f7 (diff) | |
parent | a0e70820f374b9312b4a1b510f361958e37bd33a (diff) | |
download | GT5-Unofficial-2355a9c77781463ec98c8ba2144b2a5ee72b34e6.tar.gz GT5-Unofficial-2355a9c77781463ec98c8ba2144b2a5ee72b34e6.tar.bz2 GT5-Unofficial-2355a9c77781463ec98c8ba2144b2a5ee72b34e6.zip |
Merge pull request #641 from GTNewHorizons/no-concurrency
Removed useless concurrency for some maps
Diffstat (limited to 'src/main/java/gregtech/api/objects')
-rw-r--r-- | src/main/java/gregtech/api/objects/CollectorUtils.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/objects/CollectorUtils.java b/src/main/java/gregtech/api/objects/CollectorUtils.java new file mode 100644 index 0000000000..3f89ad0773 --- /dev/null +++ b/src/main/java/gregtech/api/objects/CollectorUtils.java @@ -0,0 +1,29 @@ +package gregtech.api.objects; + +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +public class CollectorUtils { + /** + * Returns a merge function, suitable for use in + * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or + * {@link Collectors#toMap(Function, Function, BinaryOperator) toMap()}, which always + * throws {@code IllegalStateException}. This can be used to enforce the + * assumption that the elements being collected are distinct. + * + * @param <T> the type of input arguments to the merge function + * @return a merge function which always throw {@code IllegalStateException} + */ + public static <T> BinaryOperator<T> throwingMerger() { + return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }; + } + + public static <K, V, E extends Map.Entry<K, V>, M extends Map<K, V>> Collector<E, ?, M> entriesToMap(Supplier<M> mapSupplier) { + return Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, CollectorUtils.throwingMerger(), mapSupplier); + } +} |