Browse Source

Update FastMap-based ImmutableMap to guava 31 (now used by Mojang)

malte0811 3 years ago
parent
commit
a358e9af3d

+ 10 - 0
common/src/googleaccess/java/com/google/common/collect/FerriteCoreImmutableCollectionAccess.java

@@ -0,0 +1,10 @@
+package com.google.common.collect;
+
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Same as {@link FerriteCoreImmutableMapAccess}
+ */
+public abstract class FerriteCoreImmutableCollectionAccess<T> extends ImmutableCollection<T> {
+    public abstract boolean isPartialView();
+}

+ 3 - 0
common/src/googleaccess/java/com/google/common/collect/FerriteCoreImmutableMapAccess.java

@@ -17,4 +17,7 @@ public abstract class FerriteCoreImmutableMapAccess<K, V> extends ImmutableMap<K
 
     @Override
     public abstract ImmutableSet<K> createKeySet();
+
+    @Override
+    public abstract ImmutableCollection<V> createValues();
 }

+ 9 - 1
common/src/googleimpl/java/malte0811/ferritecore/fastmap/immutable/FastMapEntryEntrySet.java

@@ -3,6 +3,7 @@ package malte0811.ferritecore.fastmap.immutable;
 import com.google.common.collect.FerriteCoreEntrySetAccess;
 import com.google.common.collect.UnmodifiableIterator;
 import malte0811.ferritecore.ducks.FastMapStateHolder;
+import malte0811.ferritecore.fastmap.FastMap;
 import net.minecraft.world.level.block.state.properties.Property;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -19,7 +20,14 @@ public class FastMapEntryEntrySet extends FerriteCoreEntrySetAccess<Property<?>,
     @Override
     @NotNull
     public UnmodifiableIterator<Map.Entry<Property<?>, Comparable<?>>> iterator() {
-        return new FastMapEntryIterator(viewedState);
+        return new FastMapEntryIterator<>(viewedState) {
+            @Override
+            protected Map.Entry<Property<?>, Comparable<?>> getEntry(
+                    int propertyIndex, FastMap<?> map, int stateIndex
+            ) {
+                return map.getEntry(propertyIndex, stateIndex);
+            }
+        };
     }
 
     @Override

+ 6 - 0
common/src/googleimpl/java/malte0811/ferritecore/fastmap/immutable/FastMapEntryImmutableMap.java

@@ -1,6 +1,7 @@
 package malte0811.ferritecore.fastmap.immutable;
 
 import com.google.common.collect.FerriteCoreImmutableMapAccess;
+import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableSet;
 import malte0811.ferritecore.ducks.FastMapStateHolder;
 import net.minecraft.world.level.block.state.properties.Property;
@@ -42,4 +43,9 @@ public class FastMapEntryImmutableMap extends FerriteCoreImmutableMapAccess<Prop
     public ImmutableSet<Property<?>> createKeySet() {
         return viewedState.getStateMap().getPropertySet();
     }
+
+    @Override
+    public ImmutableCollection<Comparable<?>> createValues() {
+        return new FastMapValueSet(viewedState);
+    }
 }

+ 6 - 5
common/src/googleimpl/java/malte0811/ferritecore/fastmap/immutable/FastMapEntryIterator.java

@@ -2,11 +2,12 @@ package malte0811.ferritecore.fastmap.immutable;
 
 import com.google.common.collect.UnmodifiableIterator;
 import malte0811.ferritecore.ducks.FastMapStateHolder;
+import malte0811.ferritecore.fastmap.FastMap;
 import net.minecraft.world.level.block.state.properties.Property;
 
 import java.util.Map;
 
-public class FastMapEntryIterator extends UnmodifiableIterator<Map.Entry<Property<?>, Comparable<?>>> {
+public abstract class FastMapEntryIterator<T> extends UnmodifiableIterator<T> {
     private final FastMapStateHolder<?> viewedState;
     private int currentIndex = 0;
 
@@ -20,11 +21,11 @@ public class FastMapEntryIterator extends UnmodifiableIterator<Map.Entry<Propert
     }
 
     @Override
-    public Map.Entry<Property<?>, Comparable<?>> next() {
-        Map.Entry<Property<?>, Comparable<?>> next = viewedState.getStateMap().getEntry(
-                currentIndex, viewedState.getStateIndex()
-        );
+    public T next() {
+        T next = getEntry(currentIndex, viewedState.getStateMap(), viewedState.getStateIndex());
         ++currentIndex;
         return next;
     }
+
+    protected abstract T getEntry(int propertyIndex, FastMap<?> map, int stateIndex);
 }

+ 47 - 0
common/src/googleimpl/java/malte0811/ferritecore/fastmap/immutable/FastMapValueSet.java

@@ -0,0 +1,47 @@
+package malte0811.ferritecore.fastmap.immutable;
+
+import com.google.common.collect.FerriteCoreImmutableCollectionAccess;
+import com.google.common.collect.UnmodifiableIterator;
+import malte0811.ferritecore.ducks.FastMapStateHolder;
+import malte0811.ferritecore.fastmap.FastMap;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Objects;
+
+public class FastMapValueSet extends FerriteCoreImmutableCollectionAccess<Comparable<?>> {
+    private final FastMapStateHolder<?> viewedState;
+
+    public FastMapValueSet(FastMapStateHolder<?> viewedState) {
+        this.viewedState = viewedState;
+    }
+
+    @Override
+    public UnmodifiableIterator<Comparable<?>> iterator() {
+        return new FastMapEntryIterator<>(viewedState) {
+            @Override
+            protected Comparable<?> getEntry(int propertyIndex, FastMap<?> map, int stateIndex) {
+                return map.getKey(propertyIndex).getValue(stateIndex);
+            }
+        };
+    }
+
+    @Override
+    public int size() {
+        return viewedState.getStateMap().numProperties();
+    }
+
+    @Override
+    public boolean contains(@Nullable Object o) {
+        for (var entry : this) {
+            if (Objects.equals(entry, o)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public boolean isPartialView() {
+        return false;
+    }
+}

+ 1 - 0
common/src/main/java/malte0811/ferritecore/classloading/FastImmutableMapDefiner.java

@@ -40,6 +40,7 @@ public class FastImmutableMapDefiner {
             // Load these in the app classloader!
             defineInAppClassloader("com.google.common.collect.FerriteCoreEntrySetAccess");
             defineInAppClassloader("com.google.common.collect.FerriteCoreImmutableMapAccess");
+            defineInAppClassloader("com.google.common.collect.FerriteCoreImmutableCollectionAccess");
             // This lives in the transforming classloader, but must not be loaded before the previous classes are in
             // the app classloader!
             Class<?> map = Class.forName("malte0811.ferritecore.fastmap.immutable.FastMapEntryImmutableMap");

+ 1 - 1
common/src/main/java/malte0811/ferritecore/fastmap/FastMapKey.java

@@ -19,7 +19,7 @@ public abstract class FastMapKey<T extends Comparable<T>> {
      * @param mapIndex An index in the FastMap's value matrix
      * @return The value of this property in that index
      */
-    abstract T getValue(int mapIndex);
+    public abstract T getValue(int mapIndex);
 
     /**
      * @param mapIndex The original index in the FastMap's value matrix