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

com.github.alittlehuang.data.query.specification.Expressions Maven / Gradle / Ivy

package com.github.alittlehuang.data.query.specification;

import com.github.alittlehuang.data.query.support.ExpressionsImpl;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@FunctionalInterface
public interface Expressions extends Selection, Expression {

    @SuppressWarnings( "UnusedReturnValue" )
    R apply(T t);

    static  Expressions of(Expressions expression) {
        //noinspection unchecked
        return (Expressions) expression;
    }

    default  Expressions to(Expressions expression) {
        Expressions previous = this;
        return new Expressions() {
            List> list = new ArrayList<>();

            {
                list.add(previous);
                list.add(expression);
            }

            @Override
            public V apply(T t) {
                return null;
            }

            @Override
            public List> list() {
                return list;
            }

            @Override
            public  Expressions to(Expressions expression) {
                list.add(expression);
                //noinspection unchecked
                return (Expressions) this;
            }

        };
    }

    default List> list() {
        return Collections.singletonList(this);
    }

//    @Override
//    default String[] getNames() {
//        return GetterMethodUtil.getAttrNames(null, this);
//    }

    @Override
    default String[] getNames(Class type) {
        return GetterMethodUtil.getAttrNames(type, this);
    }

    /**
     * absolute value
     */
    static  Expressions abs(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.ABS);
    }

    static  Expressions sum(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.SUM, other);
    }

    static  Expressions sum(Expressions expression, Number val) {
        return new ExpressionsImpl<>(expression, Function.SUM, val);
    }

    /**
     * product
     */
    static  Expressions prod(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.PROD, other);
    }

    /**
     * product
     */
    static  Expressions prod(Expressions expression, Number val) {
        return new ExpressionsImpl<>(expression, Function.PROD, val);
    }

    /**
     * difference
     */
    static  Expressions diff(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.DIFF, other);
    }

    /**
     * difference
     */
    static  Expressions diff(Expressions expression, Number val) {
        return new ExpressionsImpl<>(expression, Function.DIFF, val);
    }

    /**
     * quotient
     */
    static  Expressions quot(Expressions expression, Number val) {
        return new ExpressionsImpl<>(expression, Function.QUOT, val);
    }

    /**
     * quotient
     */
    static  Expressions quot(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.QUOT, other);
    }

    /**
     * mod
     */
    static  Expressions mod(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.MOD, other);
    }

    /**
     * mod
     */
    static  Expressions mod(Expressions expression, Integer other) {
        return new ExpressionsImpl<>(expression, Function.MOD, other);
    }

    /**
     * sqrt
     */
    static  Expressions sqrt(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.SQRT);
    }

    static  Expressions concat(Expressions expression, Expressions other) {
        return new ExpressionsImpl<>(expression, Function.CONCAT, other);
    }

    static  Expressions concat(Expressions expression, String other) {
        return new ExpressionsImpl<>(expression, Function.CONCAT, other);
    }

    /**
     * Create an expression for substring extraction.
     * Extracts a substring of given length starting at the
     * specified position.
     * First position is 1.
     */
    static  Expressions substring(Expressions expression, int from, int length) {
        return new ExpressionsImpl<>(expression, Function.SUBSTRING, from, length);
    }


    /**
     * Create an expression for substring extraction.
     * Extracts a substring of given length starting at the
     * specified position.
     * First position is 1.
     */
    static  Expressions substring(Expressions expression, int from) {
        return new ExpressionsImpl<>(expression, Function.SUBSTRING, from);
    }

    /**
     * Create expression to trim blanks from both ends of
     * a string.
     */
    static  Expressions trim(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.BOTH);
    }

    /**
     * Trim from leading end
     */
    static  Expressions trimLeading(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.LEADING);
    }

    /**
     * Trim from trailing end.
     */
    static  Expressions trimTrailing(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.TRAILING);
    }


    /**
     * Create expression to trim blanks from both ends of
     * a string.
     */
    static  Expressions trim(Expressions expression, char beTrimmed) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.BOTH, beTrimmed);
    }

    /**
     * Trim from leading end
     */
    static  Expressions trimLeading(Expressions expression, char beTrimmed) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.LEADING, beTrimmed);
    }

    /**
     * Trim from trailing end.
     */
    static  Expressions trimTrailing(Expressions expression, char beTrimmed) {
        return new ExpressionsImpl<>(expression, Function.TRIM, CriteriaBuilder.Trimspec.TRAILING, beTrimmed);
    }

    static  Expressions lower(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.LOWER);
    }

    static  Expressions upper(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.UPPER);
    }

    static  Expressions length(Expressions expression) {
        return new ExpressionsImpl<>(expression, Function.LENGTH);
    }

    /**
     * Create expression to locate the position of one string
     * within another, returning position of first character
     * if found.
     * The first position in a string is denoted by 1.  If the
     * string to be located is not found, 0 is returned.
     */
    static  Expressions locate(Expressions expression, String pattern) {
        return new ExpressionsImpl<>(expression, Function.LOCATE, pattern);
    }

    /**
     * Create expression to locate the position of one string
     * within another, returning position of first character
     * if found.
     * The first position in a string is denoted by 1.  If the
     * string to be located is not found, 0 is returned.
     */
    static  Expressions locate(Expressions expression, Expressions pattern) {
        return new ExpressionsImpl<>(expression, Function.LOCATE, pattern);
    }

    /**
     * Create an expression that returns null if all its arguments evaluate to null,
     * and the value of the first non-null argument otherwise.
     */
    static > Expressions coalesceVal(E expression, Y y) {
        return new ExpressionsImpl<>(expression, Function.COALESCE, y);
    }

    /**
     * Create an expression that returns null if all its arguments evaluate to null,
     * and the value of the first non-null argument otherwise.
     */
    static > Expressions coalesce(E expression, E y) {
        return new ExpressionsImpl<>(expression, Function.COALESCE, y);
    }

    /**
     * Create an expression that tests whether its argument are
     * equal, returning null if they are and the value of the
     * first expression if they are not.
     */
    static > Expressions nullifVal(E expression, Y val) {
        return new ExpressionsImpl<>(expression, Function.NULLIF, val);
    }

    /**
     * Create an expression that tests whether its argument are
     * equal, returning null if they are and the value of the
     * first expression if they are not.
     */
    static > Expressions nullif(E expression, E val) {
        return new ExpressionsImpl<>(expression, Function.NULLIF, val);
    }

    static  Expressions function(String function, Expressions expression, Object... val) {
        return new ExpressionsImpl<>(function, expression, Function.CUSTOMIZE, val);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy