diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedArrayList.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedArrayList.java index 61942bd..7bfa07f 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedArrayList.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedArrayList.java @@ -26,7 +26,7 @@ public boolean contains(Class type, E e) { } @Override - public E get(Class type, int index) { + public E get(int index, Class type) { TypedObject typed = super.get(index); if (type.equals(typed.getType())) { return (E) typed.getValue(); diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedHashMap.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedHashMap.java new file mode 100644 index 0000000..a7f9c35 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedHashMap.java @@ -0,0 +1,52 @@ +package com.mmodding.mmodding_lib.library.utils; + +import org.apache.logging.log4j.util.TriConsumer; + +import java.util.HashMap; +import java.util.Objects; + +@SuppressWarnings("unchecked") +public class MixedHashMap extends HashMap> implements MixedMap { + + public MixedHashMap(int initialCapacity, float loadFactor) { + super(initialCapacity, loadFactor); + } + + public MixedHashMap(int initialCapacity) { + super(initialCapacity); + } + + public MixedHashMap() { + super(); + } + + public MixedHashMap(MixedMap m) { + super(m); + } + + @Override + public boolean containsValue(Class type, V value) { + return super.containsValue(TypedObject.of(type, value)); + } + + @Override + public V get(K key, Class type) { + TypedObject typed = super.get(key); + if (type.equals(typed.getType())) { + return (V) typed.getValue(); + } + else { + throw new IllegalArgumentException("Given type does not match the targeted type!"); + } + } + + @Override + public V put(K key, Class type, V value) { + return (V) Objects.requireNonNull(super.put(key, TypedObject.of(type, value))).getValue(); + } + + @Override + public void forEach(TriConsumer, ? super V> action) { + this.forEach((key, value) -> action.accept(key, (Class) value.getType(), (V) value.getValue())); + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedList.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedList.java index 6bce20b..5c0ba23 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedList.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedList.java @@ -12,7 +12,7 @@ static TypedObject emptyValue(Class type) { boolean contains(Class type, E e); - E get(Class type, int index); + E get(int index, Class type); boolean add(Class type, E e); diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedMap.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedMap.java new file mode 100644 index 0000000..00bcdf8 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/MixedMap.java @@ -0,0 +1,20 @@ +package com.mmodding.mmodding_lib.library.utils; + +import org.apache.logging.log4j.util.TriConsumer; + +import java.util.Map; + +public interface MixedMap extends Map> { + + static TypedObject emptyValue(Class type) { + return TypedObject.of(type, null); + } + + boolean containsValue(Class type, V value); + + V get(K key, Class type); + + V put(K key, Class type, V value); + + void forEach(TriConsumer, ? super V> action); +}