KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.activemq.kaha.impl.index.IndexItem;
27 import org.apache.activemq.kaha.impl.index.IndexLinkedList;
28
29 /**
30 * Values collection for the MapContainer
31 *
32 * @version $Revision: 1.2 $
33 */

34 class ContainerValueCollection extends ContainerCollectionSupport implements Collection JavaDoc{
35     
36    
37     ContainerValueCollection(MapContainerImpl container){
38         super(container);
39     }
40     
41     
42     
43     public boolean contains(Object JavaDoc o){
44         return container.containsValue(o);
45     }
46
47     
48     public Iterator JavaDoc iterator(){
49         IndexLinkedList list=container.getItemList();
50         return new ContainerValueCollectionIterator(container,list,list.getRoot());
51     }
52
53    
54     public Object JavaDoc[] toArray(){
55         Object JavaDoc[] result = null;
56         IndexLinkedList list = container.getItemList();
57         synchronized(list){
58             result = new Object JavaDoc[list.size()];
59             IndexItem item = list.getFirst();
60             int count = 0;
61             while (item != null){
62                 Object JavaDoc value=container.getValue(item);
63                 result[count++] = value;
64                 
65                 item = list.getNextEntry(item);
66             }
67            
68             
69         }
70         return result;
71     }
72
73     public Object JavaDoc[] toArray(Object JavaDoc[] result){
74         IndexLinkedList list=container.getItemList();
75         synchronized(list){
76             if(result.length<=list.size()){
77                 IndexItem item = list.getFirst();
78                 int count = 0;
79                 while (item != null){
80                     Object JavaDoc value=container.getValue(item);
81                     result[count++] = value;
82                     
83                     item = list.getNextEntry(item);
84                 }
85             }
86         }
87         return result;
88     }
89
90     
91     public boolean add(Object JavaDoc o){
92         throw new UnsupportedOperationException JavaDoc("Can't add an object here");
93     }
94
95    
96     public boolean remove(Object JavaDoc o){
97         return container.removeValue(o);
98     }
99
100     
101     public boolean containsAll(Collection JavaDoc c){
102         boolean result = !c.isEmpty();
103         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ){
104             if (!contains(i.next())){
105                 result = false;
106                 break;
107             }
108         }
109         return result;
110     }
111
112    
113     public boolean addAll(Collection JavaDoc c){
114         throw new UnsupportedOperationException JavaDoc("Can't add everything here!");
115     }
116
117     
118     public boolean removeAll(Collection JavaDoc c){
119         boolean result = true;
120         for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ){
121             Object JavaDoc obj = i.next();
122                 result&=remove(obj);
123         }
124         return result;
125     }
126
127     
128     public boolean retainAll(Collection JavaDoc c){
129        List JavaDoc tmpList = new ArrayList JavaDoc();
130        for (Iterator JavaDoc i = c.iterator(); i.hasNext(); ){
131            Object JavaDoc o = i.next();
132            if (!contains(o)){
133                tmpList.add(o);
134            }
135        }
136        for(Iterator JavaDoc i = tmpList.iterator(); i.hasNext();){
137            remove(i.next());
138        }
139        return !tmpList.isEmpty();
140     }
141
142    
143     public void clear(){
144      container.clear();
145     }
146 }
147
Popular Tags