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.ArrayList;
020    import java.util.Collection;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Set;
024    
025    import org.apache.activemq.kaha.impl.index.IndexItem;
026    
027    /**
028     * A Set of keys for the container
029     * 
030     * 
031     */
032    public class ContainerKeySet extends ContainerCollectionSupport implements Set {
033    
034        ContainerKeySet(MapContainerImpl container) {
035            super(container);
036        }
037    
038        public boolean contains(Object o) {
039            return container.containsKey(o);
040        }
041    
042        public Iterator iterator() {
043            return new ContainerKeySetIterator(container);
044        }
045    
046        public Object[] toArray() {
047            List<Object> list = new ArrayList<Object>();
048            IndexItem item = container.getInternalList().getRoot();
049            while ((item = container.getInternalList().getNextEntry(item)) != null) {
050                list.add(container.getKey(item));
051            }
052            return list.toArray();
053        }
054    
055        public Object[] toArray(Object[] a) {
056            List<Object> list = new ArrayList<Object>();
057            IndexItem item = container.getInternalList().getRoot();
058            while ((item = container.getInternalList().getNextEntry(item)) != null) {
059                list.add(container.getKey(item));
060            }
061            return list.toArray(a);
062        }
063    
064        public boolean add(Object o) {
065            throw new UnsupportedOperationException("Cannot add here");
066        }
067    
068        public boolean remove(Object o) {
069            return container.remove(o) != null;
070        }
071    
072        public boolean containsAll(Collection c) {
073            for (Object key : c) {
074                if (!container.containsKey(key)) {
075                    return false;
076                }
077            }
078            return true;
079        }
080    
081        public boolean addAll(Collection c) {
082            throw new UnsupportedOperationException("Cannot add here");
083        }
084    
085        public boolean retainAll(Collection c) {
086            List<Object> tmpList = new ArrayList<Object>();
087            for (Iterator i = c.iterator(); i.hasNext();) {
088                Object o = i.next();
089                if (!contains(o)) {
090                    tmpList.add(o);
091                }
092            }
093            for (Iterator<Object> i = tmpList.iterator(); i.hasNext();) {
094                remove(i.next());
095            }
096            return !tmpList.isEmpty();
097        }
098    
099        public boolean removeAll(Collection c) {
100            boolean result = true;
101            for (Iterator i = c.iterator(); i.hasNext();) {
102                if (!remove(i.next())) {
103                    result = false;
104                }
105            }
106            return result;
107        }
108    
109        public void clear() {
110            container.clear();
111        }
112    
113        public String toString() {
114            StringBuffer result = new StringBuffer(32);
115            result.append("ContainerKeySet[");
116            IndexItem item = container.getInternalList().getRoot();
117            while ((item = container.getInternalList().getNextEntry(item)) != null) {
118                result.append(container.getKey(item));
119                result.append(",");
120            }
121            result.append("]");
122            return result.toString();
123        }
124    }