Android Open Source - Android-Lib-Database Predicate






From Project

Back to project page Android-Lib-Database.

License

The source code is released under:

Apache License

If you think the Android project Android-Lib-Database listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.lib.database.predicate;
/*from w ww.ja  va2  s.c  o m*/
import java.util.ArrayList;
import java.util.List;

/**
 * Specifies the filter conditions that records must meet to be included in the query results.
 */
public class Predicate {
    private static final String AND = "(%1$s) AND (%2$s)"; //$NON-NLS-1$
    private static final String OR  = "(%1$s) OR (%2$s)";  //$NON-NLS-1$

    private static final String EQUAL_TO                 = "%1$s = %2$s";                //$NON-NLS-1$
    private static final String NOT_EQUAL_TO             = "%1$s <> %2$s";               //$NON-NLS-1$
    private static final String LESS_THAN                = "%1$s < %2$s";                //$NON-NLS-1$
    private static final String LESS_THAN_OR_EQUAL_TO    = "%1$s <= %2$s";               //$NON-NLS-1$
    private static final String GREATER_THAN             = "%1$s > %2$s";                //$NON-NLS-1$
    private static final String GREATER_THAN_OR_EQUAL_TO = "%1$s >= %2$s";               //$NON-NLS-1$
    private static final String LIKE                     = "%1$s LIKE %2$s";             //$NON-NLS-1$
    private static final String IS_NULL                  = "%s IS NULL";                 //$NON-NLS-1$
    private static final String IS_NOT_NULL              = "%s IS NOT NULL";             //$NON-NLS-1$
    private static final String IN                       = "%1$s IN (%2$s)";             //$NON-NLS-1$
    private static final String BETWEEN                  = "%1$s BETWEEN %2$s AND %3$s"; //$NON-NLS-1$

    protected static final String PARAMETER = "?"; //$NON-NLS-1$

    private final List<Object> parameters;

    private final String predicate;

    protected Predicate(final String template, final Object identifier, final boolean isValue) {
        this.parameters = new ArrayList<Object>();

        if (isValue) {
            this.predicate = String.format(template, Predicate.PARAMETER);

            this.parameters.add(identifier);
        } else {
            this.predicate = String.format(template, identifier.toString());
        }
    }

    protected Predicate(final String predicate, final List<Object> parameters) {
        this.predicate  = predicate;
        this.parameters = parameters;
    }

    public Predicate and(final Predicate predicate) {
        final List<Object> parameters = new ArrayList<Object>();

        parameters.addAll(this.parameters);
        parameters.addAll(predicate.parameters);

        return new Predicate(String.format(Predicate.AND, this.toString(), predicate.toString()), parameters);
    }

    public Predicate or(final Predicate predicate) {
        final List<Object> parameters = new ArrayList<Object>();

        parameters.addAll(this.parameters);
        parameters.addAll(predicate.parameters);

        return new Predicate(String.format(Predicate.OR, this.toString(), predicate.toString()), parameters);
    }

    public final List<Object> getParameters() {
        return this.parameters;
    }

    @Override
    public String toString() {
        return this.predicate;
    }

    /**
     * A short-hand for {@link #equalTo(Object, Object, false, true)}.
     */
    public static Predicate equalTo(final Object left, final Object right) {
        return Predicate.equalTo(left, right, false, true);
    }

    public static Predicate equalTo(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.EQUAL_TO, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #notEqualTo(Object, Object, false, true)}.
     */
    public static Predicate notEqualTo(final Object left, final Object right) {
        return Predicate.notEqualTo(left, right, false, true);
    }

    public static Predicate notEqualTo(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.NOT_EQUAL_TO, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #lessThan(Object, Object, false, true)}.
     */
    public static Predicate lessThan(final Object left, final Object right) {
        return Predicate.lessThan(left, right, false, true);
    }

    public static Predicate lessThan(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.LESS_THAN, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #lessThanOrEqualTo(Object, Object, false, true)}.
     */
    public static Predicate lessThanOrEqualTo(final Object left, final Object right) {
        return Predicate.lessThanOrEqualTo(left, right, false, true);
    }

    public static Predicate lessThanOrEqualTo(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.LESS_THAN_OR_EQUAL_TO, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #greaterThan(Object, Object, false, true)}.
     */
    public static Predicate greaterThan(final Object left, final Object right) {
        return Predicate.greaterThan(left, right, false, true);
    }

    public static Predicate greaterThan(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.GREATER_THAN, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #greaterThanOrEqualTo(Object, Object, false, true)}.
     */
    public static Predicate greaterThanOrEqualTo(final Object left, final Object right) {
        return Predicate.greaterThanOrEqualTo(left, right, false, true);
    }

    public static Predicate greaterThanOrEqualTo(final Object left, final Object right, final boolean leftIsValue, final boolean rightIsValue) {
        return TwoSidedPredicate.newInstance(Predicate.GREATER_THAN_OR_EQUAL_TO, left, right, leftIsValue, rightIsValue);
    }

    /**
     * A short-hand for {@link #like(Object, false, String)}.
     */
    public static Predicate like(final Object identifier, final String expression) {
        return Predicate.like(identifier, false, expression);
    }

    public static Predicate like(final Object identifier, final boolean isValue, final String expression) {
        return TwoSidedPredicate.newInstance(Predicate.LIKE, identifier, expression, isValue, true);
    }

    /**
     * A short-hand for {@link #isNull(Object, false)}.
     */
    public static Predicate isNull(final Object identifier) {
        return Predicate.isNull(identifier, false);
    }

    public static Predicate isNull(final Object identifier, final boolean isValue) {
        return new Predicate(Predicate.IS_NULL, identifier, isValue);
    }

    /**
     * A short-hand for {@link #isNotNull(Object, false)}.
     */
    public static Predicate isNotNull(final Object identifier) {
        return Predicate.isNotNull(identifier, false);
    }

    public static Predicate isNotNull(final Object identifier, final boolean isValue) {
        return new Predicate(Predicate.IS_NOT_NULL, identifier, isValue);
    }

    /**
     * A short-hand for {@link #in(Object, false, Object...)}.
     */
    public static Predicate in(final Object identifier, final Object... values) {
        return Predicate.in(identifier, false, values);
    }

    public static Predicate in(final Object identifier, final boolean isValue, final Object... values) {
        return ManySidedPredicate.newInstance(Predicate.IN, identifier, isValue, values);
    }

    /**
     * A short-hand for {@link #between(Object, Object, Object, false, true, true)}.
     */
    public static Predicate between(final Object left, final Object middle, final Object right) {
        return Predicate.between(left, middle, right, false, true, true);
    }

    public static Predicate between(final Object left, final Object middle, final Object right, final boolean leftIsValue, final boolean middleIsValue, final boolean rightIsValue) {
        return ThreeSidedPredicate.newInstance(Predicate.BETWEEN, left, middle, right, leftIsValue, middleIsValue, rightIsValue);
    }
}




Java Source Code List

android.lib.database.Column.java
android.lib.database.CompositeIndex.java
android.lib.database.DatabaseOpenHelper.java
android.lib.database.Database.java
android.lib.database.DateConverter.java
android.lib.database.Index.java
android.lib.database.JSONRowMapper.java
android.lib.database.RowMapper.java
android.lib.database.Table.java
android.lib.database.TypeConverter.java
android.lib.database.UniqueCompositeIndex.java
android.lib.database.UnsupportedTypeException.java
android.lib.database.UseConverter.java
android.lib.database.predicate.ManySidedPredicate.java
android.lib.database.predicate.Predicate.java
android.lib.database.predicate.ThreeSidedPredicate.java
android.lib.database.predicate.TwoSidedPredicate.java
android.lib.database.query.Delete.java
android.lib.database.query.Insert.java
android.lib.database.query.QueryBuilder.java
android.lib.database.query.Query.java
android.lib.database.query.Select.java
android.lib.database.query.Update.java