Spring OrderComparator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
        boolean p1 = (o1 instanceof PriorityOrdered);
        boolean p2 = (o2 instanceof PriorityOrdered);
        if (p1 && !p2) {
            return -1;
        } else if (p2 && !p1) {
            return 1;
        }

        int i1 = getOrder(o1, sourceProvider);
        int i2 = getOrder(o2, sourceProvider);
        // 对比两个Order值得大小返回
        return Integer.compare(i1, i2);
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) {
        Integer order = null;
        if (obj != null && sourceProvider != null) {
            // 获取Order
            Object orderSource = sourceProvider.getOrderSource(obj);
            if (orderSource != null) {
                if (orderSource.getClass().isArray()) {
                    // 获取 OrderSourceProvider 的值
                    Object[] sources = ObjectUtils.toObjectArray(orderSource);
                    for (Object source : sources) {
                        // 找 order 返回
                        order = findOrder(source);
                        if (order != null) {
                            break;
                        }
                    }
                } else {
                    // 寻找 order
                    order = findOrder(orderSource);
                }
            }
        }
        return (order != null ? order : getOrder(obj));
    }
  • 测试用例
1
2
3
4
5
6
    @Test
    public void compareWithSourceProviderArray() {
        Comparator<Object> customComparator = this.comparator.withSourceProvider(
                new TestSourceProvider(5L, new Object[]{new StubOrdered(10), new StubOrdered(-25)}));
        assertEquals(-1, customComparator.compare(5L, new Object()));
    }

image-20200116141838601

1
2
3
4
5
    @Nullable
    protected Integer findOrder(Object obj) {
        // 获取Ordered实现类
        return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    private static final class StubOrdered implements Ordered {

        private final int order;


        public StubOrdered(int order) {
            this.order = order;
        }

        @Override
        public int getOrder() {
            return this.order;
        }
    }

image-20200116141932486

最终Integer.compare(i1, i2)比较返回 OK !