11

The only one I can find is the BoundedFIFOBuffer, which is deprecated. Any others?

Justin
  • 125
  • 1
  • 6

4 Answers4

14

BoundedFIFOBuffer in Apache Commons Collections (which I assume is what you're referring to) is not deprecated, it has just moved packages. The original one in org.apache.commons.collections is deprecated, and has instead been moved to org.apache.commons.collections.buffer

skaffman
  • 381,978
  • 94
  • 789
  • 754
14

Why not just use a LinkedBlockingQueue and use the non-blocking methods offer (or add) and poll to access it? You can create it with a fixed capacity (i.e. to make it bounded).

Hank Gay
  • 65,372
  • 31
  • 148
  • 218
Adamski
  • 51,827
  • 12
  • 103
  • 150
  • Probably not worth being concerned about, but LinkedBlockingQueue will still take a lock. Future versions of hotspot can probably optimize that away. But something to be aware of when profiling later on. – Justin Rudd Aug 18 '09 at 00:34
3

There are some bounded collections in Apache commons-collections, including a BoundedFifoBuffer.

In the same library, is also BoundedBuffer and CircularFifoBuffer

Rich Seller
  • 79,705
  • 22
  • 167
  • 173
2

I've been using Google Collections recently. I think you could have a Java Generics solution pretty easily with it. There is a class called ForwardingList which I think you could implement this idea pretty easily. Obviously not as easy as just using BoundedFifoBuffer (non-generic) or ArrayBlockingQueue.

final ArrayList<MyObject> realList = Lists.newArrayList();
final List<MyObject> list = new ForwardingList<MyObject>() {
    protected List<MyObject> delegate() {
        return realList;
    }

    public boolean add(MyObject obj) {
        if(delegate().size() > 100) return false;
        return delegate().add(obj);
    }
};
Justin Rudd
  • 5,036
  • 4
  • 20
  • 16