awesome assets from gffs code
[feisty_meow.git] / kona / src / org / gffs / cache / CacheList.java
1 package org.gffs.cache;
2
3 /*
4  * Copyright 2006 University of Virginia
5  * 
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
7  * obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
13  * and limitations under the License.
14  */
15
16 abstract class CacheList<KeyType, DataType>
17 {
18         protected int _myRole;
19
20         protected RoleBasedCacheNode<KeyType, DataType> _head;
21         protected RoleBasedCacheNode<KeyType, DataType> _tail;
22
23         protected CacheList(int role)
24         {
25                 _myRole = role;
26
27                 _head = _tail = null;
28         }
29
30         public abstract void insert(RoleBasedCacheNode<KeyType, DataType> node);
31
32         public RoleBasedCacheNode<KeyType, DataType> removeFirst()
33         {
34                 if (_head == null)
35                         return null;
36
37                 RoleBasedCacheNode<KeyType, DataType> ret = _head;
38
39                 _head = _head.getNext(_myRole);
40                 if (_head != null)
41                         _head.setPrevious(_myRole, null);
42                 else
43                         _tail = null;
44
45                 ret.clearLinks(_myRole);
46                 return ret;
47         }
48
49         public RoleBasedCacheNode<KeyType, DataType> peekFirst()
50         {
51                 if (_head == null)
52                         return null;
53
54                 return _head;
55         }
56
57         public void remove(RoleBasedCacheNode<KeyType, DataType> node)
58         {
59                 if (node.getPrevious(_myRole) == null) // At the head of the list
60                         _head = node.getNext(_myRole);
61                 else
62                         node.getPrevious(_myRole).setNext(_myRole, node.getNext(_myRole));
63
64                 if (node.getNext(_myRole) == null) // At the tail of the list
65                         _tail = node.getPrevious(_myRole);
66                 else
67                         node.getNext(_myRole).setPrevious(_myRole, node.getPrevious(_myRole));
68
69                 node.clearLinks(_myRole);
70         }
71
72         public void clear()
73         {
74                 _head = null;
75                 _tail = null;
76         }
77 }