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

com.github.datalking.common.convert.support.NumberToNumberConverterFactory Maven / Gradle / Ivy

The newest version!
package com.github.datalking.common.convert.support;

import com.github.datalking.common.convert.converter.ConditionalConverter;
import com.github.datalking.common.convert.converter.Converter;
import com.github.datalking.common.convert.converter.ConverterFactory;
import com.github.datalking.common.convert.descriptor.TypeDescriptor;
import com.github.datalking.util.NumberUtils;

/**
 * @author yaoo on 5/10/18
 */
public final class NumberToNumberConverterFactory
        implements ConverterFactory, ConditionalConverter {

    public  Converter getConverter(Class targetType) {
        return new NumberToNumber<>(targetType);
    }

    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        return !sourceType.equals(targetType);
    }

    private final static class NumberToNumber implements Converter {

        private final Class targetType;

        public NumberToNumber(Class targetType) {
            this.targetType = targetType;
        }

        public T convert(Number source) {
            return NumberUtils.convertNumberToTargetClass(source, this.targetType);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy