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

net.avcompris.commons.query.impl.LteProxy Maven / Gradle / Ivy

There is a newer version: 0.6.3
Show newest version
package net.avcompris.commons.query.impl;

import static net.avcompris.commons.query.Filtering.Type.LTE;
import static net.avcompris.commons.query.impl.FieldUtils.isDateTimeField;
import static net.avcompris.commons.query.impl.FieldUtils.isIntField;
import static net.avcompris.commons.query.impl.FilteringFieldProxy.extractFieldValue;

import javax.annotation.Nullable;

import org.apache.commons.lang3.NotImplementedException;
import org.joda.time.DateTime;

import net.avcompris.commons.query.DateTimePrecision;
import net.avcompris.commons.query.Filtering;
import net.avcompris.commons.query.FilteringHandler;

final class LteProxy, U extends Filtering.Field> extends AbstractFilteringFieldProxy {

	public LteProxy(final Class filteringClass, final U field, @Nullable final Object refValue) {

		super(filteringClass, field, "lte", refValue);
	}

	@Override
	public boolean match(final Object arg) {

		if (arg == null && refValue == null) {

			return false;
		}

		final Class argClass = arg.getClass();

		if (Integer.class.equals(argClass)) {

			return (int) arg <= (int) refValue;

		} else if (refValue instanceof Integer) {

			final int value = extractFieldValue(arg, field, int.class);

			return value <= (int) refValue;

		} else if (DateTime.class.equals(argClass)) {

			return !((DateTime) arg).isBefore((DateTime) refValue);

		} else if (refValue instanceof DateTime) {

			final DateTime value = extractFieldValue(arg, field, DateTime.class);

			return !value.isBefore((DateTime) refValue);

		} else if (refValue instanceof DateTimeArg) {

			final DateTime value = extractFieldValue(arg, field, DateTime.class);

			final DateTimeArg dateTimeArg = (DateTimeArg) refValue;

			return dateTimeArg.lte(value);
		}

		throw new NotImplementedException("argClass: " + argClass);
	}

	@Override
	public void applyTo(final FilteringHandler handler) {

		if (isIntField(field)) {

			handler.lte(field, (int) refValue);

		} else if (isDateTimeField(field)) {

			if (refValue instanceof DateTimeArg) {

				final DateTimeArg dateTimeArg = (DateTimeArg) refValue;

				handler.lte(field, dateTimeArg.dateTime, dateTimeArg.precision);

			} else {

				handler.lte(field, (DateTime) refValue, DateTimePrecision.MILLISECOND);
			}

		} else {

			throw new NotImplementedException("refValue.class: " + refValue.getClass().getName());
		}
	}

	@Override
	public Type getType() {

		return LTE;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy