1

をソートするためにメソッドを使用する必要がないことは理解していますがPriorityQueue、アイテムを追加してアイテムをフェッチするだけで、それらは自然な順序になります。

public class JobSetQueue extends PriorityBlockingQueue<JobSet> {
    public JobSetQueue() {
        super(1, new JobSetComparator());
    }
}

コンパレータ

以下の getValue() メソッドが最高の優先度で期待される値を返していること、および Comparator が期待する適切な値が返されていることを確認するために、デバッガーをステップ実行しました。私が間違っている? コンプレーターが PriorityQueue の順序に影響を与えるために何かする必要がありますか?

public class JobSetComparator implements Comparator<JobSet> {

    @Override
    public int compare(JobSet o1, JobSet o2) {
        return Integer.compare(o1.getHighestPriority().getValue(), o2.getHighestPriority().getValue());
    }
}

優先順位

public class Priority {
    public static final Priority TOP = new Priority("TOP", 1000);

    public static final Priority PRIORITY_REMAN = new Priority("PRIORITY_REMAN", 750);

    public static final Priority PRIORITY = new Priority("PRIORITY", 500);

    public static final Priority STANDARD_REMAN = new Priority("STANDARD_REMAN", 250);

    public static final Priority STANDARD = new Priority("STANDARD", 100);

    private final String name;
    private final int value;

    protected Priority(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }

    public String toString() {
        return getName();
    }
}

私のテスト

@Before
public void setUp() {
    queue = new JobSetQueue();

    queue.add(new JobSet(new JobUnit(new Product(new ProductEntity(), Priority.STANDARD), 1)));
    queue.add(new JobSet(new JobUnit(new Product(new ProductEntity(), Priority.PRIORITY_REMAN), 1)));
    queue.add(new JobSet(new JobUnit(new Product(new ProductEntity(), Priority.PRIORITY), 1)));
}

@Test
public void testTop() {
    queue.add(new JobSet(new JobUnit(new Product(new ProductEntity(), Priority.TOP), 1)));

    Assert.assertEquals("Queue priority,", Priority.TOP, queue.poll().getJobUnitList().get(0).getProduct().getPriority());
}
4

2 に答える 2

0

まず、Javadoc に Integer.compare が表示されず、compareTo が表示されます。

第二に、あなたのコンパレーターは後方にあると思います。最高の優先度を小さい優先度の前に置きたい:

 @Override
    public int compare(JobSet o1, JobSet o2) {
        return o2.getHighestPriority().getValue() - o1.getHighestPriority().getValue());
    }

ここでは、01 の優先度が高い場合 (つまり、o1 が 02 よりも小さい場合にキュー内で前に来る場合)、負の数を返します。

于 2013-05-20T21:02:39.600 に答える