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

com.infomaximum.database.domainobject.iterator.AllIterator Maven / Gradle / Ivy

The newest version!
package com.infomaximum.database.domainobject.iterator;

import com.infomaximum.database.provider.DBIterator;
import com.infomaximum.database.provider.KeyPattern;
import com.infomaximum.database.domainobject.DomainObject;
import com.infomaximum.database.domainobject.DataEnumerable;
import com.infomaximum.database.schema.Schema;
import com.infomaximum.database.schema.StructEntity;
import com.infomaximum.database.utils.key.FieldKey;
import com.infomaximum.database.exception.DatabaseException;

import java.lang.reflect.Constructor;
import java.util.NoSuchElementException;
import java.util.Set;

public class AllIterator implements IteratorEntity {

    private final DataEnumerable dataEnumerable;
    private final Constructor constructor;
    private final Set loadingFields;
    private final DBIterator dataIterator;
    private final StructEntity entity;

    private final DataEnumerable.NextState state;

    public AllIterator(DataEnumerable dataEnumerable, Class clazz, Set loadingFields) throws DatabaseException {
        this.dataEnumerable = dataEnumerable;
        this.constructor = DomainObject.getConstructor(clazz);
        this.loadingFields = loadingFields;
        this.entity = Schema.getEntity(clazz);
        this.dataIterator = dataEnumerable.createIterator(entity.getColumnFamily());

        KeyPattern dataKeyPattern = loadingFields != null ? FieldKey.buildKeyPattern(entity.getFieldNames(loadingFields)) : null;
        this.state = dataEnumerable.seek(dataIterator, dataKeyPattern, entity);
        if (this.state.isEmpty()) {
            close();
        }
    }

    @Override
    public boolean hasNext() {
        return !state.isEmpty();
    }

    @Override
    public E next() throws DatabaseException {
        if (state.isEmpty()) {
            throw new NoSuchElementException();
        }

        return dataEnumerable.nextObject(constructor, loadingFields, dataIterator, state, entity);
    }

    @Override
    public void close() throws DatabaseException {
        dataIterator.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy