DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

EvictingQueue.remainingCapacity() #1545

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Could you add isFull() to EvictingQueue? I think it would be useful in cases 
where you want a rolling collection of data, but don't want to operate on that 
data until the queue is full.

Original issue reported on code.google.com by leah...@gmail.com on 25 Sep 2013 at 9:10

GoogleCodeExporter commented 9 years ago
I already left myself a TODO in EvictingQueue for exactly this :-)

// TODO(kak): Do we want to add EvictingQueue#isFull()?

I'm sort of surprised nobody has asked for it earlier.

Original comment by kak@google.com on 25 Sep 2013 at 9:18

GoogleCodeExporter commented 9 years ago
@leahycm:

During the code review, someone suggested we add this instead:
  public int remainingCapacity()

So instead of queue.isFull(), you'd write:
  (queue.remainingCapacity() == 0)

The JDK's ArrayBlockingQueue, LinkedBlockingDeque, and LinkedBlockingQueue APIs 
already have a .remainingCapacity() method.

Thoughts?

Original comment by kak@google.com on 25 Sep 2013 at 10:08

GoogleCodeExporter commented 9 years ago
.remainingCapacity() makes sense, especially since it is part of the 
BlockingQueue interface.

If you think (queue.remainingCapacity() == 0) is going to be common use of 
.remainingCapacity(), then it might make sense to provide isFull() as well: 
similar to how Collection has .size() and .isEmpty().

Original comment by leah...@gmail.com on 26 Sep 2013 at 1:42

GoogleCodeExporter commented 9 years ago
I see about 60 calls to BlockingQueue.remainingCapacity(). Users are doing a 
few different things. The largest group is doing monitoring, so they want more 
than a yes/no signal. A tiny group is doing some sanity checking on the queue 
-- checking whether that the queue has a bound or that the queue can hold a 
required number of simultaneous elements.

The other large group is performing checks like:

if (queue.remainingCapacity() > 0) {
  queue.add(foo);
}

if (queue.remainingCapacity() > REQUIRED) {
  addABunchOfItems();
}

These make me nervous. Possibly some of the users know that there are no 
simultaneous callers. And the users who want to add multiple items to the queue 
don't have an obvious alternative. But the users who just want to add one item 
would be better off calling something like offer(). If BlockingQueue.isFull 
would lead to more of this, I'm against it.

But does that logic apply to EvictingQueue.isFull? EvictingQueue is more likely 
to be used from a single thread, since it's not synchronized, so there's less 
danger of races. On the other hand, if there's no danger of a race, the 
programmer can maintain a count himself if he really wants to, so maybe we 
don't need to provide even remainingCapacity.

Can you say a bit more about why you wouldn't want to operate on the data until 
the queue is full?

Original comment by cpov...@google.com on 26 Sep 2013 at 6:49

GoogleCodeExporter commented 9 years ago
@cpovirk: I have noticed a bunch of people are wrapping EQ in 
Queues.synchronizedQueue() so they can access it from multiple threads.

It does seem a bit silly to force the users to do:

int maxSize = ...;
EvictingQueue<Foo> queue = EvictingQueue.create(maxSize);
// if you need to figure out remaining capacity or fullness, you must always 
pass around queue and maxSize, even though queue knows what maxSize is under 
the hood)

Original comment by kak@google.com on 26 Sep 2013 at 8:04

GoogleCodeExporter commented 9 years ago
@cpovirk: I'm trying to calculate rolling backgrounds for different time 
windows. The problem is more or less a signal detection problem. I'm interested 
in if a sample is above a background rate. I'm storing the past samples (used 
to calculate the background rate) in an EvictingQueue.

Like @kak mentioned, I'm just trying to avoid having to pass maxSize around 
when the queue already knows it.

Original comment by leah...@gmail.com on 26 Sep 2013 at 8:29

GoogleCodeExporter commented 9 years ago
remainingCapacity() should make it in Guava 16.0.

Original comment by kevinb@google.com on 18 Oct 2013 at 5:56

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 19 Dec 2013 at 7:03

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08