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 . 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 File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a conversion for the given converter. Inspects either generics or the convertible pairs returned
 * by a {@link GenericConverter}.
 *
 * @param converter the converter to register.
 */
private void registerConversion(final Object converter) {
  Class<?> type = converter.getClass();
  boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
  boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

  if (converter instanceof GenericConverter) {
    GenericConverter genericConverter = (GenericConverter) converter;
    for (GenericConverter.ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
      register(new ConverterRegistration(pair, isReading, isWriting));
    }
  } else if (converter instanceof Converter) {
    Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
    register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));
  } else {
    throw new IllegalArgumentException("Unsupported Converter type!");
  }
}
 
Example 2
Source File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * Registers the given {@link ConverterRegistration} as reading or writing pair depending on the type sides being basic
 * Crate types.
 *
 * @param registration the registration.
 */
private void register(final ConverterRegistration registration) {
  GenericConverter.ConvertiblePair pair = registration.getConvertiblePair();

  if (registration.isReading()) {
    readingPairs.add(pair);
    if (LOG.isWarnEnabled() && !registration.isSimpleSourceType()) {
      LOG.warn(String.format(READ_CONVERTER_NOT_SIMPLE, pair.getSourceType(), pair.getTargetType()));
    }
  }

  if (registration.isWriting()) {
    writingPairs.add(pair);
    customSimpleTypes.add(pair.getSourceType());
    if (LOG.isWarnEnabled() && !registration.isSimpleTargetType()) {
      LOG.warn(String.format(WRITE_CONVERTER_NOT_SIMPLE, pair.getSourceType(), pair.getTargetType()));
    }
  }
}
 
Example 3
Source File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the actual target type for the given {@code sourceType} and {@code requestedTargetType}. Note that the
 * returned {@link Class} could be an assignable type to the given {@code requestedTargetType}.
 *
 * @param sourceType must not be {@literal null}.
 * @param requestedTargetType can be {@literal null}.
 * @return
 */
private Class<?> getCustomReadTarget(Class<?> sourceType, Class<?> requestedTargetType) {
  notNull(sourceType);
  if (requestedTargetType == null) {
    return null;
  }

  GenericConverter.ConvertiblePair lookupKey = new GenericConverter.ConvertiblePair(sourceType, requestedTargetType);
  CacheValue readTargetTypeValue = customReadTargetTypes.get(lookupKey);

  if (readTargetTypeValue != null) {
    return readTargetTypeValue.getType();
  }

  readTargetTypeValue = CacheValue.of(getCustomTarget(sourceType, requestedTargetType, readingPairs));
  CacheValue cacheValue = customReadTargetTypes.putIfAbsent(lookupKey, readTargetTypeValue);

  return cacheValue != null ? cacheValue.getType() : readTargetTypeValue.getType();
}
 
Example 4
Source File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * Inspects the given {@link org.springframework.core.convert.converter.GenericConverter.ConvertiblePair} for ones
 * that have a source compatible type as source. Additionally checks assignability of the target type if one is
 * given.
 *
 * @param sourceType must not be {@literal null}.
 * @param requestedTargetType can be {@literal null}.
 * @param pairs must not be {@literal null}.
 * @return
 */
private static Class<?> getCustomTarget(Class<?> sourceType, Class<?> requestedTargetType,
  Iterable<GenericConverter.ConvertiblePair> pairs) {
  notNull(sourceType);
  notNull(pairs);

  for (GenericConverter.ConvertiblePair typePair : pairs) {
    if (typePair.getSourceType().isAssignableFrom(sourceType)) {
      Class<?> targetType = typePair.getTargetType();
      if (requestedTargetType == null || targetType.isAssignableFrom(requestedTargetType)) {
        return targetType;
      }
    }
  }

  return null;
}
 
Example 5
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testConvertiblePairEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class);
	assertEquals(pair, pairEqual);
	assertEquals(pair.hashCode(), pairEqual.hashCode());
}
 
Example 6
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testConvertiblePairDifferentEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class);
	assertFalse(pair.equals(pairOpposite));
	assertFalse(pair.hashCode() == pairOpposite.hashCode());
}
 
Example 7
Source File: GenericConversionServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testConvertiblePairEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class);
	assertEquals(pair, pairEqual);
	assertEquals(pair.hashCode(), pairEqual.hashCode());
}
 
Example 8
Source File: GenericConversionServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testConvertiblePairDifferentEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class);
	assertFalse(pair.equals(pairOpposite));
	assertFalse(pair.hashCode() == pairOpposite.hashCode());
}
 
Example 9
Source File: GenericConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertiblePairEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class);
	assertEquals(pair, pairEqual);
	assertEquals(pair.hashCode(), pairEqual.hashCode());
}
 
Example 10
Source File: GenericConversionServiceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertiblePairDifferentEqualsAndHash() {
	GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
	GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class);
	assertFalse(pair.equals(pairOpposite));
	assertFalse(pair.hashCode() == pairOpposite.hashCode());
}