package com.tmerclub.cloud.search.canal.support.adapter;

import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.search.canal.util.StringUtils;

/**
 * @author throwable
 * @version v1
 * @description
 * @since 2020/9/20 17:28
 */
@SuppressWarnings("rawtypes")
class FastJsonSourceAdapter<T> implements SourceAdapter<String, T> {

    private final Class<T> klass;

    private FastJsonSourceAdapter(Class<T> klass) {
        this.klass = klass;
    }

    @SuppressWarnings("unchecked")
    public static FastJsonSourceAdapter of(Class klass) {
        return new FastJsonSourceAdapter(klass);
    }

    public Class<T> getKlass() {
        return klass;
    }

    @Override
    public T adapt(String source) {
        if (StringUtils.X.isEmpty(source)) {
            return null;
        }
        return JSON.parseObject(source, klass);
    }
}
