fr.whimtrip.ext.jwhthtmltopojo.impl.AcceptIfFirst Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimtrip-ext-htmltopojo Show documentation
Show all versions of whimtrip-ext-htmltopojo Show documentation
Fully featured highly pluggable and customizable Java html to pojo reflection converter
package fr.whimtrip.ext.jwhthtmltopojo.impl;
import fr.whimtrip.core.util.exception.ObjectCreationException;
import fr.whimtrip.ext.jwhthtmltopojo.annotation.AcceptObjectIf;
import fr.whimtrip.ext.jwhthtmltopojo.annotation.FilterFirstResultsOnly;
import fr.whimtrip.ext.jwhthtmltopojo.annotation.Selector;
import fr.whimtrip.ext.jwhthtmltopojo.intrf.AcceptIfResolver;
import org.jsoup.nodes.Element;
import java.lang.reflect.Field;
import java.util.List;
/**
* Part of project jwht-htmltopojo
*
* Example implementation of an {@link AcceptIfResolver}.
*
*
* This one will only keep some results out of all inside a given List
* of elements. You can basically give a start and an end index to pick
* from the list. Very useful when you only need for example the first
* three elements of a list.
*
*
*
* You have to provide an {@link FilterFirstResultsOnly} annotation on top of
* the corresponding field in order for this deserializer to work properly.
*
*
* @author Louis-wht
* @since 1.0.0
*/
public class AcceptIfFirst implements AcceptIfResolver {
private FilterFirstResultsOnly filterFirstResultsOnly;
private int index = 0;
/**
* {@inheritDoc}
*/
@Override
public void init(Field field, Object parentObject, Selector selector) throws ObjectCreationException {
filterFirstResultsOnly = field.getAnnotation(FilterFirstResultsOnly.class);
if(filterFirstResultsOnly == null)
throw new ObjectCreationException(field, this.getClass(), FilterFirstResultsOnly.class);
if(!List.class.isAssignableFrom(field.getType()))
throw new ObjectCreationException("Field " + field.getName() + " from object " + field.getDeclaringClass()
+ " has @" + AcceptObjectIf.class.getName() + " annotation on a non " + List.class.getName() + " field.");
index ++;
}
/**
* {@inheritDoc}
*/
@Override
public boolean accept(Element element, Object parentObject) {
return index > filterFirstResultsOnly.start()
&& index <= filterFirstResultsOnly.end();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy