/** * Create a new {@code ConvertingComparator} instance. * @param comparator the underlying comparator * @param conversionService the conversion service * @param targetType the target type */ public ConvertingComparator( Comparator<T> comparator, ConversionService conversionService, Class<? extends T> targetType) { this(comparator, new ConversionServiceConverter<>(conversionService, targetType)); }
/** * Create a new {@link ConvertingComparator} that compares {@link java.util.Map.Entry * map * entries} based on their {@link java.util.Map.Entry#getKey() keys}. * @param comparator the underlying comparator used to compare keys * @return a new {@link ConvertingComparator} instance */ public static <K, V> ConvertingComparator<Map.Entry<K, V>, K> mapEntryKeys(Comparator<K> comparator) { return new ConvertingComparator<>(comparator, Map.Entry::getKey); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return convertNullSource(sourceType, targetType); } return this.converterFactory.getConverter(targetType.getObjectType()).convert(source); }
@Override public Set<ConvertiblePair> getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Map.class, Map.class)); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { // Check raw type first... if (this.typeInfo.getTargetType() != targetType.getObjectType()) { return false; } // Full check for complex generic type match required? ResolvableType rt = targetType.getResolvableType(); if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) && !this.targetType.hasUnresolvableGenerics()) { return false; } return !(this.converter instanceof ConditionalConverter) || ((ConditionalConverter) this.converter).matches(sourceType, targetType); }
@Test public void shouldUseConversionServiceOnCompare() throws Exception { ConvertingComparator<String, Integer> convertingComparator = new ConvertingComparator<>( comparator, conversionService, Integer.class); testConversion(convertingComparator); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { boolean matches = true; if (this.converterFactory instanceof ConditionalConverter) { matches = ((ConditionalConverter) this.converterFactory).matches(sourceType, targetType); } if (matches) { Converter<?, ?> converter = this.converterFactory.getConverter(targetType.getType()); if (converter instanceof ConditionalConverter) { matches = ((ConditionalConverter) converter).matches(sourceType, targetType); } } return matches; }
@Nullable public GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { for (GenericConverter converter : this.converters) { if (!(converter instanceof ConditionalGenericConverter) || ((ConditionalGenericConverter) converter).matches(sourceType, targetType)) { return converter; } } return null; }
private void testConversion(ConvertingComparator<String, Integer> convertingComparator) { assertThat(convertingComparator.compare("0", "0"), is(0)); assertThat(convertingComparator.compare("0", "1"), is(-1)); assertThat(convertingComparator.compare("1", "0"), is(1)); comparator.assertCalled(); }
@SuppressWarnings("unused") public static ISBN valueOf(String value) { valueOfCount++; return new ISBN(value); } }
@Override public ListWrapper convert(List source) { return new ListWrapper(source); } });
public static TestEntity findTestEntity(Long id) { return new TestEntity(id); }
@Override public Set<ConvertiblePair> getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(String.class, Baz.class)); } @Override
@Test public void shouldUseConverterOnCompare() throws Exception { ConvertingComparator<String, Integer> convertingComparator = new ConvertingComparator<>( this.comparator, this.converter); testConversion(convertingComparator); }
/** * Create a new {@link ConvertingComparator} that compares {@link java.util.Map.Entry * map entries} based on their {@link java.util.Map.Entry#getValue() values}. * @param comparator the underlying comparator used to compare values * @return a new {@link ConvertingComparator} instance */ public static <K, V> ConvertingComparator<Map.Entry<K, V>, V> mapEntryValues(Comparator<V> comparator) { return new ConvertingComparator<>(comparator, Map.Entry::getValue); }
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; }
@Test public void shouldGetForConverter() throws Exception { testConversion(new ConvertingComparator<>(comparator, converter)); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnNullType() throws Exception { new ConvertingComparator<String, Integer>(this.comparator, this.conversionService, null); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnNullConverter() throws Exception { new ConvertingComparator<String, Integer>(this.comparator, null); }