KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > container > ContainerKeySet


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl.container;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.apache.activemq.kaha.impl.index.IndexItem;
26 import org.apache.activemq.kaha.impl.index.IndexLinkedList;
27
28 /**
29 * A Set of keys for the container
30 *
31 * @version $Revision: 1.2 $
32 */

33 public class ContainerKeySet extends ContainerCollectionSupport implements Set JavaDoc{
34   
35     
36     ContainerKeySet(MapContainerImpl container){
37         super(container);
38     }
39     
40     
41     public boolean contains(Object JavaDoc o){
42         return container.containsKey(o);
43     }
44
45     public Iterator JavaDoc iterator(){
46         return new ContainerKeySetIterator(container);
47     }
48
49     public Object JavaDoc[] toArray(){
50         List JavaDoc list = new ArrayList JavaDoc();
51         IndexItem item = container.getInternalList().getRoot();
52         while ((item = container.getInternalList().getNextEntry(item)) != null) {
53             list.add(container.getKey(item));
54         }
55         return list.toArray();
56     }
57
58     public Object JavaDoc[] toArray(Object JavaDoc[] a){
59         List JavaDoc list = new ArrayList JavaDoc();
60         IndexItem item = container.getInternalList().getRoot();
61         while ((item = container.getInternalList().getNextEntry(item)) != null) {
62             list.add(container.getKey(item));
63         }
64         return list.toArray(a);
65     }
66
67     public boolean add(Object JavaDoc o){
68         throw new UnsupportedOperationException JavaDoc("Cannot add here");
69     }
70
71     public boolean remove(Object JavaDoc o){
72        return container.remove(o) != null;
73     }
74
75     public boolean containsAll(Collection JavaDoc c){
76         boolean result = true;
77         for (Object JavaDoc key:c) {
78             if (!(result&=container.containsKey(key))) {
79                 break;
80             }
81         }
82        return result;
83     }
84
85     public boolean addAll(Collection JavaDoc c){
86         throw new UnsupportedOperationException JavaDoc("Cannot add here");
87     }
88
89     public boolean retainAll(Collection JavaDoc c){
90         List JavaDoc tmpList = new ArrayList JavaDoc();
91         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ){
92             Object JavaDoc o = i.next();
93             if (!contains(o)){
94                 tmpList.add(o);
95             }
96         }
97         for(Iterator JavaDoc i = tmpList.iterator(); i.hasNext();){
98             remove(i.next());
99         }
100         return !tmpList.isEmpty();
101     }
102
103     public boolean removeAll(Collection JavaDoc c){
104         boolean result = true;
105         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ){
106             if (!remove(i.next())){
107                 result = false;
108             }
109         }
110         return result;
111     }
112
113     public void clear(){
114       container.clear();
115     }
116     
117     public String JavaDoc toString() {
118         String JavaDoc result ="ContainerKeySet[";
119         IndexItem item = container.getInternalList().getRoot();
120         while ((item = container.getInternalList().getNextEntry(item)) != null) {
121             result += container.getKey(item);
122             result += ",";
123         }
124         result +="]";
125         return result;
126     }
127 }
128
Popular Tags