All Downloads are FREE. Search and download functionalities are using the official Maven repository.

oracle.toplink.essentials.internal.queryframework.DirectMapContainerPolicy Maven / Gradle / Ivy

There is a newer version: 2.1-60f
Show newest version
/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the "License").  You may not use this file except 
 * in compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * glassfish/bootstrap/legal/CDDLv1.0.txt or 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html. 
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * HEADER in each file and include the License file at 
 * glassfish/bootstrap/legal/CDDLv1.0.txt.  If applicable, 
 * add the following below this CDDL HEADER, with the 
 * fields enclosed by brackets "[]" replaced with your 
 * own identifying information: Portions Copyright [yyyy] 
 * [name of copyright owner]
 */
// Copyright (c) 1998, 2007, Oracle. All rights reserved.  
package oracle.toplink.essentials.internal.queryframework;

import java.util.*;
import oracle.toplink.essentials.internal.helper.*;
import oracle.toplink.essentials.exceptions.*;
import oracle.toplink.essentials.mappings.converters.*;
import oracle.toplink.essentials.internal.sessions.AbstractRecord;
import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
import oracle.toplink.essentials.internal.sessions.AbstractSession;

/**
 * 

Purpose: A MapContainerPolicy is ContainerPolicy whose container class * implements the Map interface. *

*

Responsibilities: * Provide the functionality to operate on an instance of a Map. * * @see ContainerPolicy * @see CollectionContainerPolicy */ public class DirectMapContainerPolicy extends InterfaceContainerPolicy { protected DatabaseField keyField; protected DatabaseField valueField; protected Converter keyConverter; protected Converter valueConverter; /** * INTERNAL: * Construct a new policy. */ public DirectMapContainerPolicy() { super(); } /** * INTERNAL: * Construct a new policy for the specified class. */ public DirectMapContainerPolicy(Class containerClass) { super(containerClass); } /** * INTERNAL: * Add key, value pair into container which implements the Map interface. */ public boolean addInto(Object key, Object value, Object container, AbstractSession session) { try { ((Map)container).put(key, value); } catch (ClassCastException ex1) { throw QueryException.cannotAddElement(key, container, ex1); } return true; } /** * INTERNAL: * Add element into container which implements the Map interface. Not used since key is not obtained from the object */ public boolean addInto(Object element, Object container, AbstractSession session) { throw ValidationException.operationNotSupported("addInto(Object element, Object container, Session session)"); } /** * INTERNAL: * Return a container populated with the contents of the specified Vector. */ public Object buildContainerFromVector(Vector vector, AbstractSession session) { Map container = (Map)containerInstance(vector.size()); AbstractRecord row; for (Enumeration e = vector.elements(); e.hasMoreElements();) { row = (AbstractRecord)e.nextElement(); Object key = row.get(keyField); Object value = row.get(valueField); if (getKeyConverter() != null) { key = getKeyConverter().convertDataValueToObjectValue(key, session); } if (getValueConverter() != null) { value = getValueConverter().convertDataValueToObjectValue(value, session); } if (key != null) { container.put(key, value); } } return container; } /** * INTERNAL: * Remove all the elements from container. */ public void clear(Object container) { try { ((Map)container).clear(); } catch (UnsupportedOperationException ex) { throw QueryException.methodNotValid(container, "clear()"); } } /** * INTERNAL: * Return true if keys are the same. False otherwise */ public boolean compareContainers(Object firstObjectMap, Object secondObjectMap) { if (sizeFor(firstObjectMap) != sizeFor(secondObjectMap)) { return false; } for (Object firstIterator = iteratorFor(firstObjectMap); hasNext(firstIterator);) { Object key = next(firstIterator); if (!((Map)firstObjectMap).get(key).equals(((Map)secondObjectMap).get(key))) { return false; } } return true; } /** * INTERNAL: * Return true if keys are the same in the source as the backup. False otherwise * in the case of readonly compare against the original */ public boolean compareKeys(Object sourceValue, AbstractSession session) { Object backUpVersion = null; //CR 4172 if (((UnitOfWorkImpl)session).isClassReadOnly(sourceValue.getClass())) { backUpVersion = ((UnitOfWorkImpl)session).getOriginalVersionOfObject(sourceValue); } else { backUpVersion = ((UnitOfWorkImpl)session).getBackupClone(sourceValue); } return (keyFrom(backUpVersion, session).equals(keyFrom(sourceValue, session))); } /** * INTERNAL: * Return the true if element exists in container. * @return boolean true if container 'contains' element */ protected boolean contains(Object element, Object container) { return ((Map)container).containsValue(element); } public Class getInterfaceType() { return ClassConstants.Map_Class; } public boolean isDirectMapPolicy() { return true; } /** * INTERNAL: * Return an Iterator for the given container. */ public Object iteratorFor(Object container) { if (((Map)container).keySet() == null) { return null; } return ((Map)container).keySet().iterator(); } /** * INTERNAL: * Return an Iterator for the given container. */ public Object iteratorForValue(Object container) { if (((Map)container).values() == null) { return null; } return ((Map)container).values().iterator(); } /** * INTERNAL: * Remove element from container which implements the Map interface. */ public boolean removeFrom(Object key, Object element, Object container, AbstractSession session) { try { Object returnValue = null; if (key != null) { returnValue = ((Map)container).remove(key); } else { returnValue = ((Map)container).remove(keyFrom(element, session)); } if (returnValue == null) { return false; } else { return true; } } catch (UnsupportedOperationException ex) { throw QueryException.methodNotValid(container, "remove(Object element)"); } } /** * INTERNAL: * Remove element from container which implements the Map interface. */ public boolean removeFromWithIdentity(Object element, Object container, AbstractSession session) { boolean found = false; Vector knownKeys = new Vector(1); try { Iterator iterator = ((Map)container).keySet().iterator(); while (iterator.hasNext()) { Object key = iterator.next(); if (((Map)container).get(key) == element) { knownKeys.addElement(key); found = true; } } if (found) { for (int index = 0; index < knownKeys.size(); ++index) { ((Map)container).remove(knownKeys.elementAt(index)); } } return found; } catch (UnsupportedOperationException ex) { throw QueryException.methodNotValid(container, "remove(Object element)"); } } public void setKeyField(DatabaseField field) { keyField = field; } public void setValueField(DatabaseField field) { valueField = field; } /** * INTERNAL: * Return the size of container. */ public int sizeFor(Object container) { return ((Map)container).size(); } /** * INTERNAL: * If the key has changed, remove the element and add it back into the target. */ public void validateElementAndRehashIfRequired(Object sourceValue, Object targetMap, AbstractSession session, Object targetVersionOfSource) { if (session.isUnitOfWork()) { //this must be a unit of work at this point Object backupValue = ((UnitOfWorkImpl)session).getBackupClone(sourceValue); if (!keyFrom(backupValue, session).equals(keyFrom(sourceValue, session))) { //the key has been changed. Remove the old value and put back the new one removeFrom(backupValue, targetMap, session); addInto(targetVersionOfSource, targetMap, session); } } } /** * INTERNAL: * Validate the container type. */ public boolean isValidContainer(Object container) { // PERF: Use instanceof which is inlined, not isAssignable which is very inefficent. return container instanceof Map; } /** * INTERNAL: * Return an value of the key from container */ public Object valueFromKey(Object key, Object container) { return ((Map)container).get(key); } public Converter getKeyConverter() { return keyConverter; } public void setKeyConverter(Converter keyConverter) { this.keyConverter = keyConverter; } public void setValueConverter(Converter valueConverter) { this.valueConverter = valueConverter; } public Converter getValueConverter() { return valueConverter; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy