Java Code Examples for org.springframework.core.convert.converter.GenericConverter.ConvertiblePair

The following examples show how to use org.springframework.core.convert.converter.GenericConverter.ConvertiblePair. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
@Nullable
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example 2
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 6 votes vote down vote up
@Nullable
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example 3
Source Project: java-technology-stack   Source File: GenericConversionService.java    License: MIT License 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
@Nullable
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example 4
Source Project: java-technology-stack   Source File: GenericConversionService.java    License: MIT License 6 votes vote down vote up
@Nullable
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example 5
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example 6
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example 7
Source Project: dubbox   Source File: CustomConversions.java    License: Apache License 2.0 6 votes vote down vote up
Class<?> getCustomTarget(Class<?> sourceType, Class<?> expectedTargetType, Iterable<ConvertiblePair> pairs) {
	Assert.notNull(sourceType);
	Assert.notNull(pairs);

	ConvertiblePair expectedTypePair = new ConvertiblePair(sourceType,
			expectedTargetType != null ? expectedTargetType : Any.class);

	if (cache.containsKey(expectedTypePair)) {
		Class<?> cachedTargetType = cache.get(expectedTypePair);
		return cachedTargetType != Any.class ? cachedTargetType : null;
	}

	for (ConvertiblePair typePair : pairs) {
		if (typePair.getSourceType().isAssignableFrom(sourceType)) {
			Class<?> targetType = typePair.getTargetType();
			if (expectedTargetType == null || targetType.isAssignableFrom(expectedTargetType)) {
				cache.putIfAbsent(expectedTypePair, targetType);
				return targetType;
			}
		}
	}

	cache.putIfAbsent(expectedTypePair, Any.class);
	return null;
}
 
Example 8
Source Project: dubbox   Source File: CustomConversions.java    License: Apache License 2.0 6 votes vote down vote up
private void registerConversion(Object converter) {
	Class<?> type = converter.getClass();
	boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
	boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

	if (!isReading && !isWriting) {
		isReading = true;
		isWriting = true;
	}

	if (converter instanceof GenericConverter) {
		GenericConverter genericConverter = (GenericConverter) converter;
		for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
			register(new ConvertibleContext(pair, isReading, isWriting));
		}
	} else if (converter instanceof Converter) {
		Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
		register(new ConvertibleContext(arguments[0], arguments[1], isReading, isWriting));
	} else {
		throw new IllegalArgumentException(
				"Unsupported Converter type! Expected either GenericConverter if Converter.");
	}
}
 
Example 9
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example 10
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example 11
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].toClass(), typeInfo[1].toClass())));
}
 
Example 12
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example 13
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example 14
Source Project: java-technology-stack   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].toClass(), typeInfo[1].toClass())));
}
 
Example 15
Source Project: java-technology-stack   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example 16
Source Project: java-technology-stack   Source File: GenericConversionService.java    License: MIT License 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example 17
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].resolve(), typeInfo[1].resolve())));
}
 
Example 18
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example 19
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example 20
Source Project: dubbox   Source File: CustomConversions.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create new instance registering given converters
 *
 * @param converters
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public CustomConversions(List converters) {
	this.converters = (converters != null ? new ArrayList<Object>(converters) : new ArrayList<Object>());
	this.readingPairs = new HashSet<ConvertiblePair>();
	this.writingPairs = new HashSet<ConvertiblePair>();
	this.customSimpleTypes = new HashSet<Class<?>>();

	this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, SolrSimpleTypes.HOLDER);

	this.converters.add(GeoConverters.StringToPointConverter.INSTANCE);
	this.converters.add(GeoConverters.Point3DToStringConverter.INSTANCE);
	this.converters.add(new SolrjConverters.UpdateToSolrInputDocumentConverter());

	// Register Joda-Time converters only if Joda-Time was found in the
	// classpath.
	if (VersionUtil.isJodaTimeAvailable()) {
		this.converters.add(DateTimeConverters.DateToJodaDateTimeConverter.INSTANCE);
		this.converters.add(DateTimeConverters.JodaDateTimeToDateConverter.INSTANCE);
		this.converters.add(DateTimeConverters.DateToLocalDateTimeConverter.INSTANCE);
		this.converters.add(DateTimeConverters.JodaLocalDateTimeToDateConverter.INSTANCE);
	}

	for (Object converter : this.converters) {
		registerConversion(converter);
	}
}
 
Example 21
Source Project: dubbox   Source File: CustomConversions.java    License: Apache License 2.0 5 votes vote down vote up
private void register(ConvertibleContext context) {
	ConvertiblePair pair = context.getConvertible();
	if (context.isReading()) {
		readingPairs.add(pair);
	}
	if (context.isWriting()) {
		writingPairs.add(pair);
		customSimpleTypes.add(pair.getSourceType());
	}
}
 
Example 22
@Override
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class);
	Assert.notNull(typeInfo, "Unable to the determine source type <S> and target range type R which your " +
			"ConverterFactory<S, R> converts between; declare these generic types.");
	addConverter(new ConverterFactoryAdapter(converterFactory,
			new ConvertiblePair(typeInfo[0].resolve(), typeInfo[1].resolve())));
}
 
Example 23
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example 24
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example 25
Source Project: springlets   Source File: EntityToStringConverterTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link io.springlets.format.EntityToStringConverter#getConvertibleTypes()}.
 */
@Test
public void testGetConvertibleTypes() {
  // Prepare

  // Exercise
  Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();

  // Validate
  assertThat(convertibleTypes).isNotEmpty()
      .containsExactly(new ConvertiblePair(Object.class, String.class));
}
 
Example 26
Source Project: springlets   Source File: EnumToMessageConverterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void checkValidConvertibleTypes() {
  // Prepare

  // Exercise
  Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();

  // Validate
  assertThat(convertibleTypes).isNotEmpty()
      .containsExactly(new ConvertiblePair(Enum.class, String.class));
}
 
Example 27
Source Project: spring-data-crate   Source File: ConverterRegistration.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link ConverterRegistration}.
 *
 * @param convertiblePair must not be {@literal null}.
 * @param isReading whether to force to consider the converter for reading.
 * @param isWriting whether to force to consider the converter for reading.
 */
public ConverterRegistration(ConvertiblePair convertiblePair, boolean isReading, boolean isWriting) {
  notNull(convertiblePair, "Convertible Pair is required");

  this.convertiblePair = convertiblePair;
  reading = isReading;
  writing = isWriting;
}
 
Example 28
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 4 votes vote down vote up
public ConverterAdapter(Converter<?, ?> converter, ResolvableType sourceType, ResolvableType targetType) {
	this.converter = (Converter<Object, Object>) converter;
	this.typeInfo = new ConvertiblePair(sourceType.toClass(), targetType.toClass());
	this.targetType = targetType;
}
 
Example 29
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 4 votes vote down vote up
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
	return Collections.singleton(this.typeInfo);
}
 
Example 30
Source Project: spring-analysis-note   Source File: GenericConversionService.java    License: MIT License 4 votes vote down vote up
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
	this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
	this.typeInfo = typeInfo;
}