KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > util > TemporaryList


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - TemporaryList.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.agent.util;
22
23 import java.util.LinkedList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * The <code>TemporaryList</code> implements a list whose items are
28  * automatically removed after a predefined timeout. When an item is
29  * removed by timeout, listener can be called to handle the remove.
30  *
31  * @author Frank Fock
32  */

33 public class TemporaryList {
34
35   public static final int DEFAULT_ITEM_TIMEOUT = 5000;
36
37   // default timeout for entries in 1/1000 seconds.
38
private int timeout = DEFAULT_ITEM_TIMEOUT;
39   private LinkedList JavaDoc list = new LinkedList JavaDoc();
40
41   public TemporaryList() {
42   }
43
44   public TemporaryList(int timeout) {
45     this.timeout = timeout;
46   }
47
48   public synchronized void add(Object JavaDoc o) {
49     long now = System.currentTimeMillis();
50     if ((list.size() > 0) &&
51         (((TemporaryListItem)list.getFirst()).atMaturity(now))) {
52       list.removeFirst();
53     }
54     if ((list.size() > 0) &&
55         (((TemporaryListItem)list.getLast()).atMaturity(now))) {
56       list.removeLast();
57     }
58     list.addFirst(new TemporaryListItem(o));
59   }
60
61   public synchronized boolean contains(Object JavaDoc o) {
62     for (Iterator JavaDoc it = list.iterator(); it.hasNext(); ) {
63       TemporaryListItem item = (TemporaryListItem) it.next();
64       if (item.getItem().equals(o)) {
65         return true;
66       }
67     }
68     return false;
69   }
70
71   public synchronized boolean remove(Object JavaDoc o) {
72     long now = System.currentTimeMillis();
73     for (Iterator JavaDoc it = list.iterator(); it.hasNext(); ) {
74       TemporaryListItem item = (TemporaryListItem)it.next();
75       if (item.getItem().equals(o)) {
76         it.remove();
77         return true;
78       }
79       else if (item.atMaturity(now)) {
80         it.remove();
81       }
82     }
83     return false;
84   }
85
86   public void setTimeout(int timeout) {
87     this.timeout = timeout;
88   }
89
90   public int getTimeout() {
91     return timeout;
92   }
93
94   public Iterator JavaDoc iterator() {
95     return new TemporaryListIterator();
96   }
97
98   public int size() {
99     return list.size();
100   }
101
102
103   public synchronized void clear() {
104     list.clear();
105   }
106
107
108   class TemporaryListItem {
109     private Object JavaDoc item;
110     private long timeOfMaturity;
111
112     public TemporaryListItem(Object JavaDoc item) {
113       this.item = item;
114       this.timeOfMaturity = System.currentTimeMillis() + timeout;
115     }
116
117     public long getTimeOfMaturity() {
118       return timeOfMaturity;
119     }
120
121     public Object JavaDoc getItem() {
122       return item;
123     }
124
125     public boolean equals(Object JavaDoc obj) {
126       return item.equals(obj);
127     }
128
129     public int hashCode() {
130       return item.hashCode();
131     }
132
133     public boolean atMaturity(long referenceTime) {
134       return (referenceTime > timeOfMaturity);
135     }
136   }
137
138   class TemporaryListIterator implements Iterator JavaDoc {
139
140     private Iterator JavaDoc iterator;
141
142     public TemporaryListIterator() {
143       iterator = list.iterator();
144     }
145
146     public boolean hasNext() {
147       return iterator.hasNext();
148     }
149
150     public Object JavaDoc next() {
151       return ((TemporaryListItem)iterator.next()).getItem();
152     }
153
154     public void remove() {
155       iterator.remove();
156     }
157
158   }
159 }
160
Popular Tags