Java 将 Spring Data Sort 转换为 Comparator

Eve*_*ude 5 java spring comparator spring-data

我构建了一个 Spring Boot 应用程序,它使用 Spring Data Sort 类对数据库中的实体进行排序。但是,出于一致性的原因,我还想将该排序机制应用于一般列表或流,因此需要将其转换为Comparator.

我想出了一个解决方案,但我觉得有一种更优雅和/或类型安全的方法。有什么建议吗?

import org.springframework.data.domain.Sort;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.Iterator;

public class ComparatorUtils {
    public static <T> Comparator<T> convert(Sort sort, Class<T> type) {
        final Iterator<Sort.Order> orderIterator = sort.iterator();
        final Sort.Order order = orderIterator.next();

        Comparator<T> comparator = convert(order, type);
        while (orderIterator.hasNext()) {
            comparator = comparator.thenComparing(convert(orderIterator.next(), type));
        }

        return comparator;
    }

    private static <T> Comparator<T> convert(Sort.Order order, Class<T> type) {
        Comparator<T> comparator = Comparator.comparing((T entity) -> {
            try {
                return (Comparable) new PropertyDescriptor(order.getProperty(), type).getReadMethod().invoke(entity);
            } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
                throw new RuntimeException(e);
            }
        });

        if (order.isDescending())
            return comparator.reversed();
        return comparator;
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*iak 0

  • 使用 org.springframework.beans.BeanUtils.getPropertyDescriptor 来缓存描述符(而不是新的 PropertyDescriptor)。
  • 此解决方案不处理按更深字段排序(例如,Person.Address.Street)
  • 空值未正确处理。使用 Comparator.nullsFirst 或 nullsLast。