From d25781f36c302a487de71c185271bdd1656845b7 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:50:03 -0400 Subject: [PATCH] Implement more methods on FakeStateMap --- .../modernfix/blockstate/FakeStateMap.java | 89 ++++++------------- 1 file changed, 26 insertions(+), 63 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/blockstate/FakeStateMap.java b/common/src/main/java/org/embeddedt/modernfix/blockstate/FakeStateMap.java index 7cc8f22c..a31aac0b 100644 --- a/common/src/main/java/org/embeddedt/modernfix/blockstate/FakeStateMap.java +++ b/common/src/main/java/org/embeddedt/modernfix/blockstate/FakeStateMap.java @@ -1,5 +1,6 @@ package org.embeddedt.modernfix.blockstate; +import com.google.common.collect.Iterators; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.world.level.block.state.properties.Property; import org.jetbrains.annotations.NotNull; @@ -96,42 +97,51 @@ public class FakeStateMap implements Map, Comparable>, S> this.usedSlots = 0; } + private List asList(T... array) { + var list = Arrays.asList(array); + if(usedSlots < array.length) { + list = list.subList(0, usedSlots); + } + return list; + } + @NotNull @Override public Set, Comparable>> keySet() { - throw new UnsupportedOperationException(); + return new AbstractSet<>() { + @Override + public Iterator, Comparable>> iterator() { + return keys.length == usedSlots ? Iterators.forArray(keys) : asList(keys).iterator(); + } + + @Override + public int size() { + return usedSlots; + } + }; } @NotNull @Override public Collection values() { - throw new UnsupportedOperationException(); + return (Collection)asList(values); } @NotNull @Override public Set, Comparable>, S>> entrySet() { - return new Set, Comparable>, S>>() { + return new AbstractSet<>() { @Override public int size() { return usedSlots; } - @Override - public boolean isEmpty() { - return FakeStateMap.this.isEmpty(); - } - - @Override - public boolean contains(Object o) { - throw new UnsupportedOperationException(); - } - @NotNull @Override public Iterator, Comparable>, S>> iterator() { - return new Iterator, Comparable>, S>>() { + return new Iterator<>() { int currentIdx = 0; + @Override public boolean hasNext() { return currentIdx < usedSlots; @@ -139,61 +149,14 @@ public class FakeStateMap implements Map, Comparable>, S> @Override public Entry, Comparable>, S> next() { - if(currentIdx >= usedSlots) + if (currentIdx >= usedSlots) throw new IndexOutOfBoundsException(); - Entry, Comparable>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S)values[currentIdx]); + Entry, Comparable>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S) values[currentIdx]); currentIdx++; return entry; } }; } - - @NotNull - @Override - public Object[] toArray() { - throw new UnsupportedOperationException(); - } - - @NotNull - @Override - public T[] toArray(@NotNull T[] ts) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean add(Entry, Comparable>, S> mapSEntry) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean containsAll(@NotNull Collection collection) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean addAll(@NotNull Collection, Comparable>, S>> collection) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(@NotNull Collection collection) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(@NotNull Collection collection) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } }; } }