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

com.synerset.unitility.quarkus.PhysicalQuantityJakartaProvider Maven / Gradle / Ivy

Go to download

The Java Physics Units of Measure Library - Unitility, Quarkus extension. This module integrates unitility-core and unitility-jackson. It provides a preconfigured Jackson module and JakartaParamProvider, enabling automatic serialization/deserialization for all supported physical quantity types. This configuration is automatically included through the Jandex index.

There is a newer version: 2.4.2
Show newest version
package com.synerset.unitility.quarkus;

import com.synerset.unitility.quarkus.serialization.PhysicalQuantityParamJakartaConverter;
import com.synerset.unitility.unitsystem.PhysicalQuantity;
import com.synerset.unitility.unitsystem.exceptions.UnitSystemClassNotSupportedException;
import com.synerset.unitility.unitsystem.util.PhysicalQuantityParsingFactory;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The PhysicalQuantityJakartaProvider class is a Jakarta RS {@link ParamConverterProvider} that provides
 * {@link ParamConverter} instances for handling {@link PhysicalQuantity} instances in Jakarta resource methods.
 * This mechanism is required for deserializing {@link PhysicalQuantity} from {@link PathParam} or {@link QueryParam}
 * based on parsers registered in {@link PhysicalQuantityParsingFactory}.
 */
@Provider
@Priority(1000)
@ApplicationScoped
class PhysicalQuantityJakartaProvider implements ParamConverterProvider {

    private final Map, ParamConverter> immutableConverterRegistry;

    public PhysicalQuantityJakartaProvider(@DefaultParsingFactory PhysicalQuantityParsingFactory parsingRegistry) {

        Map, ParamConverter> registry = new ConcurrentHashMap<>();
        parsingRegistry.findAllRegisteredClasses()
                .forEach(quantityClass -> registry.put(
                        quantityClass,
                        new PhysicalQuantityParamJakartaConverter<>(quantityClass, parsingRegistry))
                );
        this.immutableConverterRegistry = Collections.unmodifiableMap(registry);
    }

    /**
     * Returns a {@link ParamConverter} instance for the specified target class.
     *
     * @param targetClass      The class of the target type to be converted.
     * @param parametrizedType The generic type of the target type.
     * @param annotations      Annotations on the target type.
     * @param               The target type.
     * @return A ParamConverter for the specified target class.
     */
    @Override
    @SuppressWarnings("unchecked")
    public  ParamConverter getConverter(Class targetClass, Type parametrizedType, Annotation[] annotations) {
        validateTargetClass(targetClass);
        return (ParamConverter) immutableConverterRegistry.get(targetClass);
    }

    /**
     * Validates whether the target class is supported and registered.
     *
     * @param targetClass The target class to be validated.
     */
    public void validateTargetClass(Class targetClass) {
        if (!PhysicalQuantity.class.isAssignableFrom(targetClass)) {
            throw new UnitSystemClassNotSupportedException("Target class does not implements interface: " +
                    PhysicalQuantity.class + ". Target class: " + targetClass);
        }
        if (!immutableConverterRegistry.containsKey(targetClass)) {
            throw new UnitSystemClassNotSupportedException("Target class not found in converters registry. " +
                    "Target class: " + targetClass);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy