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

org.deephacks.tools4j.config.admin.query.BeanQueryBuilder Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package org.deephacks.tools4j.config.admin.query;

import java.util.Arrays;
import java.util.List;

/**
 * A static builder for creating {@link BeanQuery} objects.
 *
 * Index annotations are not required on fields/properties.
 *
 * Querying references to other configurable instances are treated as string
 * values containing the instance id(s), single or list values (maps are treated
 * as lists).
 */
public class BeanQueryBuilder {
    /**
     * Query which asserts that a property contains a certain string.
     *
     * @param property field to query
     * @param value value to query for
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static BeanRestriction contains(String property, String value) {
        return new StringContains(property, value);
    }

    /**
     * Query which asserts that a property equals certain value.
     *
     * @param property field to query
     * @param value value to query for
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static BeanRestriction equal(String property, Object value) {
        return new Equals(property, value);
    }

    /**
     * Query which asserts that a property is between a lower and an upper bound,
     * inclusive.
     *
     * @param property field to query
     * @param lower lower value to be asserted by the query
     * @param upper upper value to be asserted by the query
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static > BeanRestriction between(String property, A lower, A upper) {
        return new Between<>(property, lower, upper);
    }

    /**
     * Query which asserts that a property is greater than (but not equal to) a value.
     *
     * @param property field to query
     * @param value value to query for
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static > BeanRestriction greaterThan(String property, A value) {
        return new GreaterThan<>(property, value);
    }

    /**
     * Query which asserts that a property is less than (but not equal to) a value.
     *
     * @param property field to query
     * @param value value to query for
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static > BeanRestriction lessThan(String property, A value) {
        return new LessThan(property, value);

    }

    /**
     * Query which asserts that an attribute has a value (is not null).
     *
     * @param property field to query
     * @return restriction to be added to {@link BeanQuery}.
     */
    /*
    public static BeanRestriction has(String property) {
        return new Has(property);
    }
    */

    /**
     * A shorthand way to create an AND query comprised of several equal queries.
     *
     * @param property field to query
     * @param values value to query for
     * @return restriction to be added to {@link BeanQuery}.
     */
    public static BeanRestriction in(String property, Object... values) {
        return new In(property, Arrays.asList(values));
    }

    /**
     * A restriction representing a logical NOT of the provided restriction.
     *
     * @param r a restriction
     * @return A NOT restriction.
     */
    public static BeanRestriction not(BeanRestriction r) {
        return new Not(Arrays.asList(r));
    }

    public static interface BeanRestriction {

    }

    public static abstract class PropertyRestriction implements BeanRestriction {
        private String property;
        private boolean isNot = false;
        public PropertyRestriction(String property) {
            this.property = property;
        }

        public String getProperty() {
            return property;
        }

        public void setNot() {
            this.isNot = true;
        }

        public boolean isNot() {
            return this.isNot;
        }

    }

    public static class StringContains extends PropertyRestriction {
        private String value;

        public StringContains(String property, String value) {
            super(property);
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public static class Equals extends PropertyRestriction {
        private Object value;

        public Equals(String property, Object value) {
            super(property);
            this.value = value;
        }

        public Object getValue() {
            return value;
        }
    }

    public static class Between> extends PropertyRestriction {
        private A lower;
        private A upper;

        public Between(String property, A lower, A upper) {
            super(property);
            this.lower = lower;
            this.upper = upper;
        }

        public A getLower() {
            return lower;
        }

        public A getUpper() {
            return upper;
        }
    }

    public static class GreaterThan> extends PropertyRestriction {
        private A value;

        public GreaterThan(String property, A value) {
            super(property);
            this.value = value;
        }

        public A getValue() {
            return value;
        }
    }

    public static class LessThan> extends PropertyRestriction {
        private A value;

        public LessThan(String property, A value) {
            super(property);
            this.value = value;
        }
        public A getValue() {
            return value;
        }
    }

    public static class Has extends PropertyRestriction {

        public Has(String property) {
            super(property);
        }
    }

    public static class In extends PropertyRestriction {
        private final List values;
        public In(String property, List values) {
            super(property);
            this.values = values;
        }

        public List getValues() {
            return values;
        }
    }

    public static abstract class LogicalRestriction implements BeanRestriction {
        private List restrictions;

        public LogicalRestriction(List restrictions) {
            this.restrictions = restrictions;
        }

        public List getRestrictions() {
            return restrictions;
        }

    }

    public static class Or extends LogicalRestriction {

        public Or(List restrictions) {
            super(restrictions);
        }
    }

    public static class And extends LogicalRestriction {

        public And(List restrictions) {
            super(restrictions);
        }
    }

    public static class Not extends LogicalRestriction {

        public Not(List restrictions) {
            super(restrictions);
        }
    }
}