<commit_before>package org.protempa.backend.dsb.filter;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.protempa.proposition.interval.Interval;
import org.protempa.proposition.interval.IntervalFactory;
import org.protempa.proposition.value.Granularity;

/**
 * A filter for a position (e.g., date/time) range.
 *
 * @author <NAME>
 */
public class PositionFilter extends AbstractFilter {

    /**
     * Which side of the proposition's interval to which to apply the position
     * bounds. For timestamped propositions, these are ignored.
     */
    public static enum Side {
        START, FINISH
    }

    private static final IntervalFactory intervalFactory =
            new IntervalFactory();

    private final Interval ival;

    private final Side startSide;

    private final Side finishSide;

    /**
     * Creates a filter with a position range.
     *
     * @param propIds a {@link String[]} of proposition ids on which to filter.
     * @param start the start position in Protempa's {@link Long}
     * representation.
     * @param startGran the {@link Granularity} with which to interpret the
     * start position.
     * @param finish the finish position in Protempa's {@link Long}
     * representation.
     * @param finishGran the {@link Granularity} with which to interpret the
     * finish position.
     */
    public PositionFilter(String[] propIds, Long start,
            Granularity startGran, Long finish, Granularity finishGran) {
        this(propIds, start, startGran, finish, finishGran, null, null);
    }

    /**
     * Initializes a filter with a position range.
     *
     * @param propIds a {@link String[]} of proposition ids on which to filter.
     * @param start the start position in Protempa's {@link Long}
     * representation.
     * @param startGran the {@link Granularity} with which to interpret the
     * start position.
     * @param finish the finish position in Protempa's {@link Long}
     * representation.
     * @param finishGran the {@link Granularity} with which to interpret the
     * finish position.
     * @param startSide the {@link Side} of the proposition to which to apply
     * the start bound. The default is {@link Side.START} (if
     * <code>null</code> is specified). Ignored for timestamped propositions.
     * @param finishSide the {@link Side} of the proposition to which to
     * apply the finish bound. The default is {@link Side.FINISH} (if
     * <code>null</code> is specified). Ignored for timestamped propositions.
     */
    public PositionFilter(String[] propIds, Long start,
            Granularity startGran, Long finish, Granularity finishGran,
            Side startSide, Side finishSide) {
        super(propIds);
        this.ival = intervalFactory.getInstance(start, startGran,
                finish, finishGran);
        if (startSide == null)
            startSide = Side.START;
        if (finishSide == null)
            finishSide = Side.FINISH;
        this.startSide = startSide;
        this.finishSide = finishSide;
    }

    /**
     * Returns the {@link Granularity} with which to interpret the
     * start position.
     * 
     * @return the startGranularity a {@link Granularity}.
     */
    public Granularity getStartGranularity() {
        return this.ival.getStartGranularity();
    }

    /**
     * Returns the {@link Granularity} with which to interpret the
     * finish position.
     *
     * @return the finishGranularity a {@link Granularity}.
     */
    public Granularity getFinishGranularity() {
        return this.ival.getFinishGranularity();
    }

    /**
     * Returns the maximum finish position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMaximumFinish() {
        return this.ival.getMaximumFinish();
    }

    /**
     * Returns the maximum start position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMaximumStart() {
        return this.ival.getMaximumStart();
    }

    /**
     * Returns the minimum finish position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMinimumFinish() {
        return this.ival.getMinimumFinish();
    }

    /**
     * Returns the minimum start position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMinimumStart() {
        return this.ival.getMinimumStart();
    }

    /**
     * Returns the start position supplied in the constructor.
     *
     * @return a {@link Long}.
     */
    public Long getStart() {
        return this.ival.getMinStart();
    }

    /**
     * Returns the finish position supplied in the constructor.
     *
     * @return a {@link Long}.
     */
    public Long getFinish() {
        return this.ival.getMinFinish();
    }

    /**
     * Returns the side of the proposition to which to apply the lower bound.
     *
     * @return a {@link Side}.
     */
    public Side getStartSide() {
        return this.startSide;
    }

    /**
     * Returns the side of the proposition to which to apply the upper bound.
     *
     * @return a {@link Side}.
     */
    public Side getFinishSide() {
        return this.finishSide;
    }

    @Override
    public void accept(FilterVisitor visitor) {
        visitor.visit(this);
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}<commit_msg>To accommodate Castor, startSide and finishSide can be specified and fetched as strings.<commit_after>package org.protempa.backend.dsb.filter;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.protempa.proposition.interval.Interval;
import org.protempa.proposition.interval.IntervalFactory;
import org.protempa.proposition.value.Granularity;
import org.protempa.proposition.value.XMLGranularityFactory;

/**
 * A filter for a position (e.g., date/time) range.
 *
 * @author <NAME>
 */
public class PositionFilter extends AbstractFilter {

    /**
     * Which side of the proposition's interval to which to apply the position
     * bounds. For timestamped propositions, these are ignored.
     */
    public static enum Side {
        START("start"), FINISH("finish");
        
        private String xmlName;
        
        private Side(String xmlName) {
        	this.xmlName = xmlName;
        }
        
        public String getXmlName() {
        	return xmlName;
        }
    }

    private static final IntervalFactory intervalFactory =
            new IntervalFactory();

    private final Interval ival;

    private final Side startSide;

    private final Side finishSide;

    /**
     * Creates a filter with a position range.
     *
     * @param propIds a {@link String[]} of proposition ids on which to filter.
     * @param start the start position in Protempa's {@link Long}
     * representation.
     * @param startGran the {@link Granularity} with which to interpret the
     * start position.
     * @param finish the finish position in Protempa's {@link Long}
     * representation.
     * @param finishGran the {@link Granularity} with which to interpret the
     * finish position.
     */
    public PositionFilter(String[] propIds, Long start,
            Granularity startGran, Long finish, Granularity finishGran) {
        this(propIds, start, startGran, finish, finishGran, null, null);
    }

    /**
     * Initializes a filter with a position range.
     *
     * @param propIds a {@link String[]} of proposition ids on which to filter.
     * @param start the start position in Protempa's {@link Long}
     * representation.
     * @param startGran the {@link Granularity} with which to interpret the
     * start position.
     * @param finish the finish position in Protempa's {@link Long}
     * representation.
     * @param finishGran the {@link Granularity} with which to interpret the
     * finish position.
     * @param startSide the {@link Side} of the proposition to which to apply
     * the start bound. The default is {@link Side.START} (if
     * <code>null</code> is specified). Ignored for timestamped propositions.
     * @param finishSide the {@link Side} of the proposition to which to
     * apply the finish bound. The default is {@link Side.FINISH} (if
     * <code>null</code> is specified). Ignored for timestamped propositions.
     */
    public PositionFilter(String[] propIds, Long start,
            Granularity startGran, Long finish, Granularity finishGran,
            Side startSide, Side finishSide) {
        super(propIds);
        this.ival = intervalFactory.getInstance(start, startGran,
                finish, finishGran);
        if (startSide == null)
            startSide = Side.START;
        if (finishSide == null)
            finishSide = Side.FINISH;
        this.startSide = startSide;
        this.finishSide = finishSide;
    }

	/**
	 * Initializes a filter with a position range.
	 * <p>
	 * This constructor is intended to be used by Castor when converting from
	 * XML.
	 * 
	 * @param propIds
	 *            a {@link String[]} of proposition ids on which to filter.
	 * @param start
	 *            the start position in Protempa's {@link Long} representation.
	 * @param startGran
	 *            the name of the {@link Granularity} with which to interpret
	 *            the start position.
	 * @param finish
	 *            the finish position in Protempa's {@link Long} representation.
	 * @param finishGran
	 *            the name of the {@link Granularity} with which to interpret
	 *            the finish position.
	 * @param startSide
	 *            the name of the {@link Side} of the proposition to which to
	 *            apply the start bound. The default is {@link Side.START} (if
	 *            <code>null</code> is specified). Ignored for timestamped
	 *            propositions.
	 * @param finishSide
	 *            the name of the {@link Side} of the proposition to which to
	 *            apply the finish bound. The default is {@link Side.FINISH} (if
	 *            <code>null</code> is specified). Ignored for timestamped
	 *            propositions.
	 */
    public PositionFilter(String[] propIds, Long start,
            String startGran, Long finish, String finishGran,
            String startSide, String finishSide) {
        this(propIds, start, nameToGranularity(startGran), finish, nameToGranularity(finishGran), nameToSide(startSide), nameToSide(finishSide));
    }
    
    private static Side nameToSide(String name) {
    	if (Side.START.getXmlName().equals(name)) {
    		return Side.START;
    	}
    	if (Side.FINISH.getXmlName().equals(name)) {
    		return Side.FINISH;
    	} else {
    		return null;
    	}
    }
    
    private static Granularity nameToGranularity(String name) {
    	Granularity g = XMLGranularityFactory.xmlToGranularity(name);
    	if (g == null) {
    		String msg = "Value passed to constructor of " + PositionFilter.class + " is not a valid name for a granularity";
    		throw new IllegalArgumentException(msg);
    	}
    	return g;
    }

    /**
     * Returns the {@link Granularity} with which to interpret the
     * start position.
     * 
     * @return the startGranularity a {@link Granularity}.
     */
    public Granularity getStartGranularity() {
        return this.ival.getStartGranularity();
    }

    /**
     * Returns the XML name of the {@link Granularity} with which to interpret the
     * start position.
     * 
     * @return the startGranularity a {@link Granularity}.
     */
    public String getStartGranularityXMLName() {
        return XMLGranularityFactory.granularityToXml(this.ival.getStartGranularity());
    }

    /**
     * Returns the {@link Granularity} with which to interpret the
     * finish position.
     *
     * @return the finishGranularity a {@link Granularity}.
     */
    public Granularity getFinishGranularity() {
        return this.ival.getFinishGranularity();
    }

    /**
     * Returns the name of the {@link Granularity} with which to interpret the
     * finish position.
     *
     * @return the finishGranularity a {@link Granularity}.
     */
    public String getFinishGranularityXMLName() {
        return XMLGranularityFactory.granularityToXml(this.ival.getFinishGranularity());
    }

    /**
     * Returns the maximum finish position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMaximumFinish() {
        return this.ival.getMaximumFinish();
    }

    /**
     * Returns the maximum start position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMaximumStart() {
        return this.ival.getMaximumStart();
    }

    /**
     * Returns the minimum finish position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMinimumFinish() {
        return this.ival.getMinimumFinish();
    }

    /**
     * Returns the minimum start position in Protempa's {@link Long}
     * representation.
     *
     * @return a {@link Long}.
     */
    public Long getMinimumStart() {
        return this.ival.getMinimumStart();
    }

    /**
     * Returns the start position supplied in the constructor.
     *
     * @return a {@link Long}.
     */
    public Long getStart() {
        return this.ival.getMinStart();
    }

    /**
     * Returns the finish position supplied in the constructor.
     *
     * @return a {@link Long}.
     */
    public Long getFinish() {
        return this.ival.getMinFinish();
    }

    /**
     * Returns the side of the proposition to which to apply the lower bound.
     *
     * @return a {@link Side}.
     */
    public Side getStartSide() {
        return this.startSide;
    }

    /**
     * Returns the name of the side of the proposition to which to apply the lower bound.
     *
     * @return a {@link Side}.
     */
    public String getStartSideXMLName() {
        return this.startSide.getXmlName();
    }

    /**
     * Returns the side of the proposition to which to apply the upper bound.
     *
     * @return a {@link Side}.
     */
    public Side getFinishSide() {
        return this.finishSide;
    }

    /**
     * Returns the name of the side of the proposition to which to apply the upper bound.
     *
     * @return a {@link Side}.
     */
    public String getFinishSideXMLName() {
        return this.finishSide.getXmlName();
    }

    @Override
    public void accept(FilterVisitor visitor) {
        visitor.visit(this);
    }


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
{'fraction_non_alphanumeric': 0.09440960280531391, 'fraction_numerical': 0.0, 'mean_word_length': 2.375825176417027, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4744327', 'n_tokens_mistral': 4168, 'n_tokens_neox': 4031, 'n_words': 1371}