package fr.titionfire.ffsaf.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;

import java.util.function.Consumer;

import static fr.titionfire.ffsaf.net2.Client_Thread.MAPPER;

public class JsonConsumer<T> implements Consumer<T> {

    private final Class<T> clazz;
    private final Consumer<T> consumer;

    public JsonConsumer(Class<T> clazz, Consumer<T> consumer){
        this.clazz = clazz;
        this.consumer = consumer;
    }

    public void castAndAccept (TreeNode message) throws JsonProcessingException {
        this.accept(MAPPER.treeToValue(message, clazz));
    }

    @Override
    public void accept(T t) {
        consumer.accept(t);
    }
}
