• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

talsma-ict / context-propagation / #1454

28 Oct 2024 08:19PM CUT coverage: 84.389% (-10.3%) from 94.737%
#1454

Pull #510

sjoerdtalsma
Move ContextAwareCompletableFuture to core.concurrent package

Signed-off-by: Sjoerd Talsma <sjoerdtalsma@users.noreply.github.com>
Pull Request #510: Add deprecations

464 of 627 new or added lines in 32 files covered. (74.0%)

12 existing lines in 3 files now uncovered.

1119 of 1326 relevant lines covered (84.39%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/context-propagation/src/main/java/nl/talsmasoftware/context/PriorityComparator.java
1
/*
2
 * Copyright 2016-2024 Talsma ICT
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *         http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package nl.talsmasoftware.context;
17

18
import java.util.Comparator;
19

20
import static java.lang.Math.abs;
21

22
/**
23
 * Comparator for classes that may or may not contain a <code>{@literal @}Priority</code> annotation
24
 * on their class or superclasses.
25
 * <p>
26
 * The priority is applied as follows:
27
 * <ol>
28
 * <li>First, non-negative priority is applied in natural order (e.g. {@code 0}, {@code 1}, {@code 2}, ...).</li>
29
 * <li>Next, objects without <code>{@literal @}Priority</code> annotation are applied
30
 * by assigning a {@link #UNDEFINED_PRIORITY default priority} of {@link Integer#MAX_VALUE}.</li>
31
 * <li>Finally, negative priority is applied in reverse-natural order (e.g. {@code -1}, {@code -2}, {@code -3}, ...).</li>
32
 * </ol>
33
 * <p>
34
 * The order of objects with equal (implicit) priority is undefined.
35
 *
36
 * @author Sjoerd Talsma
37
 * @deprecated We will switch to plain ServiceLoader in next major version to reduce complexity.
38
 */
39
@Deprecated
40
final class PriorityComparator implements Comparator<Object> {
41
    private static final int UNDEFINED_PRIORITY = Integer.MAX_VALUE;
42

43
    static final boolean PRIORITY_AVAILABLE = isPriorityAnnotationAvailable();
1✔
44
    static final PriorityComparator INSTANCE = new PriorityComparator();
1✔
45

46
    private PriorityComparator() {
47
    }
48

49
    public int compare(Object value1, Object value2) {
50
        return comparePriority(priorityOf(value1), priorityOf(value2));
1✔
51
    }
52

53
    private static int comparePriority(int prio1, int prio2) {
54
        return prio1 == prio2 ? 0
1✔
55
                : prio1 < 0 ? (prio2 < 0 ? comparePriority(abs(prio1), abs(prio2)) : 1)
1✔
56
                : prio2 < 0 ? -1
57
                : prio1 < prio2 ? -1 : 1;
58
    }
59

60
    private static int priorityOf(Object value) {
61
        if (value == null || !PRIORITY_AVAILABLE) return UNDEFINED_PRIORITY;
1✔
62
        Class<?> type = value instanceof Class ? (Class<?>) value : value.getClass();
1✔
63
        // Don't import Priority. Loading the PriorityComparator class would fail if Priority isn't there at runtime!
64
        javax.annotation.Priority priority = type.getAnnotation(javax.annotation.Priority.class);
1✔
65
        return priority != null ? priority.value() : priorityOf(type.getSuperclass());
1✔
66
    }
67

68
    private static boolean isPriorityAnnotationAvailable() {
69
        try {
70
            return Class.forName("javax.annotation.Priority") != null;
1✔
71
        } catch (ClassNotFoundException cnfe) {
×
72
            return false;
×
73
        } catch (LinkageError le) {
×
74
            return false;
×
75
        }
76
    }
77

78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc