<commit_before>package org.intermine.webservice.server.output;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.ObjectUtils;
import org.intermine.api.results.ResultElement;
import org.intermine.objectstore.query.Results;

public class ResultsIterator implements Iterator<List<ResultElement>> {

	private static Logger logger = Logger.getLogger(ResultsIterator.class);
	
	private int counter = 0;
	private int start = 0;
	private Integer end = null;
    private String filterTerm = null;
    private List<ResultElement> nextRow = null;
	
	private final Iterator<Object> subIter;
	
	public ResultsIterator(Results res) {
		this.subIter = res.iterator();
	}
	
	public ResultsIterator(Results res, int start, int size, String filterTerm) {
		this.subIter = res.iterator();
		this.start = start;
		this.end = start + size;
        this.filterTerm = StringUtils.lowerCase(filterTerm);
	}
	
	@Override
	public boolean hasNext() {
		scrollToStart();
		if (counter >= start && (end == null || counter < end)) {
            if (StringUtils.isBlank(filterTerm)) {
                return subIter.hasNext();
            } else {
                try {
                    nextRow = next();
                } catch (NoSuchElementException e) {
                    return false;
                }
                return nextRow != null;
            }
		}
		return false;
	}
	
	private void scrollToStart() {
		while (counter < start && subIter.hasNext()) {
			// throw away values we are not interested in.
            List<Object> l = (List<Object>) subIter.next();
            if (StringUtils.isBlank(filterTerm)) {
                counter++;
            } else {
                boolean contained = false;
                for (Object o: l) {
                    String n = ObjectUtils.toString(o).toLowerCase();
                    if (StringUtils.contains(n, filterTerm)) {
                        contained = true;
                    }
                } 
                if (contained) {
                    counter++;
                }
            }
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ResultElement> next() {
		scrollToStart();
		if (counter >= end) {
			throw new NoSuchElementException();
		}
        if (nextRow != null) {
            List<ResultElement> ret = nextRow;
            nextRow = null;
            return ret;
        }
		List<Object> l = (List<Object>) subIter.next();
        if (StringUtils.isBlank(filterTerm)) {
            counter++;
        } else {
            boolean contained = false;
            for (Object o: l) {
                String n = ObjectUtils.toString(o).toLowerCase();
                if (StringUtils.contains(n, filterTerm)) {
                    contained = true;
                }
            } 
            if (contained) {
                counter++;
            } else {
                return next();
            }
        }
		List<ResultElement> ret = new ArrayList<ResultElement>();<commit_msg>Fixed reporting bug<commit_after>package org.intermine.webservice.server.output;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.ObjectUtils;
import org.intermine.api.results.ResultElement;
import org.intermine.objectstore.query.Results;

public class ResultsIterator implements Iterator<List<ResultElement>> {

	private static Logger logger = Logger.getLogger(ResultsIterator.class);
	
	private int counter = 0;
	private int start = 0;
	private Integer end = null;
    private String filterTerm = null;
    private List<ResultElement> nextRow = null;
	
	private final Iterator<Object> subIter;
	
	public ResultsIterator(Results res) {
		this.subIter = res.iterator();
	}
	
	public ResultsIterator(Results res, int start, int size, String filterTerm) {
		this.subIter = res.iterator();
		this.start = start;
		this.end = start + size;
        this.filterTerm = ObjectUtils.toString(filterTerm).toLowerCase();
        logger.info("START: "  + start + ", END: " + (start + size) + ", FILTER: " + this.filterTerm);
	}
	
	@Override
	public boolean hasNext() {
		scrollToStart();
		if (counter >= start && (end == null || counter < end)) {
            if (StringUtils.isBlank(filterTerm)) {
                return subIter.hasNext();
            } else {
                try {
                    nextRow = next();
                } catch (NoSuchElementException e) {
                    return false;
                }
                return nextRow != null;
            }
		}
		return false;
	}
	
	private void scrollToStart() {
		while (counter < start && subIter.hasNext()) {
			// throw away values we are not interested in.
            List<Object> l = (List<Object>) subIter.next();
            if (StringUtils.isBlank(filterTerm)) {
                counter++;
            } else {
                boolean contained = false;
                for (Object o: l) {
                    String n = ObjectUtils.toString(o).toLowerCase();
                    if (StringUtils.contains(n, filterTerm)) {
                        contained = true;
                    }
                } 
                if (contained) {
                    counter++;
                } 
            }
		}
	}

    private List<Object> getNextInternal() {
		List<Object> l = (List<Object>) subIter.next();
        if (StringUtils.isBlank(filterTerm)) {
            return l;
        } else {
            Object o = l.get(0);
            String n = (o == null) ? "" : o.toString().toLowerCase();
            if (n.indexOf(filterTerm) >= 0) {
                return l;
            } else {
                return null;
            }
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<ResultElement> next() {
		if (nextRow != null) {
            List<ResultElement> ret = nextRow;
            nextRow = null;
            return ret;
        }
		scrollToStart();
		if (counter >= end) {
			throw new NoSuchElementException();
		}
        
        List<Object> l = null;
        while (l == null) {
            l = getNextInternal();
        }
        counter++;

		List<ResultElement> ret = new ArrayList<ResultElement>();
{'fraction_non_alphanumeric': 0.11054104477611941, 'fraction_numerical': 0.0012437810945273632, 'mean_word_length': 1.9069136918210574, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4673604', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1713, 'n_words': 422}