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

com.buschmais.xo.impl.proxy.collection.EntityCollectionProxy Maven / Gradle / Ivy

The newest version!
package com.buschmais.xo.impl.proxy.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.buschmais.xo.api.metadata.method.EntityCollectionPropertyMethodMetadata;
import com.buschmais.xo.impl.SessionContext;
import com.buschmais.xo.spi.session.InstanceManager;

public class EntityCollectionProxy
    extends AbstractCollectionProxy> implements Collection {

    public EntityCollectionProxy(SessionContext sessionContext, Entity entity,
        EntityCollectionPropertyMethodMetadata metadata) {
        super(sessionContext, entity, metadata);
    }

    public Iterator iterator() {
        final SessionContext sessionContext = getSessionContext();
        final EntityCollectionPropertyMethodMetadata metadata = getMetadata();
        final Iterator iterator = sessionContext.getEntityPropertyManager()
            .getEntityCollection(getEntity(), metadata);
        return sessionContext.getInterceptorFactory()
            .addInterceptor(new Iterator() {

                private Instance instance = null;

                @Override
                public boolean hasNext() {
                    while (!isInstance() && iterator.hasNext()) {
                        instance = sessionContext.getEntityInstanceManager()
                            .readInstance(iterator.next());
                    }
                    return isInstance();
                }

                private boolean isInstance() {
                    return instance != null && metadata.getElementType()
                        .isAssignableFrom(instance.getClass());
                }

                @Override
                public Instance next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    Instance next = instance;
                    instance = null;
                    return next;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove not supported");
                }
            }, Iterator.class);
    }

    @Override
    public boolean add(Instance instance) {
        getSessionContext().getEntityPropertyManager()
            .createEntityReference(getEntity(), getMetadata(), instance);
        return true;
    }

    @Override
    public boolean remove(Object o) {
        SessionContext sessionContext = getSessionContext();
        InstanceManager instanceManager = sessionContext.getEntityInstanceManager();
        if (instanceManager.isInstance(o)) {
            return sessionContext.getEntityPropertyManager()
                .removeEntityReference(getEntity(), getMetadata(), o);
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy