An ordered collection of Tuples (possibly) with multiples. Data is stored in a priority queue as it comes in, and only sorted when iterator is requested. LimitedSortedDataBag is not spillable. We allow a user defined comparator, but provide a default comparator in cases where the user doesn't spe
| 43 | * cases where the user doesn't specify one. |
| 44 | */ |
| 45 | public class LimitedSortedDataBag implements DataBag { |
| 46 | |
| 47 | private static final Log log = LogFactory.getLog(LimitedSortedDataBag.class); |
| 48 | private static final long serialVersionUID = 1L; |
| 49 | |
| 50 | private final Comparator<Tuple> mComp; |
| 51 | private final PriorityQueue<Tuple> priorityQ; |
| 52 | private final long limit; |
| 53 | |
| 54 | /** |
| 55 | * @param comp Comparator to use to do the sorting. |
| 56 | * If null, DefaultComparator will be used. |
| 57 | */ |
| 58 | public LimitedSortedDataBag(Comparator<Tuple> comp, long limit) { |
| 59 | this.mComp = comp == null ? new DefaultComparator() : comp; |
| 60 | this.limit = limit; |
| 61 | this.priorityQ = new PriorityQueue<Tuple>( |
| 62 | (int)limit, getReversedComparator(mComp)); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the number of elements in the bag in memory. |
| 67 | * @return number of elements in the bag |
| 68 | */ |
| 69 | @Override |
| 70 | public long size() { |
| 71 | return priorityQ.size(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Find out if the bag is sorted. |
| 76 | * @return true if this is a sorted data bag, false otherwise. |
| 77 | */ |
| 78 | @Override |
| 79 | public boolean isSorted() { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Find out if the bag is distinct. |
| 85 | * @return true if the bag is a distinct bag, false otherwise. |
| 86 | */ |
| 87 | @Override |
| 88 | public boolean isDistinct() { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get an iterator to the bag. For default and distinct bags, |
| 94 | * no particular order is guaranteed. For sorted bags the order |
| 95 | * is guaranteed to be sorted according |
| 96 | * to the provided comparator. |
| 97 | * @return tuple iterator |
| 98 | */ |
| 99 | @Override |
| 100 | public Iterator<Tuple> iterator() { |
| 101 | return new LimitedSortedDataBagIterator(); |
| 102 | } |
nothing calls this directly
no outgoing calls
no test coverage detected