<commit_before>/*
 * Copyright <NAME> 2011, All rights reserved.
 *
 */

package net.domesdaybook.searcher;

import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.domesdaybook.reader.Reader;

/**
 *
 * @author matt
 */
public class SearchBackwardIterator implements Iterator {
    
    private final byte[] bytes;
    private final Reader reader;
    private final long toPosition;
    private final Searcher searcher;
    
    private long searchPosition;
    private boolean searchedForNext = false;
    private long matchPosition = Searcher.NOT_FOUND;
   
    
    public SearchBackwardIterator(final Searcher searcher, final Reader reader) throws IOException {
        this(searcher, reader.length() - 1, 0, reader);
    }
    
    
    public SearchBackwardIterator(final Searcher searcher, final long fromPosition, 
                                 final long toPosition, final Reader reader) {
        if (searcher == null || reader == null) {
            throw new IllegalArgumentException("Null searcher or byte reader.");
        }        
        this.searcher = searcher;
        this.reader = reader;
        this.toPosition = toPosition;
        this.bytes = null;
        this.searchPosition = fromPosition;
    }        
   
    
    
    public SearchBackwardIterator(final Searcher searcher, final byte[] bytes) {
        this(searcher, bytes.length - 1, 0, bytes);
    }
    
    
    public SearchBackwardIterator(final Searcher searcher, final long fromPosition, 
                                 final long toPosition, final byte[] bytes) {
        if (searcher == null || bytes == null) {
            throw new IllegalArgumentException("Null searcher or byte array.");
        }
        this.searcher = searcher;
        this.bytes = bytes;
        this.toPosition = toPosition;
        this.reader = null;
        this.searchPosition = fromPosition;  
    }        
    
    
    @Override
    public boolean hasNext() {
        if (!searchedForNext) {
            try {
                matchPosition = getNextMatchPosition();
                searchedForNext = true;
            } catch (IOException ex) {
                return false;
            }
        }
        return matchPosition >= 0;
    }

    
    @Override
    public Long next() {
        if (hasNext()) {
            searchPosition = matchPosition - 1;
            searchedForNext = false;
            return matchPosition;
        }
        throw new NoSuchElementException();
    }

    
    @Override
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove search results.");
    }
    
    
    private long getNextMatchPosition() throws IOException {
        long nextMatchingPosition = Searcher.NOT_FOUND;
        if (reader != null) {
            nextMatchingPosition = searcher.searchBackwards(reader, searchPosition, toPosition);
        } else if (bytes != null) {
            nextMatchingPosition = searcher.searchBackwards(bytes, (int) searchPosition, (int) toPosition);
        }
        return nextMatchingPosition;
    }
    
}
<commit_msg>minor comment headers for readability.<commit_after>/*
 * Copyright <NAME> 2011, All rights reserved.
 *
 */

package net.domesdaybook.searcher;

import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.domesdaybook.reader.Reader;

/**
 *
 * @author matt
 */
public class SearchBackwardIterator implements Iterator {
    
    // immutable fields:
    private final byte[] bytes;
    private final Reader reader;
    private final long toPosition;
    private final Searcher searcher;
    
    // private state:
    private long searchPosition;
    private boolean searchedForNext = false;
    private long matchPosition = Searcher.NOT_FOUND;
   
    
    public SearchBackwardIterator(final Searcher searcher, final Reader reader) throws IOException {
        this(searcher, reader.length() - 1, 0, reader);
    }
    
    
    public SearchBackwardIterator(final Searcher searcher, final long fromPosition, 
                                 final long toPosition, final Reader reader) {
        if (searcher == null || reader == null) {
            throw new IllegalArgumentException("Null searcher or byte reader.");
        }        
        this.searcher = searcher;
        this.reader = reader;
        this.toPosition = toPosition;
        this.bytes = null;
        this.searchPosition = fromPosition;
    }        
   
    
    
    public SearchBackwardIterator(final Searcher searcher, final byte[] bytes) {
        this(searcher, bytes.length - 1, 0, bytes);
    }
    
    
    public SearchBackwardIterator(final Searcher searcher, final long fromPosition, 
                                 final long toPosition, final byte[] bytes) {
        if (searcher == null || bytes == null) {
            throw new IllegalArgumentException("Null searcher or byte array.");
        }
        this.searcher = searcher;
        this.bytes = bytes;
        this.toPosition = toPosition;
        this.reader = null;
        this.searchPosition = fromPosition;  
    }        
    
    
    @Override
    public boolean hasNext() {
        if (!searchedForNext) {
            try {
                matchPosition = getNextMatchPosition();
                searchedForNext = true;
            } catch (IOException ex) {
                return false;
            }
        }
        return matchPosition >= 0;
    }

    
    @Override
    public Long next() {
        if (hasNext()) {
            searchPosition = matchPosition - 1;
            searchedForNext = false;
            return matchPosition;
        }
        throw new NoSuchElementException();
    }

    
    @Override
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove search results.");
    }
    
    
    private long getNextMatchPosition() throws IOException {
        long nextMatchingPosition = Searcher.NOT_FOUND;
        if (reader != null) {
            nextMatchingPosition = searcher.searchBackwards(reader, searchPosition, toPosition);
        } else if (bytes != null) {
            nextMatchingPosition = searcher.searchBackwards(bytes, (int) searchPosition, (int) toPosition);
        }
        return nextMatchingPosition;
    }
    
}

{'fraction_non_alphanumeric': 0.08128156565656566, 'fraction_numerical': 0.0031565656565656565, 'mean_word_length': 2.07621359223301, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1561206', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1642, 'n_words': 446}