Skip to content

Commit

Permalink
Add MixedMap And MixedHashMap
Browse files Browse the repository at this point in the history
Change MixedList And MixedArrayList
  • Loading branch information
FirstMegaGame4 committed Nov 23, 2023
1 parent 56259e7 commit e9ccb72
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public <E> boolean contains(Class<E> type, E e) {
}

@Override
public <E> E get(Class<E> type, int index) {
public <E> E get(int index, Class<E> type) {
TypedObject<?> typed = super.get(index);
if (type.equals(typed.getType())) {
return (E) typed.getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<K> extends HashMap<K, TypedObject<?>> implements MixedMap<K> {

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

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

public MixedHashMap() {
super();
}

public MixedHashMap(MixedMap<? extends K> m) {
super(m);
}

@Override
public <V> boolean containsValue(Class<V> type, V value) {
return super.containsValue(TypedObject.of(type, value));
}

@Override
public <V> V get(K key, Class<V> 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> V put(K key, Class<V> type, V value) {
return (V) Objects.requireNonNull(super.put(key, TypedObject.of(type, value))).getValue();
}

@Override
public <V> void forEach(TriConsumer<? super K, ? super Class<V>, ? super V> action) {
this.forEach((key, value) -> action.accept(key, (Class<V>) value.getType(), (V) value.getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static <E> TypedObject<E> emptyValue(Class<E> type) {

<E> boolean contains(Class<E> type, E e);

<E> E get(Class<E> type, int index);
<E> E get(int index, Class<E> type);

<E> boolean add(Class<E> type, E e);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<K> extends Map<K, TypedObject<?>> {

static <V> TypedObject<V> emptyValue(Class<V> type) {
return TypedObject.of(type, null);
}

<V> boolean containsValue(Class<V> type, V value);

<V> V get(K key, Class<V> type);

<V> V put(K key, Class<V> type, V value);

<V> void forEach(TriConsumer<? super K, ? super Class<V>, ? super V> action);
}

0 comments on commit e9ccb72

Please sign in to comment.