Top Description Inners Fields Constructors Methods
org.springframework.core.convert.converter

public Class ConvertingComparator<S, T>

extends Object
implements Comparator<S>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Type Parameters
<S>
the source type
<T>
the target type
Imports
java.util.Comparator, .Map, org.springframework.core.convert.ConversionService, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.util.comparator.Comparators

A Comparator that converts values before they are compared.

The specified Converter will be used to convert each value before it is passed to the underlying Comparator.

Author
Phillip Webb
Since
3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Comparator<T>
private final Converter<S, T>

Constructor Summary

AccessConstructor and Description
public
ConvertingComparator(Converter<S, T>
the converter
converter
)

Create a new ConvertingComparator instance.

public
ConvertingComparator(Comparator<T>
the underlying comparator used to compare the converted values
comparator
,
Converter<S, T>
the converter
converter
)

Create a new ConvertingComparator instance.

public
ConvertingComparator(Comparator<T>
the underlying comparator
comparator
,
ConversionService
the conversion service
conversionService
,
Class<? extends T>
the target type
targetType
)

Create a new ConvertingComparator instance.

Method Summary

Modifier and TypeMethod and Description
public int
compare(S
the first object to be compared.
o1
,
S
the second object to be compared.
o2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
public static <K, V> ConvertingComparator<Map.Entry<K, V>, K>

Returns:

a new ConvertingComparator instance
mapEntryKeys
(Comparator<K>
the underlying comparator used to compare keys
comparator
)

Create a new ConvertingComparator that compares map entries based on their keys.

public static <K, V> ConvertingComparator<Map.Entry<K, V>, V>

Returns:

a new ConvertingComparator instance
mapEntryValues
(Comparator<V>
the underlying comparator used to compare values
comparator
)

Create a new ConvertingComparator that compares map entries based on their values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private final Comparator<T> comparator
converterback to summary
private final Converter<S, T> converter

Constructor Detail

ConvertingComparatorback to summary
public ConvertingComparator(Converter<S, T> converter)

Create a new ConvertingComparator instance.

Parameters
converter:Converter<S, T>

the converter

ConvertingComparatorback to summary
public ConvertingComparator(Comparator<T> comparator, Converter<S, T> converter)

Create a new ConvertingComparator instance.

Parameters
comparator:Comparator<T>

the underlying comparator used to compare the converted values

converter:Converter<S, T>

the converter

ConvertingComparatorback to summary
public ConvertingComparator(Comparator<T> comparator, ConversionService conversionService, Class<? extends T> targetType)

Create a new ConvertingComparator instance.

Parameters
comparator:Comparator<T>

the underlying comparator

conversionService:ConversionService

the conversion service

targetType:Class<? extends T>

the target type

Method Detail

compareback to summary
public int compare(S o1, S o2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
o1:S

the first object to be compared.

o2:S

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
mapEntryKeysback to summary
public static <K, V> ConvertingComparator<Map.Entry<K, V>, K> mapEntryKeys(Comparator<K> comparator)

Create a new ConvertingComparator that compares map entries based on their keys.

Parameters
comparator:Comparator<K>

the underlying comparator used to compare keys

Returns:ConvertingComparator<Map.Entry<K, V>, K>

a new ConvertingComparator instance

mapEntryValuesback to summary
public static <K, V> ConvertingComparator<Map.Entry<K, V>, V> mapEntryValues(Comparator<V> comparator)

Create a new ConvertingComparator that compares map entries based on their values.

Parameters
comparator:Comparator<V>

the underlying comparator used to compare values

Returns:ConvertingComparator<Map.Entry<K, V>, V>

a new ConvertingComparator instance

org.springframework.core.convert.converter back to summary

private Class ConvertingComparator.ConversionServiceConverter<S, T>

extends Object
implements Converter<S, T>
Class Inheritance
  • java.lang.Object
  • org.springframework.core.convert.converter.ConvertingComparator.ConversionServiceConverter
All Implemented Interfaces
org.springframework.core.convert.converter.Converter

Adapts a ConversionService and targetType to a Converter.

Field Summary

Modifier and TypeField and Description
private final ConversionService
private final Class<? extends T>

Constructor Summary

AccessConstructor and Description
public
ConversionServiceConverter(ConversionService conversionService, Class<? extends T> targetType)

Method Summary

Modifier and TypeMethod and Description
public T
convert(S
the source object to convert, which must be an instance of S (never null)
source
)

Implements org.springframework.core.convert.converter.Converter.convert.

Convert the source object of type S to target type T.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conversionServiceback to summary
private final ConversionService conversionService
targetTypeback to summary
private final Class<? extends T> targetType

Constructor Detail

ConversionServiceConverterback to summary
public ConversionServiceConverter(ConversionService conversionService, Class<? extends T> targetType)

Method Detail

convertback to summary
public T convert(S source)

Implements org.springframework.core.convert.converter.Converter.convert.

Doc from org.springframework.core.convert.converter.Converter.convert.

Convert the source object of type S to target type T.

Parameters
source:S

the source object to convert, which must be an instance of S (never null)

Returns:T

the converted object, which must be an instance of T (potentially null)

Annotations
@Override
@Nullable