aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/component/chain/dependencies/ordering/OrderedReadyNodes.java
blob: 9be22f79466d302a50216fa8750b170766b7f77e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.chain.dependencies.ordering;


import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Ensures that Searchers are ordered deterministically.
 *
 * @author Tony Vaagenes
 */
class OrderedReadyNodes {

    private class PriorityComparator implements Comparator<Node> {
        @Override
        public int compare(Node lhs, Node rhs) {
            int result = Integer.valueOf(lhs.classPriority()).compareTo(rhs.classPriority());

            return result != 0 ?
                    result :
                    Integer.valueOf(lhs.priority).compareTo(rhs.priority);
        }
    }

    final private PriorityQueue<Node> nodes = new PriorityQueue<>(10, new PriorityComparator());

    public void add(Node node) {
        nodes.add(node);
    }

    public Node pop() {
        return nodes.poll();
    }

    public boolean isEmpty() {
        return nodes.isEmpty();
    }

}