/**
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.apache.activemq.broker.region.cursors;
import java.util.Iterator;
import java.util.LinkedList;
import org.apache.activemq.broker.region.MessageReference;
/**
 * hold pending messages in a linked list (messages awaiting disptach to a consumer) cursor
 * 
 * @version $Revision$
 */
public class VMPendingMessageCursor extends AbstractPendingMessageCursor{
    private LinkedList list = new LinkedList();
    private Iterator iter = null;
    /**
     * @return true if there are no pending messages
     */
    public boolean isEmpty(){
        return list.isEmpty();
    }

    /**
     * reset the cursor
     *
     */
    public void reset(){
        iter = list.listIterator();
    }
    
    /**
     * add message to await dispatch
     * 
     * @param node
     */
    public void addMessageLast(MessageReference node){
        list.addLast(node);
    }
    
    /**
     * add message to await dispatch
     * @param position 
     * @param node
     */
    public void addMessageFirst(MessageReference node){
        list.addFirst(node);
    }


    /**
     * @return true if there pending messages to dispatch
     */
    public boolean hasNext(){
       return iter.hasNext();
    }

    /**
     * @return the next pending message
     */
    public MessageReference next(){
        return (MessageReference) iter.next();
    }

    /**
     * remove the message at the cursor position
     * 
     */
    public void remove(){
        iter.remove();
    }

    /**
     * @return the number of pending messages
     */
    public int size(){
        return list.size();
    }

    /**
     * clear all pending messages
     * 
     */
    public void clear(){
        list.clear();
    }
    
    public void remove(MessageReference node) {
        boolean matched = false;
        int size = list.size();
        for (Iterator i = list.iterator();i.hasNext();) {
            MessageReference ref = (MessageReference)i.next();
                System.err.println("MATCHIG " + node.getMessageId() + " AGAINST " + ref.getMessageId());
                if(node.getMessageId().equals(ref.getMessageId())){
                    i.remove();
                    matched = true;
                    break;
                }
        }
        System.err.println("REMOVED " + node.getMessageId() + "  = " + matched + " presize = " + size + " size now = " + list.size());
    }
}

@@ -2553,76 +2553,10 @@
 ode)
- 
 %7B%0A
-        boolean matched = false;%0A        int size = list.size();%0A
     
@@ -2562,17 +2562,16 @@
      for
- 
 (Iterato
@@ -2573,19 +2573,17 @@
 erator i
- = 
+=
 list.ite
@@ -2603,17 +2603,16 @@
 Next();)
- 
 %7B%0A      
@@ -2637,19 +2637,17 @@
 ence ref
- = 
+=
 (Message
@@ -2670,117 +2670,8 @@
 ();%0A
-                System.err.println(%22MATCHIG %22 + node.getMessageId() + %22 AGAINST %22 + ref.getMessageId());%0A    
     
@@ -2746,20 +2746,16 @@
         
- 
-   
 i.remove
@@ -2762,48 +2762,8 @@
 ();%0A
-                    matched = true;%0A    
     
@@ -2789,28 +2789,24 @@
             
-   
- 
 %7D%0A        %7D%0A
@@ -2809,143 +2809,8 @@
   %7D%0A
-        System.err.println(%22REMOVED %22 + node.getMessageId() + %22  = %22 + matched + %22 presize = %22 + size + %22 size now = %22 + list.size());%0A
     

remove println messages from remove()