Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kr.entree.lionbossraid.config.option;
- import kr.entree.lionbossraid.config.option.config.ConfigProperty;
- import org.bukkit.configuration.ConfigurationSection;
- import java.util.Collection;
- import java.util.List;
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.stream.Collector;
- import java.util.stream.Collectors;
- /**
- * Created by JunHyung Lim on 2019-12-06
- */
- public class Mapper<T, R> implements Property<R> {
- private final Property<T> property;
- private final Function<T, R> mapper;
- private final Consumer<R> setter;
- private Mapper(Property<T> property, Function<T, R> mapper, Consumer<R> setter) {
- this.property = property;
- this.mapper = mapper;
- this.setter = setter;
- }
- public static <T, R> Mapper<T, R> of(Property<T> property, Function<T, R> mapper, Consumer<R> setter) {
- return new Mapper<>(property, mapper, setter);
- }
- public static <T, R extends T> Mapper<T, R> of(Property<T> property, Function<T, R> mapper) {
- return of(property, mapper, property::set);
- }
- public static <T extends Number> Mapper<Number, T> configNumberOf(ConfigurationSection section, String key, Function<Number, T> mapper) {
- return of(ConfigProperty.ofNumber(section, key), mapper);
- }
- 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) {
- return of(property, ts -> ts.stream().map(mapper).collect(collector), rs -> property.set((Collection<T>) rs));
- }
- public static <T, R extends T> Mapper<Collection<T>, List<R>> listOf(Property<Collection<T>> property, Function<T, R> mapper) {
- return Mapper.collectOf(property, mapper, Collectors.toList());
- }
- @Override
- public R get() {
- return mapper.apply(property.get());
- }
- @Override
- public void set(R value) {
- setter.accept(value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement