01: package org.jgroups.util;
02:
03: /**
04: * A bounded subclass of List, oldest elements are removed once max capacity is exceeded
05: * @author Bela Ban Nov 20, 2003
06: * @version $Id: BoundedList.java,v 1.2 2004/07/26 15:23:26 belaban Exp $
07: */
08: public class BoundedList extends List {
09: int max_capacity = 10;
10:
11: public BoundedList() {
12: }
13:
14: public BoundedList(int size) {
15: super ();
16: max_capacity = size;
17: }
18:
19: /**
20: * Adds an element at the tail. Removes an object from the head if capacity is exceeded
21: * @param obj The object to be added
22: */
23: public void add(Object obj) {
24: if (obj == null)
25: return;
26: while (size >= max_capacity && size > 0) {
27: removeFromHead();
28: }
29: super .add(obj);
30: }
31:
32: /**
33: * Adds an object to the head, removes an element from the tail if capacity has been exceeded
34: * @param obj The object to be added
35: */
36: public void addAtHead(Object obj) {
37: if (obj == null)
38: return;
39: while (size >= max_capacity && size > 0) {
40: remove();
41: }
42: super.addAtHead(obj);
43: }
44: }
|