mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-24 06:06:45 +01:00
54 lines
1.7 KiB
Java
54 lines
1.7 KiB
Java
package com.gmail.nossr50.skills.child;
|
|
|
|
import java.util.Collections;
|
|
import java.util.EnumSet;
|
|
import java.util.HashMap;
|
|
import java.util.Set;
|
|
|
|
import com.gmail.nossr50.datatypes.skills.PrimarySkill;
|
|
|
|
public class FamilyTree {
|
|
private static HashMap<PrimarySkill, Set<PrimarySkill>> tree = new HashMap<PrimarySkill, Set<PrimarySkill>>();
|
|
|
|
public static Set<PrimarySkill> getParents(PrimarySkill childSkill) {
|
|
enforceChildSkill(childSkill);
|
|
|
|
// We do not check if we have the child skill in question, as not having it would mean we did something wrong, and an NPE is desired.
|
|
return tree.get(childSkill);
|
|
}
|
|
|
|
protected static void registerParent(PrimarySkill childSkill, PrimarySkill parentSkill) {
|
|
enforceChildSkill(childSkill);
|
|
enforceNotChildSkill(parentSkill);
|
|
|
|
if (!tree.containsKey(childSkill)) {
|
|
tree.put(childSkill, EnumSet.noneOf(PrimarySkill.class));
|
|
}
|
|
|
|
tree.get(childSkill).add(parentSkill);
|
|
}
|
|
|
|
protected static void closeRegistration() {
|
|
for (PrimarySkill childSkill : tree.keySet()) {
|
|
Set<PrimarySkill> immutableSet = Collections.unmodifiableSet(tree.get(childSkill));
|
|
tree.put(childSkill, immutableSet);
|
|
}
|
|
}
|
|
|
|
protected static void clearRegistrations() {
|
|
tree.clear();
|
|
}
|
|
|
|
protected static void enforceChildSkill(PrimarySkill skill) {
|
|
if (!skill.isChildSkill()) {
|
|
throw new IllegalArgumentException(skill.name() + " is not a child skill!");
|
|
}
|
|
}
|
|
|
|
protected static void enforceNotChildSkill(PrimarySkill skill) {
|
|
if (skill.isChildSkill()) {
|
|
throw new IllegalArgumentException(skill.name() + " is a child skill!");
|
|
}
|
|
}
|
|
}
|