aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/objects
diff options
context:
space:
mode:
authorboubou_19 <miisterunknown@gmail.com>2021-08-16 20:32:53 +0200
committerboubou_19 <miisterunknown@gmail.com>2021-08-16 20:32:53 +0200
commit8f5dd5625e4632bbecda0771caad47bbbbd35d4c (patch)
tree15d544c783cd17606e6d71259eefc062ba4ea8e5 /src/main/java/gregtech/api/objects
parent99030ff940686562c7e4f133919fb1496b0575f4 (diff)
parentc543724aa11d696fa049855e7b330364c1000a17 (diff)
downloadGT5-Unofficial-8f5dd5625e4632bbecda0771caad47bbbbd35d4c.tar.gz
GT5-Unofficial-8f5dd5625e4632bbecda0771caad47bbbbd35d4c.tar.bz2
GT5-Unofficial-8f5dd5625e4632bbecda0771caad47bbbbd35d4c.zip
Merge remote-tracking branch 'upstream/experimental' into experimental
Diffstat (limited to 'src/main/java/gregtech/api/objects')
-rw-r--r--src/main/java/gregtech/api/objects/CollectorUtils.java29
-rw-r--r--src/main/java/gregtech/api/objects/iterators/MergedIterator.java30
2 files changed, 59 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);
+ }
+}
diff --git a/src/main/java/gregtech/api/objects/iterators/MergedIterator.java b/src/main/java/gregtech/api/objects/iterators/MergedIterator.java
new file mode 100644
index 0000000000..77fac8d22f
--- /dev/null
+++ b/src/main/java/gregtech/api/objects/iterators/MergedIterator.java
@@ -0,0 +1,30 @@
+package gregtech.api.objects.iterators;
+
+import java.util.Iterator;
+
+public class MergedIterator<T> implements Iterator<T> {
+ private final Iterator<T>[] inners;
+ private int current;
+
+ @SafeVarargs
+ public MergedIterator(Iterator<T>... iterators) {
+ inners = iterators;
+ current = 0;
+ }
+
+ public boolean hasNext() {
+ while (current < inners.length && !inners[current].hasNext()) {
+ current++;
+ }
+
+ return current < inners.length;
+ }
+
+ public T next() {
+ while (current < inners.length && !inners[current].hasNext()) {
+ current++;
+ }
+
+ return inners[current].next();
+ }
+}