をソートするためにメソッドを使用する必要がないことは理解していますが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());
}