* @param needCurrentPrice
* @param needFutureEndDate TODO
* @return
*/
private ItemInfo getRandomItem(LinkedList<ItemInfo> itemSet, boolean needCurrentPrice, boolean needFutureEndDate) {
ItemInfo itemInfo = null;
Set<ItemInfo> seen = new HashSet<ItemInfo>();
TimestampType currentTime = this.updateAndGetCurrentTime();
synchronized (itemSet) {
int num_items = itemSet.size();
Integer partition = null;
int idx = -1;
if (trace.val)
LOG.trace(String.format("Getting random ItemInfo [numItems=%d, currentTime=%s, needCurrentPrice=%s]",
num_items, currentTime, needCurrentPrice));
long tries = 1000;
while (num_items > 0 && tries-- > 0 && seen.size() < num_items) {
partition = null;
idx = this.rng.nextInt(num_items);
ItemInfo temp = itemSet.get(idx);
assert(temp != null);
if (seen.contains(temp)) continue;
seen.add(temp);
// Needs to have an embedded currentPrice
if (needCurrentPrice && temp.hasCurrentPrice() == false) {
continue;
}
// If they want an item that is ending in the future, then we compare it with
// the current timestamp
if (needFutureEndDate) {
boolean compareTo = (temp.getEndDate().compareTo(currentTime) < 0);
if (trace.val)
LOG.trace("CurrentTime:" + currentTime + " / EndTime:" + temp.getEndDate() + " [compareTo=" + compareTo + "]");
if (temp.hasEndDate() == false || compareTo) {
continue;
}
}
// Uniform
if (this.window_size == null) {
itemInfo = temp;
break;
}
// Temporal Skew
partition = this.getPartition(temp.getSellerId());
if (this.window_partitions.contains(partition)) {
this.window_histogram.put(partition);
itemInfo = temp;
break;
}