Package org.qi4j.runtime.association

Source Code of org.qi4j.runtime.association.AbstractAssociationInstance

package org.qi4j.runtime.association;

import org.qi4j.api.entity.EntityComposite;
import org.qi4j.api.entity.EntityReference;
import org.qi4j.api.association.AbstractAssociation;
import org.qi4j.functional.Function2;
import org.qi4j.runtime.composite.ProxyReferenceInvocationHandler;
import org.qi4j.runtime.entity.EntityInstance;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;

/**
* Implementation of AbstractAssociation. Includes helper methods for subclasses
*/
public abstract class AbstractAssociationInstance<T>
    implements AbstractAssociation
{
    protected AssociationInfo associationInfo;
    private Function2<EntityReference, Type, Object> entityFunction;

    public AbstractAssociationInstance( AssociationInfo associationInfo,
                                        Function2<EntityReference, Type, Object> entityFunction
    )
    {
        this.associationInfo = associationInfo;
        this.entityFunction = entityFunction;
    }

    public AssociationInfo getAssociationInfo()
    {
        return associationInfo;
    }

    public void setAssociationInfo(AssociationInfo newInfo)
    {
        this.associationInfo = newInfo;
    }

    protected T getEntity( EntityReference entityId )
    {
        if( entityId == null )
        {
            return null;
        }

        return (T) entityFunction.map( entityId, associationInfo.type() );
    }

    protected EntityReference getEntityReference( Object composite )
    {
        if( composite == null )
        {
            return null;
        }

        InvocationHandler handler = Proxy.getInvocationHandler( composite );
        if( handler instanceof ProxyReferenceInvocationHandler )
        {
            handler = Proxy.getInvocationHandler( ( (ProxyReferenceInvocationHandler) handler ).proxy() );
        }
        EntityInstance instance = (EntityInstance) handler;
        return instance.identity();
    }

    protected void checkType( Object instance )
    {
        if( instance != null )
        {
            if( !( instance instanceof EntityComposite ) )
            {
                if( instance instanceof Proxy )
                {
                    if( Proxy.getInvocationHandler( instance ) instanceof EntityInstance )
                    {
                        return; // It's fine
                    }
                }

                throw new IllegalArgumentException( "Object must be an EntityComposite" );
            }
        }
    }

    protected void checkImmutable() throws IllegalStateException
    {
        if( associationInfo.isImmutable() )
        {
            throw new IllegalStateException( "Association [" + associationInfo.qualifiedName() + "] is immutable." );
        }
    }
}
TOP

Related Classes of org.qi4j.runtime.association.AbstractAssociationInstance

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.