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 */
017package org.apache.activemq.kaha.impl.container;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.HashSet;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Set;
025
026/**
027 * Set of Map.Entry objects for a container
028 * 
029 * 
030 */
031public class ContainerEntrySet extends ContainerCollectionSupport implements Set {
032    ContainerEntrySet(MapContainerImpl container) {
033        super(container);
034    }
035
036    public boolean contains(Object o) {
037        return container.entrySet().contains(o);
038    }
039
040    public Iterator iterator() {
041        return new ContainerEntrySetIterator(container, buildEntrySet().iterator());
042    }
043
044    public Object[] toArray() {
045        return buildEntrySet().toArray();
046    }
047
048    public Object[] toArray(Object[] a) {
049        return buildEntrySet().toArray(a);
050    }
051
052    public boolean add(Object o) {
053        throw new UnsupportedOperationException("Cannot add here");
054    }
055
056    public boolean remove(Object o) {
057        boolean result = false;
058        if (buildEntrySet().remove(o)) {
059            ContainerMapEntry entry = (ContainerMapEntry)o;
060            container.remove(entry.getKey());
061        }
062        return result;
063    }
064
065    public boolean containsAll(Collection c) {
066        return buildEntrySet().containsAll(c);
067    }
068
069    public boolean addAll(Collection c) {
070        throw new UnsupportedOperationException("Cannot add here");
071    }
072
073    public boolean retainAll(Collection c) {
074        List<Object> tmpList = new ArrayList<Object>();
075        for (Iterator i = c.iterator(); i.hasNext();) {
076            Object o = i.next();
077            if (!contains(o)) {
078                tmpList.add(o);
079            }
080        }
081        boolean result = false;
082        for (Iterator<Object> i = tmpList.iterator(); i.hasNext();) {
083            result |= remove(i.next());
084        }
085        return result;
086    }
087
088    public boolean removeAll(Collection c) {
089        boolean result = true;
090        for (Iterator i = c.iterator(); i.hasNext();) {
091            if (!remove(i.next())) {
092                result = false;
093            }
094        }
095        return result;
096    }
097
098    public void clear() {
099        container.clear();
100    }
101
102    protected Set<ContainerMapEntry> buildEntrySet() {
103        Set<ContainerMapEntry> set = new HashSet<ContainerMapEntry>();
104        for (Iterator i = container.keySet().iterator(); i.hasNext();) {
105            ContainerMapEntry entry = new ContainerMapEntry(container, i.next());
106            set.add(entry);
107        }
108        return set;
109    }
110}