com.gitee.feizns.convert.ConvertUtils Maven / Gradle / Ivy
package com.gitee.feizns.convert;
import com.gitee.feizns.convert.impl.DateConverter;
import com.gitee.feizns.convert.impl.NumberConverter;
import com.gitee.feizns.convert.impl.StringConverter;
import com.gitee.feizns.convert.method.MethodConverter;
import com.gitee.feizns.convert.method.valueof.SingleParameterValueOfConverter;
import com.gitee.feizns.convert.method.valueof.TwoParameterValueOfConverter;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/**
* @author feizns
* @since 2019/5/19 0019
*/
public abstract class ConvertUtils {
/**
* 常规转换器
*/
private static final Set CONVERTERS = create();
static {
registry(new NumberConverter());
registry(new DateConverter());
registry(new StringConverter());
registry(new SingleParameterValueOfConverter());
registry(new TwoParameterValueOfConverter());
}
/**
*
* @param original
* @param targetType
* @param
* @return
*/
public static T to(Object original, Class targetType){
if ( original != null && original.getClass() != targetType ) {
for (Converter converter : CONVERTERS) {
if ( converter.support(original.getClass(), targetType) ) {
if ( converter instanceof MethodConverter ) {
((MethodConverter) converter).setConverters(otherConverter(converter));
}
Object ret = converter.to(original, targetType);
if ( ret != null )
return (T) ret;
}
}
}
return (T) original;
}
private static Set otherConverter(Converter converter) {
Set converters = create();
CONVERTERS.stream().filter(item -> item != converter).forEach(converters::add);
return converters;
}
/**
*
* @param original
* @param targetType
* @param defaultVal
* @param
* @return
*/
public static final T to(Object original, Class targetType, T defaultVal) {
T ret = null;
return (ret = to(original, targetType)) != null
? ret
: defaultVal;
}
/**
* 注册转换器
* @param converter
*/
public static final void registry(Converter converter) {
CONVERTERS.add(converter);
}
/**
*
* @param source
* @param target
* @param converter
* @param
* @param
*/
public static final void registry(Class source, Class target, Converter converter) {
CONVERTERS.add(new Converter() {
@Override
public R to(S source, Class targetType) {
return converter.to(source, targetType);
}
@Override
public boolean support(Class sourceType, Class targetType) {
return source.isAssignableFrom(sourceType) && target.isAssignableFrom(target);
}
});
}
/**
*
* @param target
* @param converter
* @param
* @param
*/
public static final void registry(Class target, Converter converter) {
CONVERTERS.add(new Converter() {
@Override
public R to(S source, Class targetType) {
return converter.to(source, targetType);
}
@Override
public boolean support(Class extends S> sourceType, Class extends R> targetType) {
return target.isAssignableFrom(targetType);
}
});
}
private static Set create() {
return new TreeSet<>(priority());
}
/**
* 排序规则
* @return
*/
private static final Comparator super Converter> priority() {
return (o1, o2) -> o1.priority() < o2.priority() ? -1 : 1;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy