mcMMO/src/main/java/com/gmail/nossr50/skills/child/FamilyTree.java

54 lines
1.7 KiB
Java
Raw Normal View History

package com.gmail.nossr50.skills.child;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Set;
2018-12-29 14:24:55 +01:00
import com.gmail.nossr50.datatypes.skills.PrimarySkill;
public class FamilyTree {
2018-12-29 14:24:55 +01:00
private static HashMap<PrimarySkill, Set<PrimarySkill>> tree = new HashMap<PrimarySkill, Set<PrimarySkill>>();
2018-12-29 14:24:55 +01:00
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);
}
2018-12-29 14:24:55 +01:00
protected static void registerParent(PrimarySkill childSkill, PrimarySkill parentSkill) {
enforceChildSkill(childSkill);
enforceNotChildSkill(parentSkill);
if (!tree.containsKey(childSkill)) {
2018-12-29 14:24:55 +01:00
tree.put(childSkill, EnumSet.noneOf(PrimarySkill.class));
}
tree.get(childSkill).add(parentSkill);
}
protected static void closeRegistration() {
2018-12-29 14:24:55 +01:00
for (PrimarySkill childSkill : tree.keySet()) {
Set<PrimarySkill> immutableSet = Collections.unmodifiableSet(tree.get(childSkill));
tree.put(childSkill, immutableSet);
}
}
2013-09-28 00:46:19 +02:00
protected static void clearRegistrations() {
tree.clear();
}
2018-12-29 14:24:55 +01:00
protected static void enforceChildSkill(PrimarySkill skill) {
if (!skill.isChildSkill()) {
throw new IllegalArgumentException(skill.name() + " is not a child skill!");
}
}
2018-12-29 14:24:55 +01:00
protected static void enforceNotChildSkill(PrimarySkill skill) {
if (skill.isChildSkill()) {
throw new IllegalArgumentException(skill.name() + " is a child skill!");
}
}
}