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.Iterator;
020    import org.apache.activemq.kaha.impl.index.IndexItem;
021    import org.apache.activemq.kaha.impl.index.IndexLinkedList;
022    
023    /**
024     * Iterator for the set of keys for a container
025     * 
026     * 
027     */
028    public class ContainerKeySetIterator implements Iterator {
029        
030        protected IndexItem nextItem;
031        protected IndexItem currentItem;
032    
033        private MapContainerImpl container;
034        private IndexLinkedList list;
035    
036        ContainerKeySetIterator(MapContainerImpl container) {
037            this.container = container;
038            this.list = container.getInternalList();
039            this.currentItem = list.getRoot();
040            this.nextItem = list.getNextEntry(currentItem);
041        }
042    
043        public boolean hasNext() {
044            return nextItem != null;
045        }
046    
047        public Object next() {
048            currentItem = nextItem;
049            Object result = container.getKey(nextItem);
050            nextItem = list.getNextEntry(nextItem);
051            return result;
052        }
053    
054        public void remove() {
055            if (currentItem != null) {
056                container.remove(currentItem);
057                if (nextItem != null) {
058                    list.refreshEntry(nextItem);
059                }
060            }
061        }
062    }