KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > queryframework > IndirectListContainerPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.queryframework;
23
24 import java.util.*;
25 import oracle.toplink.essentials.exceptions.*;
26 import oracle.toplink.essentials.indirection.*;
27 import oracle.toplink.essentials.internal.helper.ClassConstants;
28
29 /**
30  * A ContainerPolicy for jdk1.1 only (IndirectList implements Collection
31  * in jdk1.2; so the CollectionContainerPolicy can be used.)
32  *
33  * @see ContainerPolicy
34  * @author Big Country
35  * @since TOPLink/Java 2.5
36  */

37 public class IndirectListContainerPolicy extends InterfaceContainerPolicy {
38
39     /**
40      * INTERNAL:
41      * Construct a new policy.
42      */

43     public IndirectListContainerPolicy() {
44         super();
45     }
46
47     /**
48      * INTERNAL:
49      * Construct a new policy for the specified class.
50      * @param containerClass java.lang.Class
51      */

52     public IndirectListContainerPolicy(Class JavaDoc containerClass) {
53         super(containerClass);
54         DescriptorException.invalidContainerPolicy(this, containerClass);
55     }
56
57     /**
58      * INTERNAL:
59      * Add element into the container.
60      *
61      * @param element java.lang.Object
62      * @param container java.lang.Object
63      * @return boolean indicating whether the container changed
64      */

65     protected boolean addInto(Object JavaDoc key, Object JavaDoc element, Object JavaDoc container) {
66         try {
67             ((IndirectList)container).addElement(element);
68             return true;
69         } catch (ClassCastException JavaDoc ex) {
70             throw QueryException.cannotAddElement(element, container, ex);
71         }
72     }
73
74     /**
75      * INTERNAL:
76      * Add element into a container which implements the Collection interface.
77      *
78      * @param element java.lang.Object
79      * @param container java.lang.Object
80      * @return boolean indicating whether the container changed
81      */

82     public void addIntoWithOrder(Vector indexes, Hashtable elements, Object JavaDoc container) {
83         Object JavaDoc object = null;
84         try {
85             Enumeration indexEnum = indexes.elements();
86             while (indexEnum.hasMoreElements()) {
87                 Integer JavaDoc index = (Integer JavaDoc)indexEnum.nextElement();
88                 object = elements.get(index);
89                 if (index.intValue() >= (sizeFor(container) - 1)) {
90                     ((IndirectList)container).addElement(object);
91                 } else {
92                     ((IndirectList)container).setElementAt(object, index.intValue());
93                 }
94             }
95         } catch (ClassCastException JavaDoc ex1) {
96             throw QueryException.cannotAddElement(object, container, ex1);
97         }
98     }
99
100     /**
101      * INTERNAL:
102      * Remove all the elements from container.
103      *
104      * @param container java.lang.Object
105      */

106     public void clear(Object JavaDoc container) {
107         ((IndirectList)container).clear();
108     }
109
110     /**
111      * INTERNAL:
112      * Return the true if element exists in container.
113      *
114      * @param element java.lang.Object
115      * @param container java.lang.Object
116      * @return boolean true if container 'contains' element
117      */

118     protected boolean contains(Object JavaDoc element, Object JavaDoc container) {
119         return ((IndirectList)container).contains(element);
120     }
121
122     public Class JavaDoc getInterfaceType() {
123         return ClassConstants.IndirectList_Class;
124     }
125
126     /**
127      * INTERNAL:
128      * Return whether the iterator has more objects,
129      *
130      * @param iterator java.lang.Object
131      * @return boolean true if iterator has more objects
132      */

133     public boolean hasNext(Object JavaDoc iterator) {
134         return ((Enumeration)iterator).hasMoreElements();
135     }
136
137     /**
138      * INTERNAL:
139      * Returns true if the collection has order
140      *
141      * @see ContainerPolicy#iteratorFor(java.lang.Object)
142      */

143     public boolean hasOrder() {
144         return true;
145     }
146
147     /**
148      * INTERNAL:
149      * Return an Iterator for the given container.
150      *
151      * @param container java.lang.Object
152      * @return java.lang.Object the iterator
153      */

154     public Object JavaDoc iteratorFor(Object JavaDoc container) {
155         return ((IndirectList)container).elements();
156     }
157
158     /**
159      * INTERNAL:
160      * Return the next object on the queue.
161      * Valid for some subclasses only.
162      *
163      * @param iterator java.lang.Object
164      * @return java.lang.Object the next object in the queue
165      */

166     protected Object JavaDoc next(Object JavaDoc iterator) {
167         return ((Enumeration)iterator).nextElement();
168     }
169
170     /**
171      * INTERNAL:
172      * Remove element from container which implements the Collection interface.
173      *
174      * @param element java.lang.Object
175      * @param container java.lang.Object
176      */

177     protected boolean removeFrom(Object JavaDoc key, Object JavaDoc element, Object JavaDoc container) {
178         return ((IndirectList)container).removeElement(element);
179     }
180
181     /**
182      * INTERNAL:
183      * Remove elements from this container starting with this index
184      *
185      * @param beginIndex int the point to start deleting values from the collection
186      * @param container java.lang.Object
187      * @return boolean indicating whether the container changed
188      */

189     public void removeFromWithOrder(int beginIndex, Object JavaDoc container) {
190         int size = sizeFor(container) - 1;
191         try {
192             for (; size >= beginIndex; --size) {
193                 ((IndirectList)container).removeElementAt(size);
194             }
195         } catch (ClassCastException JavaDoc ex1) {
196             throw QueryException.cannotRemoveFromContainer(new Integer JavaDoc(size), container, this);
197         }
198     }
199
200     /**
201      * INTERNAL:
202      * Return the size of container.
203      *
204      * @param anObject java.lang.Object
205      * @return int The size of the container.
206      */

207     public int sizeFor(Object JavaDoc container) {
208         return ((IndirectList)container).size();
209     }
210 }
211
Popular Tags