All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.polaris.core.converter.Converters Maven / Gradle / Ivy
package io.polaris.core.converter;
import io.polaris.core.annotation.AnnotationProcessing;
import javax.annotation.Nullable;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.Type;
/**
* @author Qt
* @since Dec 27, 2023
*/
public class Converters {
private static volatile ConverterRegistry defaultRegistry;
public static ConverterRegistry getDefaultRegistry() {
if (defaultRegistry != null) {
return defaultRegistry;
}
synchronized (Converters.class) {
if (defaultRegistry == null) {
defaultRegistry = new ConverterRegistry();
}
}
return defaultRegistry;
}
@Nullable
public static Converter getConverter(Type type) {
return getDefaultRegistry().getConverter(type);
}
public static Converter getConverterOrDefault(Type type, Converter defaults) {
return getDefaultRegistry().getConverterOrDefault(type, defaults);
}
public static T convert(Type type, Type valueType, Object value, T defaultValue) {
return getDefaultRegistry().convert(type, valueType, value, defaultValue);
}
public static T convert(Type type, Type valueType, Object value) {
return getDefaultRegistry().convert(type, valueType, value);
}
public static T convert(Type type, Object value, T defaultValue) {
return getDefaultRegistry().convert(type, value, defaultValue);
}
@AnnotationProcessing
public static T convert(Type type, Object value) {
return getDefaultRegistry().convert(type, value);
}
public static T convertQuietly(Type type, Type valueType, Object value, T defaultValue) {
return getDefaultRegistry().convertQuietly(type, valueType, value, defaultValue);
}
public static T convertQuietly(Type type, Type valueType, Object value) {
return getDefaultRegistry().convertQuietly(type, valueType, value);
}
public static T convertQuietly(Type type, Object value, T defaultValue) {
return getDefaultRegistry().convertQuietly(type, value, defaultValue);
}
public static T convertQuietly(Type type, Object value) {
return getDefaultRegistry().convertQuietly(type, value);
}
public static T convertByPropertyEditor(Class type, Object value, T defaultValue) {
PropertyEditor sourceEditor = PropertyEditorManager.findEditor(value.getClass());
PropertyEditor targetEditor = PropertyEditorManager.findEditor(type);
if (sourceEditor != null && targetEditor != null) {
sourceEditor.setValue(value);
targetEditor.setAsText(sourceEditor.getAsText());
return (T) targetEditor.getValue();
}
return defaultValue;
}
}