DDSkills.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package org.leumasjaffe.charsheet.model.skill;
  2. import java.util.Collection;
  3. import java.util.Collections;
  4. import java.util.Map;
  5. import java.util.TreeMap;
  6. import java.util.stream.Collectors;
  7. import org.leumasjaffe.observer.Observable;
  8. import com.fasterxml.jackson.annotation.JsonCreator;
  9. import com.fasterxml.jackson.annotation.JsonValue;
  10. import lombok.AccessLevel;
  11. import lombok.experimental.FieldDefaults;
  12. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  13. public class DDSkills extends Observable.Instance {
  14. Map<String, DDSkill> skills = new TreeMap<>();
  15. @JsonCreator
  16. public DDSkills(Collection<DDSkill> in) {
  17. skills.putAll(DDSkillPrototype.all().collect(Collectors.toMap(t -> t.getName(), t -> new DDSkill(t))));
  18. skills.putAll(in.stream().collect(Collectors.toMap(t -> t.getName(), t -> t)));
  19. }
  20. @JsonValue
  21. private Collection<DDSkill> getSerial() {
  22. return skills.values().stream().filter(s -> s.getPointsSpent() > 0).collect(Collectors.toList());
  23. }
  24. public Collection<DDSkill> getSkills() {
  25. return Collections.unmodifiableCollection(skills.values());
  26. }
  27. public DDSkill getSkill(String name) {
  28. skills.computeIfAbsent(name, DDSkill::new);
  29. return skills.get(name);
  30. }
  31. public void removeSkill(DDSkill skill) {
  32. final DDSkill removed = skills.get(skill.getName());
  33. if (removed != null && !skill.equals(removed)) {
  34. throw new IllegalArgumentException("Attempting to remove a skill (" + skill.getName() + ") not in the object");
  35. }
  36. skills.remove(skill.getName());
  37. }
  38. }