org.fastmongo.odm.repository.query.QueryField.java Source code

Java tutorial

Introduction

Here is the source code for org.fastmongo.odm.repository.query.QueryField.java

Source

/*
 * Copyright (c) 2014 Alexander Gulko <kirhog at gmail dot com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.fastmongo.odm.repository.query;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;

import java.util.Set;

/**
 * Field with its value for the {@link Query}.
 *
 * @author Alexander Gulko
 */
@SuppressWarnings("UnusedDeclaration")
public class QueryField {

    private static final int INCLUDE = 1;
    private static final int EXCLUDE = 0;

    private static final String VAR = "$";

    private static final String PUSH = "$push";
    private static final String IN = "$in";
    private static final String NOT_IN = "$nin";
    private static final String GTE = "$gte";
    private static final String LT = "$lt";
    private static final String LTE = "$lte";
    private static final String SUM = "$sum";
    private static final String REGEX = "$regex";
    private static final String EXISTS = "$exists";
    private static final String ELEM_MATCH = "$elemMatch";
    private static final String CONTAINS_ALL = "$all";

    private Query query;
    private String name;
    private Object value;
    private boolean varName;
    private boolean varValue;

    public QueryField(Query query, String name) {
        this.query = query;
        this.name = name;

        this.query.getFields().add(this);
    }

    public QueryField isVarName(boolean isVarName) {
        this.varName = isVarName;
        return this;
    }

    public QueryField isVarValue(boolean isVarValue) {
        this.varValue = isVarValue;
        return this;
    }

    public Query eq(Object val) {
        this.value = val;
        return query;
    }

    public Query in(Object val) {
        this.value = new BasicDBObject().append(IN, val);
        return query;
    }

    public Query notIn(Object val) {
        this.value = new BasicDBObject().append(NOT_IN, val);
        return query;
    }

    public Query gte(Object val) {
        this.value = new BasicDBObject().append(GTE, val);
        return query;
    }

    public Query lt(Object val) {
        this.value = new BasicDBObject().append(LT, val);
        return query;
    }

    public Query lte(Object val) {
        this.value = new BasicDBObject().append(LTE, val);
        return query;
    }

    /**
     * min < value < max.
     * <p/>
     * { "latitude" : { "$gte" : 80.0 , "$lt" : 85.0}}
     *
     * @param minValue minimum value
     * @param maxValue maximum value
     * @return a query
     */
    public Query between(Object minValue, Object maxValue) {
        BasicDBObject object = new BasicDBObject();
        object.append(GTE, minValue);
        object.append(LT, maxValue);

        this.value = object;
        return query;
    }

    public Query include() {
        this.value = INCLUDE;
        return query;
    }

    public Query exclude() {
        this.value = EXCLUDE;
        return query;
    }

    public Query push(String val) {
        this.value = new BasicDBObject(PUSH, VAR + val);
        return query;
    }

    public Query sum(Object val) {
        this.value = new BasicDBObject(SUM, val);
        return query;
    }

    public Query regex(String val) {
        return regex(val, false);
    }

    public Query regex(String val, boolean caseInsensitive) {
        BasicDBObject dbo = new BasicDBObject(REGEX, val);
        if (caseInsensitive) {
            dbo.append("$options", "i");
        }
        this.value = dbo;
        return query;
    }

    public Query exists(boolean val) {
        this.value = new BasicDBObject(EXISTS, val);
        return query;
    }

    public Query elemMatch(Object val) {
        this.value = new BasicDBObject(ELEM_MATCH, val instanceof Query ? ((Query) val).toDbObject() : val);
        return query;
    }

    public Query containsAll(Set<?> values) {
        this.value = new BasicDBObject(CONTAINS_ALL, values);
        return query;
    }

    public Query and(Object... val) {
        BasicDBList and = new BasicDBList();
        for (Object object : val) {
            and.add(object instanceof Query ? ((Query) object).toDbObject() : object);
        }
        this.value = and;

        return query;
    }

    /**
     * The '$orderby' operator sorts the results of a query in ascending or descending order.
     *
     * @param order the sort order
     * @return current query instance
     */
    public Query orderBy(SortOrder order) {
        this.value = order.getValue();
        return query;
    }

    public String getName() {
        return !varName ? name : VAR + name;
    }

    public Object getValue() {
        return !varValue ? value : VAR + value;
    }

    /**
     * Specifies sort order for result.
     */
    public static enum SortOrder {
        /**
         * To sort in ascending order.
         */
        ASC(1),

        /**
         * To sort in descending order.
         */
        DESC(-1);

        private int value;

        SortOrder(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }
}