-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypedObject, MixedList And MixedArrayList
- Loading branch information
1 parent
ccaa2b7
commit 56259e7
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/mmodding/mmodding_lib/library/utils/MixedArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.mmodding.mmodding_lib.library.utils; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.function.BiConsumer; | ||
|
||
@SuppressWarnings("unchecked") | ||
public class MixedArrayList extends ArrayList<TypedObject<?>> implements MixedList { | ||
|
||
public MixedArrayList(int initialCapacity) { | ||
super(initialCapacity); | ||
} | ||
|
||
public MixedArrayList() { | ||
super(); | ||
} | ||
|
||
public MixedArrayList(@NotNull MixedList c) { | ||
super(c); | ||
} | ||
|
||
@Override | ||
public <E> boolean contains(Class<E> type, E e) { | ||
return super.contains(TypedObject.of(type, e)); | ||
} | ||
|
||
@Override | ||
public <E> E get(Class<E> type, int index) { | ||
TypedObject<?> typed = super.get(index); | ||
if (type.equals(typed.getType())) { | ||
return (E) typed.getValue(); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Given type does not match the targeted type!"); | ||
} | ||
} | ||
|
||
@Override | ||
public <E> boolean add(Class<E> type, E e) { | ||
return super.add(TypedObject.of(type, e)); | ||
} | ||
|
||
@Override | ||
public <E> boolean remove(Class<E> type, E e) { | ||
return super.remove(TypedObject.of(type, e)); | ||
} | ||
|
||
@Override | ||
public <E> E set(int index, Class<E> type, E element) { | ||
return (E) super.set(index, TypedObject.of(type, element)).getValue(); | ||
} | ||
|
||
@Override | ||
public <E> void forEach(BiConsumer<? super Class<E>, ? super E> action) { | ||
this.forEach(value -> action.accept((Class<E>) value.getType(), (E) value.getValue())); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/mmodding/mmodding_lib/library/utils/MixedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.mmodding.mmodding_lib.library.utils; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
|
||
public interface MixedList extends List<TypedObject<?>> { | ||
|
||
static <E> TypedObject<E> emptyValue(Class<E> type) { | ||
return TypedObject.of(type, null); | ||
} | ||
|
||
<E> boolean contains(Class<E> type, E e); | ||
|
||
<E> E get(Class<E> type, int index); | ||
|
||
<E> boolean add(Class<E> type, E e); | ||
|
||
<E> boolean remove(Class<E> type, E e); | ||
|
||
<E> E set(int index, Class<E> type, E element); | ||
|
||
<E> void forEach(BiConsumer<? super Class<E>, ? super E> action); | ||
|
||
@SuppressWarnings("unchecked") | ||
static MixedList generateMixedListFromTrustedArray(Object... input) { | ||
assert input.getClass() == Object[].class; | ||
|
||
for (Object o : input) { | ||
Objects.requireNonNull(o); | ||
} | ||
|
||
MixedList mixedList = new MixedArrayList(); | ||
|
||
for (int i = 0; i < input.length / 2; i++) { | ||
assert input[i * 2] instanceof Class && input[i * 2 + 1].getClass() == input[i * 2]; | ||
mixedList.add((Class<Object>) input[i * 2], input[i * 2 + 1]); | ||
} | ||
|
||
return mixedList; | ||
} | ||
|
||
static <E0> MixedList of(Class<E0> t0, E0 e0) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0); | ||
} | ||
|
||
static <E0, E1> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1); | ||
} | ||
|
||
static <E0, E1, E2> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2); | ||
} | ||
|
||
static <E0, E1, E2, E3> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4, E5> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4, Class<E5> t5, E5 e5) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4, t5, e5); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4, E5, E6> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4, Class<E5> t5, E5 e5, Class<E6> t6, E6 e6) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4, t5, e5, t6, e6); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4, E5, E6, E7> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4, Class<E5> t5, E5 e5, Class<E6> t6, E6 e6, Class<E7> t7, E7 e7) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4, t5, e5, t6, e6, t7, e7); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4, E5, E6, E7, E8> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4, Class<E5> t5, E5 e5, Class<E6> t6, E6 e6, Class<E7> t7, E7 e7, Class<E8> t8, E8 e8) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4, t5, e5, t6, e6, t7, e7, t8, e8); | ||
} | ||
|
||
static <E0, E1, E2, E3, E4, E5, E6, E7, E8, E9> MixedList of(Class<E0> t0, E0 e0, Class<E1> t1, E1 e1, Class<E2> t2, E2 e2, Class<E3> t3, E3 e3, Class<E4> t4, E4 e4, Class<E5> t5, E5 e5, Class<E6> t6, E6 e6, Class<E7> t7, E7 e7, Class<E8> t8, E8 e8, Class<E9> t9, E9 e9) { | ||
return MixedList.generateMixedListFromTrustedArray(t0, e0, t1, e1, t2, e2, t3, e3, t4, e4, t5, e5, t6, e6, t7, e7, t8, e8, t9, e9); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/mmodding/mmodding_lib/library/utils/TypedObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mmodding.mmodding_lib.library.utils; | ||
|
||
public class TypedObject<T> { | ||
|
||
private final Class<T> type; | ||
private final T value; | ||
|
||
private TypedObject(Class<T> type, T value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
public static <T> TypedObject<T> of(Class<T> type, T value) { | ||
return new TypedObject<>(type, value); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof TypedObject<?> typed) { | ||
return this.getType() == typed.getType() && this.getValue() == typed.getValue(); | ||
} | ||
else { | ||
return super.equals(obj); | ||
} | ||
} | ||
|
||
public Class<T> getType() { | ||
return this.type; | ||
} | ||
|
||
public T getValue() { | ||
return this.value; | ||
} | ||
} |