private Optional<Long> getLastModifiedInMilliseconds(Object object) { return getAuditableBeanWrapper(object)// .flatMap(it -> it.getLastModifiedDate())// .map(it -> conversionService.convert(it, Date.class))// .map(it -> conversionService.convert(it, Instant.class))// .map(it -> it.toEpochMilli()); } }
key, propertySource.getName(), valueType.getSimpleName(), value)); if (!this.conversionService.canConvert(valueType, targetValueType)) { throw new IllegalArgumentException( format("Cannot convert value [%s] from source type [%s] to target type [%s]", value, valueType.getSimpleName(), targetValueType.getSimpleName())); return conversionService.convert(value, targetValueType);
/** * Removes the converter being able to convert any object into an {@link Optional} from the given * {@link ConversionService} in case we're running on Java 8. * * @param conversionService must not be {@literal null}. */ public static void potentiallyRemoveOptionalConverter(ConfigurableConversionService conversionService) { ClassLoader classLoader = JpaQueryExecution.class.getClassLoader(); if (ClassUtils.isPresent("java.util.Optional", classLoader)) { try { Class<?> optionalType = ClassUtils.forName("java.util.Optional", classLoader); conversionService.removeConvertible(Object.class, optionalType); } catch (ClassNotFoundException | LinkageError o_O) {} } } }
/** * setConverters * * @param converters a {@link java.util.Set} object. */ @SuppressWarnings("rawtypes") public void setConverters(Set<Converter> converters) { for (Converter c : converters) { conversion.addConverter(c); } }
/** * Registers converters for wrapper types found on the classpath. * * @param conversionService must not be {@literal null}. */ public static void registerConvertersIn(ConfigurableConversionService conversionService) { Assert.notNull(conversionService, "ConversionService must not be null!"); conversionService.removeConvertible(Collection.class, Object.class); if (GUAVA_PRESENT) { conversionService.addConverter(new NullableWrapperToGuavaOptionalConverter(conversionService)); } if (JDK_8_PRESENT) { conversionService.addConverter(new NullableWrapperToJdk8OptionalConverter(conversionService)); conversionService.addConverter(new NullableWrapperToCompletableFutureConverter(conversionService)); } if (SCALA_PRESENT) { conversionService.addConverter(new NullableWrapperToScalaOptionConverter(conversionService)); } if (JAVASLANG_PRESENT) { conversionService.addConverter(new NullableWrapperToJavaslangOptionConverter(conversionService)); conversionService.addConverter(JavaslangCollections.FromJavaConverter.INSTANCE); } if (VAVR_PRESENT) { conversionService.addConverter(new NullableWrapperToVavrOptionConverter(conversionService)); conversionService.addConverter(VavrCollections.FromJavaConverter.INSTANCE); } conversionService.addConverter(new NullableWrapperToFutureConverter(conversionService)); conversionService.addConverter(IterableToStreamableConverter.INSTANCE); }
public TupleBuilder setConfigurableConversionService(ConfigurableConversionService formattingConversionService) { Assert.notNull(formattingConversionService); if (locale != null) { formattingConversionService.addConverterFactory(new LocaleAwareStringToNumberConverterFactory(NumberFormat .getInstance(locale))); } if (dateFormat != null) { formattingConversionService.addConverter(new StringToDateConverter(dateFormat)); } builder.setConfigurableConversionService(formattingConversionService); return builder; } }
/** * canConvert * * @param sourceType a {@link java.lang.Class} object. * @param targetType a {@link java.lang.Class} object. * @return a boolean. */ public static boolean canConvert(Class<?> sourceType, Class<?> targetType) { return conversion.canConvert(sourceType, targetType); }
@Override protected void configureConversionService(ConfigurableConversionService conversionService) { super.configureConversionService(conversionService); conversionService.addConverter(new TaskPlatformIdConverter()); }
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { Class boxedTargetType = ConversionUtils.boxIfNeeded(targetType); Class boxedSourceType = ConversionUtils.boxIfNeeded(sourceType); return boxedSourceType.equals(boxedTargetType) || this.conversionService.canConvert(boxedSourceType, boxedTargetType); }
@Override public <T> T getProperty(String key, Class<T> targetValueType) { Object value = properties.get(key); if (value == null){ return null; }else{ return conversionService.convert(value, targetValueType); } }
@Override public void configureConversionService(ConfigurableConversionService service) { service.addConverter(testConverter()); }
public static void potentiallyRemoveOptionalConverter( ConfigurableConversionService conversionService) { ClassLoader classLoader = MybatisQueryExecution.class.getClassLoader(); if (ClassUtils.isPresent("java.util.Optional", classLoader)) { try { Class<?> optionalType = ClassUtils.forName("java.util.Optional", classLoader); conversionService.removeConvertible(Object.class, optionalType); } catch (ClassNotFoundException | LinkageError o_O) { } } }
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { Class boxedTargetType = ConversionUtils.boxIfNeeded(targetType); Class boxedSourceType = ConversionUtils.boxIfNeeded(sourceType); return boxedSourceType.equals(boxedTargetType) || this.conversionService.canConvert(boxedSourceType, boxedTargetType); }
/** * convert * * @param source a {@link java.lang.Object} object. * @param targetType a {@link java.lang.Class} object. * @param <T> a T object. * @return a T object. */ @SuppressWarnings("unchecked") public static <T> T convert(Object source, Class<T> targetType) { if (source == null) { return null; } if (targetType.isInstance(source)) { return (T) source; } return conversion.convert(source, targetType); }
protected void configureConversionService(ConfigurableConversionService conversionService) { conversionService.addConverter(new UserFriendShipIdConverter()); }
/** * Removes the converter being able to convert any object into an {@link Optional} from the given * {@link ConversionService} in case we're running on Java 8. * * @param conversionService must not be {@literal null}. */ public static void potentiallyRemoveOptionalConverter(ConfigurableConversionService conversionService) { ClassLoader classLoader = JpaQueryExecution.class.getClassLoader(); if (ClassUtils.isPresent("java.util.Optional", classLoader)) { try { Class<?> optionalType = ClassUtils.forName("java.util.Optional", classLoader); conversionService.removeConvertible(Object.class, optionalType); } catch (ClassNotFoundException | LinkageError o_O) {} } } }
private Optional<Long> getLastModifiedInMilliseconds(Object object) { return getAuditableBeanWrapper(object)// .flatMap(it -> it.getLastModifiedDate())// .map(it -> conversionService.convert(it, Date.class))// .map(it -> conversionService.convert(it, Instant.class))// .map(it -> it.toEpochMilli()); } }
@PostConstruct public void initialize() { conversionService.addConverter(this); }
/** * Checks whether we have a custom conversion registered for the given value into an arbitrary simple JDBC type. * Returns the converted value if so. If not, we perform special enum handling or simply return the value as is. * * @param value to be converted. Must not be {@code null}. * @return the converted value if a conversion applies or the original value. Might return {@code null}. */ @Nullable private Object getPotentiallyConvertedSimpleWrite(Object value) { Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(value.getClass()); if (customTarget.isPresent()) { return conversionService.convert(value, customTarget.get()); } return Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value; }
@PostConstruct public void initialize() { conversionService.addConverter(this); }