-
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.
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/mmodding/mmodding_lib/library/utils/MixedHashMap.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,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())); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/mmodding/mmodding_lib/library/utils/MixedMap.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,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); | ||
} |