int minHits = _fspec.getMinHitCount();
LinkedList<BrowseFacet> list = new LinkedList<BrowseFacet>();
int cnt = 0;
Comparable<?> facet = null;
FacetIterator iter = this.iterator();
Comparator<BrowseFacet> comparator;
if (FacetSortSpec.OrderValueAsc.equals(_fspec.getOrderBy())) {
while ((facet = iter.next(minHits)) != null) {
// find the next facet whose combined hit count obeys minHits
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
if (++cnt >= maxCnt) break;
}
} else if (FacetSortSpec.OrderHitsDesc.equals(_fspec.getOrderBy())) {
comparator = new Comparator<BrowseFacet>() {
@Override
public int compare(BrowseFacet f1, BrowseFacet f2) {
int val = f2.getFacetValueHitCount() - f1.getFacetValueHitCount();
if (val == 0) {
val = (f1.getValue().compareTo(f2.getValue()));
}
return val;
}
};
if (maxCnt != Integer.MAX_VALUE) {
// we will maintain a min heap of size maxCnt
// Order by hits in descending order and max count is supplied
PriorityQueue queue = createPQ(maxCnt, comparator);
int qsize = 0;
while ((qsize < maxCnt) && ((facet = iter.next(minHits)) != null)) {
queue.add(new BrowseFacet(String.valueOf(facet), iter.count));
qsize++;
}
if (facet != null) {
BrowseFacet rootFacet = (BrowseFacet) queue.top();
minHits = rootFacet.getFacetValueHitCount() + 1;
// facet count less than top of min heap, it will never be added
while (((facet = iter.next(minHits)) != null)) {
rootFacet.setValue(String.valueOf(facet));
rootFacet.setFacetValueHitCount(iter.count);
rootFacet = (BrowseFacet) queue.updateTop();
minHits = rootFacet.getFacetValueHitCount() + 1;
}
}
// at this point, queue contains top maxCnt facets that have hitcount >= minHits
while (qsize-- > 0) {
// append each entry to the beginning of the facet list to order facets by hits descending
list.addFirst((BrowseFacet) queue.pop());
}
} else {
// no maxCnt specified. So fetch all facets according to minHits and sort them later
while ((facet = iter.next(minHits)) != null)
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
Collections.sort(list, comparator);
}
} else // FacetSortSpec.OrderByCustom.equals(_fspec.getOrderBy()
{
comparator = _fspec.getCustomComparatorFactory().newComparator();
if (maxCnt != Integer.MAX_VALUE) {
PriorityQueue queue = createPQ(maxCnt, comparator);
BrowseFacet browseFacet = new BrowseFacet();
int qsize = 0;
while ((qsize < maxCnt) && ((facet = iter.next(minHits)) != null)) {
queue.add(new BrowseFacet(String.valueOf(facet), iter.count));
qsize++;
}
if (facet != null) {
while ((facet = iter.next(minHits)) != null) {
// check with the top of min heap
browseFacet.setFacetValueHitCount(iter.count);
browseFacet.setValue(String.valueOf(facet));
browseFacet = (BrowseFacet) queue.insertWithOverflow(browseFacet);
}
}
// remove from queue and add to the list
while (qsize-- > 0)
list.addFirst((BrowseFacet) queue.pop());
} else {
// order by custom but no max count supplied
while ((facet = iter.next(minHits)) != null)
list.add(new BrowseFacet(String.valueOf(facet), iter.count));
Collections.sort(list, comparator);
}
}
return list;