KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > cursors > VMPendingMessageCursor


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

14 package org.apache.activemq.broker.region.cursors;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import org.apache.activemq.broker.region.MessageReference;
18 /**
19  * hold pending messages in a linked list (messages awaiting disptach to a consumer) cursor
20  *
21  * @version $Revision: 518437 $
22  */

23 public class VMPendingMessageCursor extends AbstractPendingMessageCursor{
24     private LinkedList JavaDoc<MessageReference> list = new LinkedList JavaDoc<MessageReference>();
25     private Iterator JavaDoc<MessageReference> iter = null;
26     private MessageReference last;
27     
28     /**
29      * @return true if there are no pending messages
30      */

31     public boolean isEmpty(){
32         return list.isEmpty();
33     }
34
35     /**
36      * reset the cursor
37      *
38      */

39     public void reset(){
40         iter = list.listIterator();
41         last=null;
42     }
43     
44     /**
45      * add message to await dispatch
46      *
47      * @param node
48      */

49     public void addMessageLast(MessageReference node){
50         node.incrementReferenceCount();
51         list.addLast(node);
52     }
53     
54     /**
55      * add message to await dispatch
56      * @param position
57      * @param node
58      */

59     public void addMessageFirst(MessageReference node){
60         node.incrementReferenceCount();
61         list.addFirst(node);
62     }
63
64
65     /**
66      * @return true if there pending messages to dispatch
67      */

68     public boolean hasNext(){
69        return iter.hasNext();
70     }
71
72     /**
73      * @return the next pending message
74      */

75     public MessageReference next(){
76         last = (MessageReference) iter.next();
77         return last;
78     }
79
80     /**
81      * remove the message at the cursor position
82      *
83      */

84     public void remove(){
85         if( last!=null ) {
86             last.decrementReferenceCount();
87         }
88         iter.remove();
89     }
90
91     /**
92      * @return the number of pending messages
93      */

94     public int size(){
95         return list.size();
96     }
97
98     /**
99      * clear all pending messages
100      *
101      */

102     public void clear(){
103         list.clear();
104     }
105     
106     public void remove(MessageReference node){
107         for(Iterator JavaDoc<MessageReference> i=list.iterator();i.hasNext();){
108             MessageReference ref=i.next();
109             if(node.getMessageId().equals(ref.getMessageId())){
110                 ref.decrementReferenceCount();
111                 i.remove();
112                 break;
113             }
114         }
115     }
116     
117     /**
118      * Page in a restricted number of messages
119      * @param maxItems
120      * @return a list of paged in messages
121      */

122     public LinkedList JavaDoc<MessageReference> pageInList(int maxItems) {
123         return list;
124     }
125 }
126
Popular Tags