ConfigMap.java 924 B

12345678910111213141516171819202122232425262728293031
  1. package me.lortseam.completeconfig;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. public abstract class ConfigMap<T> extends LinkedHashMap<String, T> {
  6. @Override
  7. public T put(String id, T value) {
  8. check(id, value);
  9. return super.put(id, value);
  10. }
  11. @Override
  12. public void putAll(Map<? extends String, ? extends T> m) {
  13. m.forEach(this::check);
  14. super.putAll(m);
  15. }
  16. private void check(String id, T value) {
  17. if (StringUtils.isBlank(id)) {
  18. throw new IllegalArgumentException("ID of type " + value.getClass().getSimpleName() + " must not be null or blank");
  19. }
  20. if (containsKey(id)) {
  21. throw new UnsupportedOperationException("A value of type " + value.getClass().getSimpleName() + " with ID " + id + " already exists in the corresponding structure");
  22. }
  23. }
  24. }