Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 603344) +++ CHANGES.txt (working copy) @@ -82,6 +82,10 @@ 11. LUCENE-1064: Changed TopDocs constructor to be public. (Shai Erera via Michael Busch) + 12. LUCENE-1088: Added new "wouldBeInserted(Object)" method to + PriorityQueue that returns true if the Object would be inserted, + but does not actually insert it. (Peter Keegan via Mike + McCandless) Bug fixes Index: src/test/org/apache/lucene/util/TestPriorityQueue.java =================================================================== --- src/test/org/apache/lucene/util/TestPriorityQueue.java (revision 603344) +++ src/test/org/apache/lucene/util/TestPriorityQueue.java (working copy) @@ -59,9 +59,15 @@ { int next = gen.nextInt(); sum += next; - pq.put(new Integer(next)); + final Integer v = new Integer(next); + assertTrue(pq.wouldBeInserted(v)); + pq.put(v); + // Make sure exactly equal element would be inserted + assertTrue(pq.wouldBeInserted(v)); } + assertFalse(pq.wouldBeInserted(new Integer(Integer.MIN_VALUE))); + // Date end = new Date(); // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000); Index: src/java/org/apache/lucene/util/PriorityQueue.java =================================================================== --- src/java/org/apache/lucene/util/PriorityQueue.java (revision 603344) +++ src/java/org/apache/lucene/util/PriorityQueue.java (working copy) @@ -26,7 +26,7 @@ private int maxSize; /** Determines the ordering of objects in this priority queue. Subclasses - must define this one method. */ + must define this one method. Returns true if a < b.*/ protected abstract boolean lessThan(Object a, Object b); /** Subclass constructors must call this. */ @@ -49,6 +49,17 @@ } /** + * Returns true if the Object would be inserted, but does + * not actually insert it. + */ + public boolean wouldBeInserted(Object element) { + if (size < maxSize || (maxSize > 0 && !lessThan(element, heap[1]))) + return true; + else + return false; + } + + /** * Adds element to the PriorityQueue in log(size) time if either * the PriorityQueue is not full, or not lessThan(element, top()). * @param element