Skip to content

Commit

Permalink
Enhance BiList, BiMap, BiArrayList, BiHashMap
Browse files Browse the repository at this point in the history
Add TriList, TriMap, TriArrayList, TriHashMap
Add QuadConsumer
Default Features uses now BiList instead of List<Pair>
  • Loading branch information
FirstMegaGame4 committed Nov 10, 2023
1 parent 89f5b01 commit c19aa0e
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public BiArrayList(@NotNull BiList<E1, E2> c) {
super(c);
}

@Override
public boolean contains(E1 first, E2 second) {
return this.contains(new ImmutablePair<>(first, second));
}

@Override
public E1 getFirst(int index) {
return this.get(index).getLeft();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public BiHashMap(BiMap<? extends K, V1, V2> m) {
super(m);
}


@Override
public boolean containsValue(V1 firstValue, V2 secondValue) {
return this.containsValue(new ImmutablePair<>(firstValue, secondValue));
}

public V1 getFirstValue(K key) {
return this.getOrDefault(key, BiMap.emptyValue()).getLeft();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

public interface BiList<E1, E2> extends List<Pair<E1, E2>> {

static <E> boolean contains(BiList<E, E> biList, E first, E second) {
return biList.contains(first, second) || biList.contains(second, first);
}

static <E1, E2> Pair<E1, E2> emptyValue() {
return new ImmutablePair<>(null, null);
}

boolean contains(E1 first, E2 second);

E1 getFirst(int index);

E2 getSecond(int index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

public interface BiMap<K, V1, V2> extends Map<K, Pair<V1, V2>> {

static <K, V> boolean contains(BiMap<K, V, V> biMap, V firstValue, V secondValue) {
return biMap.containsValue(firstValue, secondValue) || biMap.containsValue(secondValue, firstValue);
}

static <V1, V2> Pair<V1, V2> emptyValue() {
return new ImmutablePair<>(null, null);
}

boolean containsValue(V1 firstValue, V2 secondValue);

V1 getFirstValue(K key);

V2 getSecondValue(K key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mmodding.mmodding_lib.library.utils;

public interface QuadConsumer<K, V, S, T> {

void accept(K k, V v, S s, T t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.mmodding.mmodding_lib.library.utils;

import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.logging.log4j.util.TriConsumer;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.function.Consumer;

public class TriArrayList<E1, E2, E3> extends ArrayList<Triple<E1, E2, E3>> implements TriList<E1, E2, E3> {

public TriArrayList(int initialCapacity) {
super(initialCapacity);
}

public TriArrayList() {
super();
}

public TriArrayList(@NotNull TriList<E1, E2, E3> c) {
super(c);
}

@Override
public boolean contains(E1 first, E2 second, E3 third) {
return this.contains(new ImmutableTriple<>(first, second, third));
}

@Override
public E1 getFirst(int index) {
return this.get(index).getLeft();
}

@Override
public E2 getSecond(int index) {
return this.get(index).getMiddle();
}

@Override
public E3 getThird(int index) {
return this.get(index).getRight();
}

@Override
public boolean add(E1 first, E2 second, E3 third) {
return this.add(new ImmutableTriple<>(first, second, third));
}

@Override
public boolean remove(E1 first, E2 second, E3 third) {
return this.remove(new ImmutableTriple<>(first, second, third));
}

@Override
public Triple<E1, E2, E3> set(int index, E1 first, E2 second, E3 third) {
return this.set(index, new ImmutableTriple<>(first, second, third));
}

@Override
public void forEachFirst(Consumer<? super E1> action) {
this.forEach(value -> action.accept(value.getLeft()));
}

@Override
public void forEachSecond(Consumer<? super E2> action) {
this.forEach(value -> action.accept(value.getMiddle()));
}

@Override
public void forEachThird(Consumer<? super E3> action) {
this.forEach(value -> action.accept(value.getRight()));
}

@Override
public void forEach(TriConsumer<? super E1, ? super E2, ? super E3> action) {
this.forEach(value -> action.accept(value.getLeft(), value.getMiddle(), value.getRight()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mmodding.mmodding_lib.library.utils;

import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;

import java.util.HashMap;
import java.util.function.BiConsumer;

public class TriHashMap<K, V1, V2, V3> extends HashMap<K, Triple<V1, V2, V3>> implements TriMap<K, V1, V2, V3> {

public TriHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}

public TriHashMap(int initialCapacity) {
super(initialCapacity);
}

public TriHashMap() {
super();
}

public TriHashMap(TriMap<? extends K, V1, V2, V3> m) {
super(m);
}


@Override
public boolean containsValue(V1 firstValue, V2 secondValue, V3 thirdValue) {
return this.containsValue(new ImmutableTriple<>(firstValue, secondValue, thirdValue));
}

public V1 getFirstValue(K key) {
return this.getOrDefault(key, TriMap.emptyValue()).getLeft();
}

public V2 getSecondValue(K key) {
return this.getOrDefault(key, TriMap.emptyValue()).getMiddle();
}

public V3 getThirdValue(K key) {
return this.getOrDefault(key, TriMap.emptyValue()).getRight();
}

public Triple<V1, V2, V3> put(K key, V1 firstValue, V2 secondValue, V3 thirdValue) {
return this.put(key, new ImmutableTriple<>(firstValue, secondValue, thirdValue));
}

public void forEachFirst(BiConsumer<? super K, ? super V1> action) {
this.forEach((key, value) -> action.accept(key, value.getLeft()));
}

public void forEachSecond(BiConsumer<? super K, ? super V2> action) {
this.forEach((key, value) -> action.accept(key, value.getMiddle()));
}

public void forEachThird(BiConsumer<? super K, ? super V3> action) {
this.forEach((key, value) -> action.accept(key, value.getRight()));
}

public void forEach(QuadConsumer<? super K, ? super V1, ? super V2, ? super V3> action) {
this.forEach((key, value) -> action.accept(key, value.getLeft(), value.getMiddle(), value.getRight()));
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/mmodding/mmodding_lib/library/utils/TriList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mmodding.mmodding_lib.library.utils;

import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.logging.log4j.util.TriConsumer;

import java.util.List;
import java.util.function.Consumer;

public interface TriList<E1, E2, E3> extends List<Triple<E1, E2, E3>> {

static <E> boolean contains(TriList<E, E, E> triList, E first, E second, E third) {
return (triList.contains(first, second, third) || triList.contains(first, third, second))
|| (triList.contains(second, first, third) || triList.contains(second, third, first))
|| (triList.contains(third, first, second) || triList.contains(third, second, first));
}

static <E1, E2, E3> Triple<E1, E2, E3> emptyValue() {
return new ImmutableTriple<>(null, null, null);
}

boolean contains(E1 first, E2 second, E3 third);

E1 getFirst(int index);

E2 getSecond(int index);

E3 getThird(int index);

boolean add(E1 first, E2 second, E3 third);

boolean remove(E1 first, E2 second, E3 third);

Triple<E1, E2, E3> set(int index, E1 first, E2 second, E3 third);

void forEachFirst(Consumer<? super E1> action);

void forEachSecond(Consumer<? super E2> action);

void forEachThird(Consumer<? super E3> action);

void forEach(TriConsumer<? super E1, ? super E2, ? super E3> action);
}
38 changes: 38 additions & 0 deletions src/main/java/com/mmodding/mmodding_lib/library/utils/TriMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mmodding.mmodding_lib.library.utils;

import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Triple;

import java.util.Map;
import java.util.function.BiConsumer;

public interface TriMap<K, V1, V2, V3> extends Map<K, Triple<V1, V2, V3>> {

static <K, V> boolean contains(TriMap<K, V, V, V> triMap, V firstValue, V secondValue, V thirdValue) {
return (triMap.containsValue(firstValue, secondValue, thirdValue) || triMap.containsValue(firstValue, thirdValue, secondValue))
|| (triMap.containsValue(secondValue, firstValue, thirdValue) || triMap.containsValue(secondValue, thirdValue, firstValue))
|| (triMap.containsValue(thirdValue, firstValue, secondValue) || triMap.containsValue(thirdValue, secondValue, firstValue));
}

static <V1, V2, V3> Triple<V1, V2, V3> emptyValue() {
return new ImmutableTriple<>(null, null, null);
}

boolean containsValue(V1 firstValue, V2 secondValue, V3 thirdValue);

V1 getFirstValue(K key);

V2 getSecondValue(K key);

V3 getThirdValue(K key);

Triple<V1, V2, V3> put(K key, V1 firstValue, V2 secondValue, V3 thirdValue);

void forEachFirst(BiConsumer<? super K, ? super V1> action);

void forEachSecond(BiConsumer<? super K, ? super V2> action);

void forEachThird(BiConsumer<? super K, ? super V3> action);

void forEach(QuadConsumer<? super K, ? super V1, ? super V2, ? super V3> action);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.mmodding.mmodding_lib.library.worldgen.features.defaults;

import com.mmodding.mmodding_lib.library.utils.BiArrayList;
import com.mmodding.mmodding_lib.library.utils.BiList;
import com.mmodding.mmodding_lib.library.utils.IdentifierUtils;
import com.mmodding.mmodding_lib.library.worldgen.MModdingFeatures;
import com.mmodding.mmodding_lib.library.worldgen.features.differeds.DifferedDripstoneClusterFeature;
import net.minecraft.block.Block;
import net.minecraft.util.Holder;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.floatprovider.FloatProvider;
import net.minecraft.util.math.intprovider.IntProvider;
import net.minecraft.util.registry.Registry;
Expand All @@ -31,7 +32,7 @@ public class CustomDripstoneClusterFeature implements CustomFeature, FeatureRegi

private final AtomicBoolean registered = new AtomicBoolean();
private final AtomicReference<Identifier> identifier = new AtomicReference<>();
private final List<Pair<PlacedFeature, String>> additionalPlacedFeatures = new ArrayList<>();
private final BiList<PlacedFeature, String> additionalPlacedFeatures = new BiArrayList<>();

private final Block pointedDripstoneBlock;
private final Block dripstoneBlock;
Expand Down Expand Up @@ -106,12 +107,12 @@ public PlacedFeature getDefaultPlacedFeature() {
}

public CustomDripstoneClusterFeature addPlacedFeature(IntProvider countRange, String idExt) {
this.additionalPlacedFeatures.add(new Pair<>(this.createPlacedFeature(countRange), idExt));
this.additionalPlacedFeatures.add(this.createPlacedFeature(countRange), idExt);
return this;
}

@Override
public List<Pair<PlacedFeature, String>> getAdditionalPlacedFeatures() {
public BiList<PlacedFeature, String> getAdditionalPlacedFeatures() {
return this.additionalPlacedFeatures;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mmodding.mmodding_lib.library.worldgen.features.defaults;

import net.minecraft.util.Pair;
import com.mmodding.mmodding_lib.library.utils.BiList;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.PlacedFeature;
Expand All @@ -15,5 +15,5 @@ public interface CustomFeature {

PlacedFeature getDefaultPlacedFeature();

List<Pair<PlacedFeature, String>> getAdditionalPlacedFeatures();
BiList<PlacedFeature, String> getAdditionalPlacedFeatures();
}
Loading

0 comments on commit c19aa0e

Please sign in to comment.