@Override public Facet facet() { TIntIntHashMap facets = aggregator.facets(); if (facets.isEmpty()) { CacheRecycler.pushIntIntMap(facets); return new InternalIntTermsFacet( facetName, comparatorType, size, ImmutableList.<InternalIntTermsFacet.IntEntry>of(), aggregator.missing()); } else { if (size < EntryPriorityQueue.LIMIT) { EntryPriorityQueue ordered = new EntryPriorityQueue(size, comparatorType.comparator()); for (TIntIntIterator it = facets.iterator(); it.hasNext(); ) { it.advance(); ordered.insertWithOverflow(new InternalIntTermsFacet.IntEntry(it.key(), it.value())); } InternalIntTermsFacet.IntEntry[] list = new InternalIntTermsFacet.IntEntry[ordered.size()]; for (int i = ordered.size() - 1; i >= 0; i--) { list[i] = (InternalIntTermsFacet.IntEntry) ordered.pop(); } CacheRecycler.pushIntIntMap(facets); return new InternalIntTermsFacet( facetName, comparatorType, size, Arrays.asList(list), aggregator.missing()); } else { BoundedTreeSet<InternalIntTermsFacet.IntEntry> ordered = new BoundedTreeSet<InternalIntTermsFacet.IntEntry>(comparatorType.comparator(), size); for (TIntIntIterator it = facets.iterator(); it.hasNext(); ) { it.advance(); ordered.add(new InternalIntTermsFacet.IntEntry(it.key(), it.value())); } CacheRecycler.pushIntIntMap(facets); return new InternalIntTermsFacet( facetName, comparatorType, size, ordered, aggregator.missing()); } } }