001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.kaha.impl.container;
018    
019    import java.util.ListIterator;
020    import org.apache.activemq.kaha.StoreEntry;
021    import org.apache.activemq.kaha.impl.index.IndexItem;
022    import org.apache.activemq.kaha.impl.index.IndexLinkedList;
023    
024    /**
025     * 
026     */
027    public class ContainerListIterator extends ContainerValueCollectionIterator implements ListIterator {
028    
029        protected ContainerListIterator(ListContainerImpl container, IndexLinkedList list, IndexItem start) {
030            super(container, list, start);
031        }
032    
033        /*
034         * (non-Javadoc)
035         * 
036         * @see java.util.ListIterator#hasPrevious()
037         */
038        public boolean hasPrevious() {
039            synchronized (container) {
040                nextItem = (IndexItem)list.refreshEntry(nextItem);
041                return list.getPrevEntry(nextItem) != null;
042            }
043        }
044    
045        /*
046         * (non-Javadoc)
047         * 
048         * @see java.util.ListIterator#previous()
049         */
050        public Object previous() {
051            synchronized (container) {
052                nextItem = (IndexItem)list.refreshEntry(nextItem);
053                nextItem = list.getPrevEntry(nextItem);
054                return nextItem != null ? container.getValue(nextItem) : null;
055            }
056        }
057    
058        /*
059         * (non-Javadoc)
060         * 
061         * @see java.util.ListIterator#nextIndex()
062         */
063        public int nextIndex() {
064            int result = -1;
065            if (nextItem != null) {
066                synchronized (container) {
067                    nextItem = (IndexItem)list.refreshEntry(nextItem);
068                    StoreEntry next = list.getNextEntry(nextItem);
069                    if (next != null) {
070                        result = container.getInternalList().indexOf(next);
071                    }
072                }
073            }
074            return result;
075        }
076    
077        /*
078         * (non-Javadoc)
079         * 
080         * @see java.util.ListIterator#previousIndex()
081         */
082        public int previousIndex() {
083            int result = -1;
084            if (nextItem != null) {
085                synchronized (container) {
086                    nextItem = (IndexItem)list.refreshEntry(nextItem);
087                    StoreEntry prev = list.getPrevEntry(nextItem);
088                    if (prev != null) {
089                        result = container.getInternalList().indexOf(prev);
090                    }
091                }
092            }
093            return result;
094        }
095    
096        /*
097         * (non-Javadoc)
098         * 
099         * @see java.util.ListIterator#set(E)
100         */
101        public void set(Object o) {
102            IndexItem item = ((ListContainerImpl)container).internalSet(previousIndex() + 1, o);
103            nextItem = item;
104        }
105    
106        /*
107         * (non-Javadoc)
108         * 
109         * @see java.util.ListIterator#add(E)
110         */
111        public void add(Object o) {
112            IndexItem item = ((ListContainerImpl)container).internalAdd(previousIndex() + 1, o);
113            nextItem = item;
114        }
115    }