All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.raynigon.unit_api.jackson.annotation.JsonQuantityHelper Maven / Gradle / Ivy

The newest version!
package com.raynigon.unit_api.jackson.annotation;

import com.raynigon.unit_api.core.io.QuantityReader;
import com.raynigon.unit_api.core.io.QuantityWriter;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class JsonQuantityHelper {

    public static QuantityReader getReaderInstance(JsonQuantityReader readerWrapper) {
        if (readerWrapper == null) return null;
        return createInstace(readerWrapper.value(), "Reader");
    }

    public static QuantityWriter getWriterInstance(JsonQuantityWriter writerWrapper) {
        if (writerWrapper == null) return null;
        return createInstace(writerWrapper.value(), "Writer");
    }

    private static  T createInstace(Class readerType, String type) {
        try {
            Constructor ctor = readerType.getConstructor();
            return ctor.newInstance();
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Unable to create Quantity " + type + ":" + readerType, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy