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

org.archive.util.iterator.PrefixMatchStringIterator Maven / Gradle / Ivy

The newest version!
package org.archive.util.iterator;

import java.io.IOException;


public class PrefixMatchStringIterator extends AbstractPeekableIterator
{
	private boolean first = true;
	private String key;
	private CloseableIterator inner;
	
	public PrefixMatchStringIterator(CloseableIterator inner, String key, boolean alwaysIncludeFirst)
	{
		this.inner = inner;
		this.key = key;
		this.first = alwaysIncludeFirst;
	}

	@Override
	public String getNextInner() {
		
		if (!inner.hasNext()) {
			return null;
		}
		
		String blockLine = inner.next();
		
		// only compare the correct length:
		String prefCmp = key;
		
		if (first) {
			// always add first:
			first = false;
		} else if (!blockLine.startsWith(prefCmp)) {
			return null;
		}
		
		return blockLine;
	}

	@Override
	public void close() throws IOException {
		inner.close();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy