KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > index > VMIndexLinkedList


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.kaha.impl.index;
19
20 import org.apache.activemq.kaha.StoreEntry;
21
22 /**
23  * A linked list used by IndexItems
24  *
25  * @version $Revision: 1.2 $
26  */

27 public final class VMIndexLinkedList implements Cloneable JavaDoc, IndexLinkedList{
28     private transient IndexItem root;
29     private transient int size=0;
30
31    
32     /**
33      * Constructs an empty list.
34      */

35     public VMIndexLinkedList(IndexItem header){
36         this.root = header;
37         this.root.next=root.prev=root;
38     }
39     
40     public IndexItem getRoot(){
41         return root;
42     }
43     
44
45
46     /* (non-Javadoc)
47      * @see org.apache.activemq.kaha.impl.IndexLinkedList#getFirst()
48      */

49     public IndexItem getFirst(){
50         if(size==0)
51             return null;
52         return root.next;
53     }
54
55     /* (non-Javadoc)
56      * @see org.apache.activemq.kaha.impl.IndexLinkedList#getLast()
57      */

58     public IndexItem getLast(){
59         if(size==0)
60             return null;
61         return root.prev;
62     }
63
64     /* (non-Javadoc)
65      * @see org.apache.activemq.kaha.impl.IndexLinkedList#removeFirst()
66      */

67     public StoreEntry removeFirst(){
68         if(size==0){
69             return null;
70         }
71         StoreEntry result=root.next;
72         remove(root.next);
73         return result;
74     }
75
76     /* (non-Javadoc)
77      * @see org.apache.activemq.kaha.impl.IndexLinkedList#removeLast()
78      */

79     public Object JavaDoc removeLast(){
80         if(size==0)
81             return null;
82         StoreEntry result=root.prev;
83         remove(root.prev);
84         return result;
85     }
86
87     /* (non-Javadoc)
88      * @see org.apache.activemq.kaha.impl.IndexLinkedList#addFirst(org.apache.activemq.kaha.impl.IndexItem)
89      */

90     public void addFirst(IndexItem item){
91         addBefore(item,root.next);
92     }
93
94     /* (non-Javadoc)
95      * @see org.apache.activemq.kaha.impl.IndexLinkedList#addLast(org.apache.activemq.kaha.impl.IndexItem)
96      */

97     public void addLast(IndexItem item){
98         addBefore(item,root);
99     }
100
101     /* (non-Javadoc)
102      * @see org.apache.activemq.kaha.impl.IndexLinkedList#size()
103      */

104     public int size(){
105         return size;
106     }
107
108     /* (non-Javadoc)
109      * @see org.apache.activemq.kaha.impl.IndexLinkedList#isEmpty()
110      */

111     public boolean isEmpty(){
112         return size==0;
113     }
114
115     /* (non-Javadoc)
116      * @see org.apache.activemq.kaha.impl.IndexLinkedList#add(org.apache.activemq.kaha.impl.IndexItem)
117      */

118     public boolean add(IndexItem item){
119         addBefore(item,root);
120         return true;
121     }
122
123     /* (non-Javadoc)
124      * @see org.apache.activemq.kaha.impl.IndexLinkedList#clear()
125      */

126    public void clear(){
127         root.next=root.prev=root;
128         size=0;
129     }
130
131     // Positional Access Operations
132
/* (non-Javadoc)
133      * @see org.apache.activemq.kaha.impl.IndexLinkedList#get(int)
134      */

135     public IndexItem get(int index){
136         return entry(index);
137     }
138
139     /* (non-Javadoc)
140      * @see org.apache.activemq.kaha.impl.IndexLinkedList#add(int, org.apache.activemq.kaha.impl.IndexItem)
141      */

142     public void add(int index,IndexItem element){
143         addBefore(element,(index==size?root:entry(index)));
144     }
145
146     /* (non-Javadoc)
147      * @see org.apache.activemq.kaha.impl.IndexLinkedList#remove(int)
148      */

149     public Object JavaDoc remove(int index){
150         IndexItem e=entry(index);
151         remove(e);
152         return e;
153     }
154
155     /**
156      * Return the indexed entry.
157      */

158     private IndexItem entry(int index){
159         if(index<0||index>=size)
160             throw new IndexOutOfBoundsException JavaDoc("Index: "+index+", Size: "+size);
161         IndexItem e=root;
162         if(index<size/2){
163             for(int i=0;i<=index;i++)
164                 e=e.next;
165         }else{
166             for(int i=size;i>index;i--)
167                 e=e.prev;
168         }
169         return e;
170     }
171
172     // Search Operations
173
/* (non-Javadoc)
174      * @see org.apache.activemq.kaha.impl.IndexLinkedList#indexOf(org.apache.activemq.kaha.impl.IndexItem)
175      */

176     public int indexOf(StoreEntry o){
177         int index=0;
178         for(IndexItem e=root.next;e!=root;e=e.next){
179             if(o==e){
180                 return index;
181             }
182             index++;
183         }
184         return -1;
185     }
186
187     /* (non-Javadoc)
188      * @see org.apache.activemq.kaha.impl.IndexLinkedList#getNextEntry(org.apache.activemq.kaha.impl.IndexItem)
189      */

190     public IndexItem getNextEntry(IndexItem entry){
191         return entry.next != root ? entry.next : null;
192     }
193
194     /* (non-Javadoc)
195      * @see org.apache.activemq.kaha.impl.IndexLinkedList#getPrevEntry(org.apache.activemq.kaha.impl.IndexItem)
196      */

197     public IndexItem getPrevEntry(IndexItem entry){
198         return entry.prev != root ? entry.prev : null;
199     }
200
201     /* (non-Javadoc)
202      * @see org.apache.activemq.kaha.impl.IndexLinkedList#addBefore(org.apache.activemq.kaha.impl.IndexItem, org.apache.activemq.kaha.impl.IndexItem)
203      */

204     public void addBefore(IndexItem insert,IndexItem e){
205         insert.next=e;
206         insert.prev=e.prev;
207         insert.prev.next=insert;
208         insert.next.prev=insert;
209         size++;
210     }
211
212     /* (non-Javadoc)
213      * @see org.apache.activemq.kaha.impl.IndexLinkedList#remove(org.apache.activemq.kaha.impl.IndexItem)
214      */

215     public void remove(IndexItem e){
216         if(e==root)
217             return;
218         e.prev.next=e.next;
219         e.next.prev=e.prev;
220         size--;
221     }
222     
223     /**
224      *@return clone
225      */

226     public Object JavaDoc clone(){
227         IndexLinkedList clone=new VMIndexLinkedList(this.root);
228         for(IndexItem e=root.next;e!=root;e=e.next)
229             clone.add(e);
230         return clone;
231     }
232
233     public StoreEntry getEntry(StoreEntry current){
234         return current;
235     }
236     
237     /**
238      * Update the indexes of a StoreEntry
239      * @param current
240      */

241     public StoreEntry refreshEntry(StoreEntry current){
242         return current;
243     }
244 }
245
Popular Tags