ConfigContainer.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package me.lortseam.completeconfig.api;
  2. import com.google.common.collect.ImmutableList;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * A container of config entries.
  11. */
  12. public interface ConfigContainer {
  13. /**
  14. * Specifies whether this class is a POJO. If true, every field inside this class will be considered to be a config
  15. * entry.
  16. *
  17. * @return whether this class is a POJO
  18. */
  19. default boolean isConfigPOJO() {
  20. return false;
  21. }
  22. default List<Class<? extends ConfigContainer>> getConfigClasses() {
  23. List<Class<? extends ConfigContainer>> classes = new ArrayList<>();
  24. Class<? extends ConfigContainer> clazz = getClass();
  25. while (clazz != null) {
  26. classes.add(clazz);
  27. if (!ConfigContainer.class.isAssignableFrom(clazz.getSuperclass())) {
  28. break;
  29. }
  30. clazz = (Class<? extends ConfigContainer>) clazz.getSuperclass();
  31. }
  32. return ImmutableList.copyOf(classes);
  33. }
  34. /**
  35. * Used to register other containers. They will then be registered at the same level as this container.
  36. *
  37. * @return an array of other containers
  38. * @see Transitive
  39. */
  40. default ConfigContainer[] getTransitiveContainers() {
  41. return new ConfigContainer[0];
  42. }
  43. /**
  44. * Applied to declare that a field of type {@link ConfigContainer} is transitive. The container will then be
  45. * registered at the same level as this container.
  46. *
  47. * <p>This annotation is only required inside non-POJO classes. POJO classes will always register every field of
  48. * type {@link ConfigContainer}.
  49. *
  50. * @see #getTransitiveContainers()
  51. */
  52. @Target(ElementType.FIELD)
  53. @Retention(RetentionPolicy.RUNTIME)
  54. @interface Transitive {
  55. }
  56. /**
  57. * Can be applied to a field inside a POJO {@link ConfigContainer} class to declare that that field should not
  58. * be considered to be a config entry.
  59. */
  60. @Target(ElementType.FIELD)
  61. @Retention(RetentionPolicy.RUNTIME)
  62. @interface Ignore {
  63. }
  64. }