You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
source-code-hunter/docs/Spring/clazz/Spring-OrderComparator.md

2.7 KiB

Spring OrderComparator

    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);
    }

    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));
    }

  • 测试用例
    @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

    @Nullable
    protected Integer findOrder(Object obj) {
        // 获取Ordered实现类
        return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);
    }

    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 !