KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > memory > CacheEntryList


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.memory;
19
20 /**
21  * Maintains a simple linked list of CacheEntry objects. It is thread safe.
22  *
23  * @version $Revision$
24  */

25 public class CacheEntryList {
26     
27     // Points at the tail of the CacheEntry list
28
public final CacheEntry tail = new CacheEntry(null, null);
29     
30     public CacheEntryList() {
31         tail.next = tail.previous = tail;
32     }
33             
34     public void add(CacheEntry ce) {
35         addEntryBefore(tail, ce);
36     }
37     
38     private void addEntryBefore(CacheEntry position, CacheEntry ce) {
39         assert ce.key!=null && ce.next==null && ce.owner==null;
40         
41         synchronized( tail ) {
42             ce.owner=this;
43             ce.next = position;
44             ce.previous = position.previous;
45             ce.previous.next = ce;
46             ce.next.previous = ce;
47         }
48     }
49         
50     public void clear() {
51         synchronized( tail ) {
52             tail.next = tail.previous = tail;
53         }
54     }
55
56     public CacheEvictor createFIFOCacheEvictor() {
57         return new CacheEvictor() {
58             public CacheEntry evictCacheEntry() {
59                 CacheEntry rc;
60                 synchronized( tail ) {
61                     rc = tail.next;
62                 }
63                 return rc.remove() ? rc : null;
64             }
65         };
66     }
67     
68     public CacheEvictor createLIFOCacheEvictor() {
69         return new CacheEvictor() {
70             public CacheEntry evictCacheEntry() {
71                 CacheEntry rc;
72                 synchronized( tail ) {
73                     rc = tail.previous;
74                 }
75                 return rc.remove() ? rc : null;
76             }
77         };
78     }
79
80 }
81
Popular Tags