Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package kr.entree.lionbossraid.config.option;
  2.  
  3. import kr.entree.lionbossraid.config.option.config.ConfigProperty;
  4. import org.bukkit.configuration.ConfigurationSection;
  5.  
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.function.Consumer;
  9. import java.util.function.Function;
  10. import java.util.stream.Collector;
  11. import java.util.stream.Collectors;
  12.  
  13. /**
  14. * Created by JunHyung Lim on 2019-12-06
  15. */
  16. public class Mapper<T, R> implements Property<R> {
  17. private final Property<T> property;
  18. private final Function<T, R> mapper;
  19. private final Consumer<R> setter;
  20.  
  21. private Mapper(Property<T> property, Function<T, R> mapper, Consumer<R> setter) {
  22. this.property = property;
  23. this.mapper = mapper;
  24. this.setter = setter;
  25. }
  26.  
  27. public static <T, R> Mapper<T, R> of(Property<T> property, Function<T, R> mapper, Consumer<R> setter) {
  28. return new Mapper<>(property, mapper, setter);
  29. }
  30.  
  31. public static <T, R extends T> Mapper<T, R> of(Property<T> property, Function<T, R> mapper) {
  32. return of(property, mapper, property::set);
  33. }
  34.  
  35. public static <T extends Number> Mapper<Number, T> configNumberOf(ConfigurationSection section, String key, Function<Number, T> mapper) {
  36. return of(ConfigProperty.ofNumber(section, key), mapper);
  37. }
  38.  
  39. public static <T, R extends T> Mapper<Collection<T>, ? extends Collection<R>> collectOf(Property<Collection<T>> property, Function<T, R> mapper, Collector<R, Object, ? extends Collection<R>> collector) {
  40. return of(property, ts -> ts.stream().map(mapper).collect(collector), rs -> property.set((Collection<T>) rs));
  41. }
  42.  
  43. public static <T, R extends T> Mapper<Collection<T>, List<R>> listOf(Property<Collection<T>> property, Function<T, R> mapper) {
  44. return Mapper.collectOf(property, mapper, Collectors.toList());
  45. }
  46.  
  47. @Override
  48. public R get() {
  49. return mapper.apply(property.get());
  50. }
  51.  
  52. @Override
  53. public void set(R value) {
  54. setter.accept(value);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement