KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > BoundedList


1 package org.jgroups.util;
2
3
4 /**
5  * A bounded subclass of List, oldest elements are removed once max capacity is exceeded
6  * @author Bela Ban Nov 20, 2003
7  * @version $Id: BoundedList.java,v 1.2 2004/07/26 15:23:26 belaban Exp $
8  */

9 public class BoundedList extends List {
10     int max_capacity=10;
11
12
13
14     public BoundedList() {
15     }
16
17     public BoundedList(int size) {
18         super();
19         max_capacity=size;
20     }
21
22
23     /**
24      * Adds an element at the tail. Removes an object from the head if capacity is exceeded
25      * @param obj The object to be added
26      */

27     public void add(Object JavaDoc obj) {
28         if(obj == null) return;
29         while(size >= max_capacity && size > 0) {
30             removeFromHead();
31         }
32         super.add(obj);
33     }
34
35
36     /**
37      * Adds an object to the head, removes an element from the tail if capacity has been exceeded
38      * @param obj The object to be added
39      */

40     public void addAtHead(Object JavaDoc obj) {
41         if(obj == null) return;
42         while(size >= max_capacity && size > 0) {
43             remove();
44         }
45         super.addAtHead(obj);
46     }
47 }
48
Popular Tags