KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.helper.*;
27 import oracle.toplink.essentials.internal.sessions.AbstractSession;
28
29 /**
30  * <p><b>Purpose</b>: A CollectionContainerPolicy is ContainerPolicy whose container class
31  * implements the Collection interface.
32  * <p>
33  * <p><b>Responsibilities</b>:
34  * Provide the functionality to operate on an instance of a Collection.
35  *
36  * @see ContainerPolicy
37  * @see MapContainerPolicy
38  */

39 public class CollectionContainerPolicy extends InterfaceContainerPolicy {
40
41     /**
42      * INTERNAL:
43      * Construct a new policy.
44      */

45     public CollectionContainerPolicy() {
46         super();
47     }
48
49     /**
50      * INTERNAL:
51      * Construct a new policy for the specified class.
52      */

53     public CollectionContainerPolicy(Class JavaDoc containerClass) {
54         super(containerClass);
55     }
56
57     /**
58      * INTERNAL:
59      * Construct a new policy for the specified class name.
60      */

61     public CollectionContainerPolicy(String JavaDoc containerClassName) {
62         super(containerClassName);
63     }
64
65     /**
66      * INTERNAL:
67      * Add element into a container which implements the Collection interface.
68      *
69      * @param element java.lang.Object
70      * @param container java.lang.Object
71      * @return boolean indicating whether the container changed
72      */

73     public boolean addInto(Object JavaDoc key, Object JavaDoc element, Object JavaDoc container) {
74         try {
75             return ((Collection)container).add(element);
76         } catch (ClassCastException JavaDoc ex1) {
77             throw QueryException.cannotAddElement(element, container, ex1);
78         } catch (IllegalArgumentException JavaDoc ex2) {
79             throw QueryException.cannotAddElement(element, container, ex2);
80         } catch (UnsupportedOperationException JavaDoc ex3) {
81             throw QueryException.cannotAddElement(element, container, ex3);
82         }
83     }
84
85     /**
86      * INTERNAL:
87      * Return a container populated with the contents of the specified Vector.
88      */

89     public Object JavaDoc buildContainerFromVector(Vector vector, AbstractSession session) {
90         if ((getContainerClass() == vector.getClass()) && (!hasElementDescriptor())) {
91             return vector;
92         } else {
93             return super.buildContainerFromVector(vector, session);
94         }
95     }
96
97     /**
98      * INTERNAL:
99      * Remove all the elements from container.
100      *
101      * @param container java.lang.Object
102      */

103     public void clear(Object JavaDoc container) {
104         try {
105             ((Collection)container).clear();
106         } catch (UnsupportedOperationException JavaDoc ex) {
107             throw QueryException.methodNotValid(container, "clear()");
108         }
109     }
110
111     /**
112      * INTERNAL:
113      * Return the true if element exists in container.
114      *
115      * @param element java.lang.Object
116      * @param container java.lang.Object
117      * @return boolean true if container 'contains' element
118      */

119     protected boolean contains(Object JavaDoc element, Object JavaDoc container) {
120         return ((Collection)container).contains(element);
121     }
122
123     public Class JavaDoc getInterfaceType() {
124         return ClassConstants.Collection_Class;
125     }
126
127     /**
128      * INTERNAL:
129      * Return whether the collection has order.
130      * SortedSets cannot be indexed, but they are order-sensitive.
131      */

132     public boolean hasOrder() {
133         return Helper.classImplementsInterface(this.getContainerClass(), ClassConstants.SortedSet_Class);
134     }
135
136     /**
137      * INTERNAL:
138      * Validate the container type.
139      */

140     public boolean isValidContainer(Object JavaDoc container) {
141         // PERF: Use instanceof which is inlined, not isAssignable which is very inefficent.
142
return container instanceof Collection;
143     }
144
145     public boolean isCollectionPolicy() {
146         return true;
147     }
148
149     /**
150      * INTERNAL:
151      * Return an iterator for the given container.
152      *
153      * @param container java.lang.Object
154      * @return java.util.Enumeration/java.util.Iterator
155      */

156     public Object JavaDoc iteratorFor(Object JavaDoc container) {
157         return ((Collection)container).iterator();
158     }
159
160     /**
161      * INTERNAL:
162      * Remove element from container which implements the Collection interface.
163      *
164      * @param key java.lang.Object This param represents the key that would be used by this object in a map, may be null
165      * @param element java.lang.Object
166      * @param container java.lang.Object
167      */

168     protected boolean removeFrom(Object JavaDoc key, Object JavaDoc element, Object JavaDoc container) {
169         try {
170             return ((Collection)container).remove(element);
171         } catch (UnsupportedOperationException JavaDoc ex) {
172             throw QueryException.methodNotValid(element, "remove(Object element)");
173         }
174     }
175
176     /**
177      * INTERNAL:
178      * Return the size of container.
179      *
180      * @param anObject java.lang.Object
181      * @return int The size of the container.
182      */

183     public int sizeFor(Object JavaDoc container) {
184         return ((Collection)container).size();
185     }
186 }
187
Popular Tags