KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class DirectMapContainerPolicy extends InterfaceContainerPolicy {
43     protected DatabaseField keyField;
44     protected DatabaseField valueField;
45     protected Converter keyConverter;
46     protected Converter valueConverter;
47
48     /**
49      * INTERNAL:
50      * Construct a new policy.
51      */

52     public DirectMapContainerPolicy() {
53         super();
54     }
55
56     /**
57      * INTERNAL:
58      * Construct a new policy for the specified class.
59      */

60     public DirectMapContainerPolicy(Class JavaDoc containerClass) {
61         super(containerClass);
62     }
63
64     /**
65      * INTERNAL:
66      * Add key, value pair into container which implements the Map interface.
67      */

68     public boolean addInto(Object JavaDoc key, Object JavaDoc value, Object JavaDoc container, AbstractSession session) {
69         try {
70             ((Map)container).put(key, value);
71         } catch (ClassCastException JavaDoc ex1) {
72             throw QueryException.cannotAddElement(key, container, ex1);
73         }
74         return true;
75     }
76
77     /**
78      * INTERNAL:
79      * Add element into container which implements the Map interface. Not used since key is not obtained from the object
80      */

81     public boolean addInto(Object JavaDoc element, Object JavaDoc container, AbstractSession session) {
82         throw ValidationException.operationNotSupported("addInto(Object element, Object container, Session session)");
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         Map container = (Map)containerInstance(vector.size());
91         AbstractRecord row;
92
93         for (Enumeration e = vector.elements(); e.hasMoreElements();) {
94             row = (AbstractRecord)e.nextElement();
95             Object JavaDoc key = row.get(keyField);
96             Object JavaDoc value = row.get(valueField);
97             if (getKeyConverter() != null) {
98                 key = getKeyConverter().convertDataValueToObjectValue(key, session);
99             }
100             if (getValueConverter() != null) {
101                 value = getValueConverter().convertDataValueToObjectValue(value, session);
102             }
103             if (key != null) {
104                 container.put(key, value);
105             }
106         }
107         return container;
108     }
109
110     /**
111      * INTERNAL:
112      * Remove all the elements from container.
113      */

114     public void clear(Object JavaDoc container) {
115         try {
116             ((Map)container).clear();
117         } catch (UnsupportedOperationException JavaDoc ex) {
118             throw QueryException.methodNotValid(container, "clear()");
119         }
120     }
121
122     /**
123      * INTERNAL:
124      * Return true if keys are the same. False otherwise
125      */

126     public boolean compareContainers(Object JavaDoc firstObjectMap, Object JavaDoc secondObjectMap) {
127         if (sizeFor(firstObjectMap) != sizeFor(secondObjectMap)) {
128             return false;
129         }
130
131         for (Object JavaDoc firstIterator = iteratorFor(firstObjectMap); hasNext(firstIterator);) {
132             Object JavaDoc key = next(firstIterator);
133             if (!((Map)firstObjectMap).get(key).equals(((Map)secondObjectMap).get(key))) {
134                 return false;
135             }
136         }
137         return true;
138     }
139
140     /**
141      * INTERNAL:
142      * Return true if keys are the same in the source as the backup. False otherwise
143      * in the case of readonly compare against the original
144      */

145     public boolean compareKeys(Object JavaDoc sourceValue, AbstractSession session) {
146         Object JavaDoc backUpVersion = null;
147
148         //CR 4172
149
if (((UnitOfWorkImpl)session).isClassReadOnly(sourceValue.getClass())) {
150             backUpVersion = ((UnitOfWorkImpl)session).getOriginalVersionOfObject(sourceValue);
151         } else {
152             backUpVersion = ((UnitOfWorkImpl)session).getBackupClone(sourceValue);
153         }
154         return (keyFrom(backUpVersion, session).equals(keyFrom(sourceValue, session)));
155     }
156
157     /**
158      * INTERNAL:
159      * Return the true if element exists in container.
160      * @return boolean true if container 'contains' element
161      */

162     protected boolean contains(Object JavaDoc element, Object JavaDoc container) {
163         return ((Map)container).containsValue(element);
164     }
165
166     public Class JavaDoc getInterfaceType() {
167         return ClassConstants.Map_Class;
168     }
169
170     public boolean isDirectMapPolicy() {
171         return true;
172     }
173
174     /**
175      * INTERNAL:
176      * Return an Iterator for the given container.
177      */

178     public Object JavaDoc iteratorFor(Object JavaDoc container) {
179         if (((Map)container).keySet() == null) {
180             return null;
181         }
182         return ((Map)container).keySet().iterator();
183     }
184
185     /**
186      * INTERNAL:
187      * Return an Iterator for the given container.
188      */

189     public Object JavaDoc iteratorForValue(Object JavaDoc container) {
190         if (((Map)container).values() == null) {
191             return null;
192         }
193         return ((Map)container).values().iterator();
194     }
195
196     /**
197      * INTERNAL:
198      * Remove element from container which implements the Map interface.
199      */

200     public boolean removeFrom(Object JavaDoc key, Object JavaDoc element, Object JavaDoc container, AbstractSession session) {
201         try {
202             Object JavaDoc returnValue = null;
203             if (key != null) {
204                 returnValue = ((Map)container).remove(key);
205             } else {
206                 returnValue = ((Map)container).remove(keyFrom(element, session));
207             }
208             if (returnValue == null) {
209                 return false;
210             } else {
211                 return true;
212             }
213         } catch (UnsupportedOperationException JavaDoc ex) {
214             throw QueryException.methodNotValid(container, "remove(Object element)");
215         }
216     }
217
218     /**
219      * INTERNAL:
220      * Remove element from container which implements the Map interface.
221      */

222     public boolean removeFromWithIdentity(Object JavaDoc element, Object JavaDoc container, AbstractSession session) {
223         boolean found = false;
224         Vector knownKeys = new Vector(1);
225         try {
226             Iterator iterator = ((Map)container).keySet().iterator();
227             while (iterator.hasNext()) {
228                 Object JavaDoc key = iterator.next();
229                 if (((Map)container).get(key) == element) {
230                     knownKeys.addElement(key);
231                     found = true;
232                 }
233             }
234             if (found) {
235                 for (int index = 0; index < knownKeys.size(); ++index) {
236                     ((Map)container).remove(knownKeys.elementAt(index));
237                 }
238             }
239             return found;
240         } catch (UnsupportedOperationException JavaDoc ex) {
241             throw QueryException.methodNotValid(container, "remove(Object element)");
242         }
243     }
244
245     public void setKeyField(DatabaseField field) {
246         keyField = field;
247     }
248
249     public void setValueField(DatabaseField field) {
250         valueField = field;
251     }
252
253     /**
254      * INTERNAL:
255      * Return the size of container.
256      */

257     public int sizeFor(Object JavaDoc container) {
258         return ((Map)container).size();
259     }
260
261     /**
262      * INTERNAL:
263      * If the key has changed, remove the element and add it back into the target.
264      */

265     public void validateElementAndRehashIfRequired(Object JavaDoc sourceValue, Object JavaDoc targetMap, AbstractSession session, Object JavaDoc targetVersionOfSource) {
266         if (session.isUnitOfWork()) {
267             //this must be a unit of work at this point
268
Object JavaDoc backupValue = ((UnitOfWorkImpl)session).getBackupClone(sourceValue);
269             if (!keyFrom(backupValue, session).equals(keyFrom(sourceValue, session))) {
270                 //the key has been changed. Remove the old value and put back the new one
271
removeFrom(backupValue, targetMap, session);
272                 addInto(targetVersionOfSource, targetMap, session);
273             }
274         }
275     }
276
277     /**
278      * INTERNAL:
279      * Validate the container type.
280      */

281     public boolean isValidContainer(Object JavaDoc container) {
282         // PERF: Use instanceof which is inlined, not isAssignable which is very inefficent.
283
return container instanceof Map;
284     }
285
286     /**
287      * INTERNAL:
288      * Return an value of the key from container
289      */

290     public Object JavaDoc valueFromKey(Object JavaDoc key, Object JavaDoc container) {
291         return ((Map)container).get(key);
292     }
293
294     public Converter getKeyConverter() {
295         return keyConverter;
296     }
297
298     public void setKeyConverter(Converter keyConverter) {
299         this.keyConverter = keyConverter;
300     }
301
302     public void setValueConverter(Converter valueConverter) {
303         this.valueConverter = valueConverter;
304     }
305
306     public Converter getValueConverter() {
307         return valueConverter;
308     }
309 }
310
Popular Tags