FileDocCategorySizeDatePackage
DirectMapContainerPolicy.javaAPI DocGlassfish v2 API11251Tue May 22 16:54:40 BST 2007oracle.toplink.essentials.internal.queryframework

DirectMapContainerPolicy

public class DirectMapContainerPolicy extends InterfaceContainerPolicy

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

Fields Summary
protected DatabaseField
keyField
protected DatabaseField
valueField
protected Converter
keyConverter
protected Converter
valueConverter
Constructors Summary
public DirectMapContainerPolicy()
INTERNAL: Construct a new policy.

        super();
    
public DirectMapContainerPolicy(Class containerClass)
INTERNAL: Construct a new policy for the specified class.

        super(containerClass);
    
Methods Summary
public booleanaddInto(java.lang.Object key, java.lang.Object value, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Add key, value pair into container which implements the Map interface.

        try {
            ((Map)container).put(key, value);
        } catch (ClassCastException ex1) {
            throw QueryException.cannotAddElement(key, container, ex1);
        }
        return true;
    
public booleanaddInto(java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Add element into container which implements the Map interface. Not used since key is not obtained from the object

        throw ValidationException.operationNotSupported("addInto(Object element, Object container, Session session)");
    
public java.lang.ObjectbuildContainerFromVector(java.util.Vector vector, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Return a container populated with the contents of the specified Vector.

        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;
    
public voidclear(java.lang.Object container)
INTERNAL: Remove all the elements from container.

        try {
            ((Map)container).clear();
        } catch (UnsupportedOperationException ex) {
            throw QueryException.methodNotValid(container, "clear()");
        }
    
public booleancompareContainers(java.lang.Object firstObjectMap, java.lang.Object secondObjectMap)
INTERNAL: Return true if keys are the same. False otherwise

        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;
    
public booleancompareKeys(java.lang.Object sourceValue, oracle.toplink.essentials.internal.sessions.AbstractSession session)
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

        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)));
    
protected booleancontains(java.lang.Object element, java.lang.Object container)
INTERNAL: Return the true if element exists in container.

return
boolean true if container 'contains' element

        return ((Map)container).containsValue(element);
    
public java.lang.ClassgetInterfaceType()

        return ClassConstants.Map_Class;
    
public oracle.toplink.essentials.mappings.converters.ConvertergetKeyConverter()

        return keyConverter;
    
public oracle.toplink.essentials.mappings.converters.ConvertergetValueConverter()

        return valueConverter;
    
public booleanisDirectMapPolicy()

        return true;
    
public booleanisValidContainer(java.lang.Object container)
INTERNAL: Validate the container type.

        // PERF: Use instanceof which is inlined, not isAssignable which is very inefficent.
        return container instanceof Map;
    
public java.lang.ObjectiteratorFor(java.lang.Object container)
INTERNAL: Return an Iterator for the given container.

        if (((Map)container).keySet() == null) {
            return null;
        }
        return ((Map)container).keySet().iterator();
    
public java.lang.ObjectiteratorForValue(java.lang.Object container)
INTERNAL: Return an Iterator for the given container.

        if (((Map)container).values() == null) {
            return null;
        }
        return ((Map)container).values().iterator();
    
public booleanremoveFrom(java.lang.Object key, java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Remove element from container which implements the Map interface.

        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)");
        }
    
public booleanremoveFromWithIdentity(java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Remove element from container which implements the Map interface.

        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 voidsetKeyConverter(oracle.toplink.essentials.mappings.converters.Converter keyConverter)

        this.keyConverter = keyConverter;
    
public voidsetKeyField(oracle.toplink.essentials.internal.helper.DatabaseField field)

        keyField = field;
    
public voidsetValueConverter(oracle.toplink.essentials.mappings.converters.Converter valueConverter)

        this.valueConverter = valueConverter;
    
public voidsetValueField(oracle.toplink.essentials.internal.helper.DatabaseField field)

        valueField = field;
    
public intsizeFor(java.lang.Object container)
INTERNAL: Return the size of container.

        return ((Map)container).size();
    
public voidvalidateElementAndRehashIfRequired(java.lang.Object sourceValue, java.lang.Object targetMap, oracle.toplink.essentials.internal.sessions.AbstractSession session, java.lang.Object targetVersionOfSource)
INTERNAL: If the key has changed, remove the element and add it back into the target.

        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);
            }
        }
    
public java.lang.ObjectvalueFromKey(java.lang.Object key, java.lang.Object container)
INTERNAL: Return an value of the key from container

        return ((Map)container).get(key);