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

com.formkiq.vision.crafter.predicate.DocumentBlockSingleFieldPredicate Maven / Gradle / Ivy

package com.formkiq.vision.crafter.predicate;

import static com.formkiq.vision.predicate.DocumentBlockContainsPredicate.contains;

import java.util.List;
import java.util.function.Predicate;

import com.formkiq.vision.crafter.PageScratchPad;
import com.formkiq.vision.document.DocumentBlockRectangle;
import com.formkiq.vision.document.DocumentField;
import com.formkiq.vision.document.DocumentSource;
import com.formkiq.vision.document.DocumentText;

/**
 * {@link Predicate} for finding {@link DocumentBlockRectangle} that only contain 1 field.
 *
 */
public class DocumentBlockSingleFieldPredicate implements Predicate {

	/** {@link List} {@link DocumentField}. */
	private List fields;
	/** {@link List} {@link DocumentText}. */
	private List texts;

	/**
	 * constructor.
	 * @param scratchpad {@link PageScratchPad}
	 */
	public DocumentBlockSingleFieldPredicate(final PageScratchPad scratchpad) {
		DocumentSource document = scratchpad.getDocument();
		Integer pageNumber = scratchpad.getDocumentPageNumber();
		
		this.fields = document.getFields(pageNumber.intValue());
		this.texts = document.getTexts(pageNumber.intValue());
	}
	
	@Override
	public boolean test(final DocumentBlockRectangle r) {
		long fieldcount = this.fields.stream().filter(f -> contains(r, f)).count();
		long textcount = this.texts.stream().filter(f -> contains(r, f)).count();
		return fieldcount < 2 && textcount == 0;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy