LazyResettable.java 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package me.shedaniel.clothconfig2.api;
  2. import java.util.Objects;
  3. import java.util.function.Supplier;
  4. public final class LazyResettable<T> implements Supplier<T> {
  5. private final Supplier<T> supplier;
  6. private T value = null;
  7. private boolean supplied = false;
  8. public LazyResettable(Supplier<T> supplier) {
  9. this.supplier = supplier;
  10. }
  11. @Override
  12. public T get() {
  13. if (!supplied) {
  14. this.value = supplier.get();
  15. this.supplied = true;
  16. }
  17. return value;
  18. }
  19. public void reset() {
  20. this.supplied = false;
  21. this.value = null;
  22. }
  23. @Override
  24. public boolean equals(Object o) {
  25. if (this == o) return true;
  26. if (o == null || getClass() != o.getClass()) return false;
  27. LazyResettable<?> that = (LazyResettable<?>) o;
  28. return Objects.equals(get(), that.get());
  29. }
  30. @Override
  31. public int hashCode() {
  32. T value = get();
  33. return value != null ? value.hashCode() : 0;
  34. }
  35. }