├── .github
└── workflows
│ └── webpack.yml
├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── README.md
├── README1.X.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── github
│ └── wz2cool
│ └── dynamic
│ ├── BaseDynamicQuery.java
│ ├── BaseFilterDescriptor.java
│ ├── BaseFilterGroup.java
│ ├── BaseSortDescriptor.java
│ ├── CustomFilterDescriptor.java
│ ├── CustomSortDescriptor.java
│ ├── DynamicQuery.java
│ ├── FilterCondition.java
│ ├── FilterDescriptor.java
│ ├── FilterGroupDescriptor.java
│ ├── FilterOperator.java
│ ├── FilterOperators.java
│ ├── GroupByQuery.java
│ ├── GroupedQuery.java
│ ├── LogicPagingQuery.java
│ ├── NormPagingQuery.java
│ ├── NormPagingQueryWrapper.java
│ ├── SortDescriptor.java
│ ├── SortDirection.java
│ ├── SortDirections.java
│ ├── UpDown.java
│ ├── UpdateQuery.java
│ ├── builder
│ ├── BaseConditionClauseBuilder.java
│ ├── ConditionClauseBuilder.java
│ ├── DynamicQueryBuilder.java
│ ├── DynamicQueryBuilderHelper.java
│ ├── IDynamicQueryBuilder.java
│ ├── OrderByClauseBuilder.java
│ ├── SelectClauseBuilder.java
│ ├── WhereClauseBuilder.java
│ ├── direction
│ │ ├── Ascending.java
│ │ ├── Descending.java
│ │ └── ISortDirection.java
│ └── opeartor
│ │ ├── Between.java
│ │ ├── Contains.java
│ │ ├── EndWith.java
│ │ ├── Equal.java
│ │ ├── GreaterThan.java
│ │ ├── GreaterThanOrEqual.java
│ │ ├── IFilterOperator.java
│ │ ├── IMultipleValueFilterOperator.java
│ │ ├── ISingleValueFilterOperator.java
│ │ ├── ITwoValueFilterOperator.java
│ │ ├── In.java
│ │ ├── LessThan.java
│ │ ├── LessThanOrEqual.java
│ │ ├── NotContains.java
│ │ ├── NotEqual.java
│ │ ├── NotIn.java
│ │ └── StartWith.java
│ ├── exception
│ ├── InternalRuntimeException.java
│ ├── PropertyNotFoundException.java
│ └── PropertyNotFoundInternalException.java
│ ├── helper
│ ├── CommonsHelper.java
│ ├── ParamResolverHelper.java
│ └── ReflectHelper.java
│ ├── lambda
│ ├── GetBigDecimalPropertyFunction.java
│ ├── GetBytePropertyFunction.java
│ ├── GetCommonPropertyFunction.java
│ ├── GetDatePropertyFunction.java
│ ├── GetDoublePropertyFunction.java
│ ├── GetFloatPropertyFunction.java
│ ├── GetIntegerPropertyFunction.java
│ ├── GetLongPropertyFunction.java
│ ├── GetPropertyFunction.java
│ ├── GetShortPropertyFunction.java
│ └── GetStringPropertyFunction.java
│ ├── model
│ ├── LogicPagingResult.java
│ ├── NormPagingResult.java
│ ├── PropertyInfo.java
│ └── SelectPropertyConfig.java
│ └── mybatis
│ ├── ColumnInfo.java
│ ├── EntityCache.java
│ ├── EntityHelper.java
│ ├── ExpressionHelper.java
│ ├── MybatisQueryProvider.java
│ ├── ParamExpression.java
│ ├── QueryHelper.java
│ ├── TypeHelper.java
│ ├── View.java
│ └── mapper
│ ├── DeleteByDynamicQueryMapper.java
│ ├── DynamicQueryMapper.java
│ ├── MySqlMapper.java
│ ├── SelectAvgByDynamicQueryMapper.java
│ ├── SelectByDynamicQueryMapper.java
│ ├── SelectByGroupedQueryMapper.java
│ ├── SelectCountByDynamicQueryMapper.java
│ ├── SelectMaxByDynamicQueryMapper.java
│ ├── SelectMaxByGroupedQueryMapper.java
│ ├── SelectMinByDynamicQueryMapper.java
│ ├── SelectMinByGroupedQueryMapper.java
│ ├── SelectRowBoundsByDynamicQueryMapper.java
│ ├── SelectSumByDynamicQueryMapper.java
│ ├── SelectViewByDynamicQueryMapper.java
│ ├── UpdateByDynamicQueryMapper.java
│ ├── UpdateByUpdateQueryMapper.java
│ ├── UpdateSelectiveByDynamicQueryMapper.java
│ ├── batch
│ └── MapperBatchAction.java
│ ├── constant
│ └── MapperConstants.java
│ ├── helper
│ ├── BaseEnhancedMapperTemplate.java
│ ├── DynamicQuerySqlHelper.java
│ ├── EnhancedSqlHelper.java
│ ├── GroupedQuerySqlHelper.java
│ └── LogicPagingHelper.java
│ ├── mysql
│ ├── InsertIgnoreMapper.java
│ └── InsertIgnoreSelectiveMapper.java
│ └── provider
│ ├── DynamicQueryProvider.java
│ ├── GroupedQueryProvider.java
│ └── InsertIgnoreProvider.java
└── test
├── java
└── com
│ └── github
│ └── wz2cool
│ └── dynamic
│ ├── CustomFilterDescriptorTest.java
│ ├── CustomSortDescriptorTest.java
│ ├── DbFilterTest.java
│ ├── DbSortTest.java
│ ├── DemoTest.java
│ ├── DynamicMapperTest.java
│ ├── DynamicQueryTest.java
│ ├── FilterDescriptorTest.java
│ ├── FilterGroupDescriptorTest.java
│ ├── JsonSerializeTest.java
│ ├── LogicPagingResultTest.java
│ ├── LogicPagingTest.java
│ ├── NormPagingResultTest.java
│ ├── SortDescriptorTest.java
│ ├── TestApplication.java
│ ├── TransientTest.java
│ ├── ViewTest.java
│ ├── builder
│ ├── DynamicQueryBuilderHelperTest.java
│ ├── DynamicQueryBuilderTest.java
│ ├── OrderByClauseBuilderTest.java
│ ├── SelectClauseBuilderTest.java
│ └── WhereClauseBuilderTest.java
│ ├── helper
│ ├── CommonsHelperTest.java
│ └── ReflectHelperTest.java
│ ├── model
│ ├── Bug.java
│ ├── ChildClass.java
│ ├── ExampleModel.java
│ ├── HelloWorld.java
│ ├── ParentClass.java
│ └── Student.java
│ └── mybatis
│ ├── EntityCacheTest.java
│ ├── EntityHelperTest.java
│ ├── ExpressionHelperTest.java
│ ├── MybatisQueryProviderTest.java
│ ├── QueryHelperTest.java
│ ├── db
│ ├── mapper
│ │ ├── BugDao.java
│ │ ├── CategoryGroupCountMapper.java
│ │ ├── CategoryGroupCountMapper2.java
│ │ ├── NorthwindDao.java
│ │ ├── ProductDao.java
│ │ ├── ProductViewMapper.java
│ │ ├── StudentMapper.java
│ │ └── UserDao.java
│ └── model
│ │ └── entity
│ │ ├── group
│ │ └── CategoryGroupCount.java
│ │ ├── table
│ │ ├── Category.java
│ │ ├── Product.java
│ │ ├── Product2.java
│ │ ├── Product3.java
│ │ ├── StudentDO.java
│ │ └── User.java
│ │ └── view
│ │ ├── ProductBaseView.java
│ │ └── ProductView.java
│ └── mapper
│ ├── constant
│ └── MapperConstantsTest.java
│ └── helper
│ └── DynamicQuerySqlHelperTest.java
└── resources
├── com
└── github
│ └── wz2cool
│ └── dynamic
│ └── mybatis
│ └── db
│ └── mapper
│ └── NorthwindDao.xml
├── config
├── application-h2.properties
├── application-mysql.properties
├── application-postresql.properties
├── application-sqlserver.properties
└── application.properties
├── mybatis-config.xml
├── schema-h2.sql
├── schema-mysql.sql
├── schema-postgresql.sql
└── schema-sqlserver.sql
/.github/workflows/webpack.yml:
--------------------------------------------------------------------------------
1 | name: Tests
2 | on: [push, pull_request]
3 | env:
4 | CI: true
5 |
6 | jobs:
7 | build:
8 | runs-on: ubuntu-latest
9 |
10 | steps:
11 | - uses: actions/checkout@v4
12 | - name: Set up JDK 8
13 | uses: actions/setup-java@v4
14 | with:
15 | java-version: '8'
16 | distribution: 'temurin'
17 | cache: maven
18 | - name: Build with Maven
19 | run: mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent package org.jacoco:jacoco-maven-plugin:report -B
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | sudo: false
3 | install: true
4 |
5 |
6 | addons:
7 | sonarcloud:
8 | organization: "wz2cool-github" # the key of the org you chose at step #3
9 | token:
10 | secure: "Zd5JBJiCw7OrIgZz5bBw7gc2Konte7ZrjmsYcF8O/uGktDSMI9vIW1eVwYpb9GbOpou+OyvAvyMP5yFhbzcjtXDrylxqO+Kywl145CAuq6M00ITf00kajWTuNYWfhI41izV22oaFHDjf1xoWpH9EME7+bZjYWNrN6F+Y8eMoYw/ur+8dyKlcZDuDgSw6feBsl6Ii017neiuU1JGoSbS/HuWox6h+tGB3u2avjNgkVaTRwhoqIloP27ljfDMI9holzAD5n6lJycQsslkwkzlSawo/9I0G7a/Womjf+p3MjJmCdyQAsjh0YA7saPDxoxa21nrj2wQ8VpVqqRB1PX0w1gcznLDwGKa0KZmL9sjXSLYJhZDx9a9n819vF9b+uzSyxYsIcBhP9xtxzgYb+K8kkuG/jKNOsPl9n/KD8ccBWB9P1gIN9aKy88VTeTG5Y0u0cvDfWG4VMwZEmJXmQaOPwOaMSOFtpyYgngBITgYDDQ1kZbOiGEV9Afx+MJmFm12vC+kQUd9krXI3lpWUMqx/Rz6WsB0IiJq13C8tctPIaZckhrzpYc2QcC4F4Bcfk7En8LDOb33f0pEFPPErLvlKGBplGgTiOkLuaI2nMlZuqSShhsFQJJY/TRUWUkqTA9nhB7xM3jtbTdURO9sVY7gByDl6iRAPeUm9SjXoKtaA9+A=" # encrypted value of your token
11 | script:
12 | - mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent package org.jacoco:jacoco-maven-plugin:report -B
13 | # setup JDK 11 for sonar scan
14 | - wget https://github.com/sormuras/bach/raw/master/install-jdk.sh && . ./install-jdk.sh -F 11 -L GPL
15 |
16 | after_success:
17 | - mvn -q clean test jacoco:report coveralls:report
18 |
19 | cache:
20 | directories:
21 | - '$HOME/.m2/repository'
22 | - '$HOME/.sonar/cache'
23 |
24 | jdk:
25 | - openjdk8
26 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | ## [v1.0.1](https://github.com/wz2cool/mybatis-dynamic-query/tree/v1.0.1) (2017-07-24)
4 |
5 | **Feature:**
6 | - merge "QueryColumn" annotation and "DbColumn" annotation to Column annotation.
7 | - change "DbTable" annotation to "Table" annotation
8 |
9 | ## [v1.0.2](https://github.com/wz2cool/mybatis-dynamic-query/tree/v1.0.1) (2017-08-02)
10 | **Feature**
11 | - add bulk insert expression
12 | - add delete expression
13 | - add jdbcType and insertIgnore property in Column annotation
14 | - add jdbcType when generating insert/update/delete expression enhancement
15 | - add method to support lambda expression to get field name.
16 |
17 | **Bug fix**
18 | - [sql server] update will throw exception bug
19 |
20 | ## [v2.0.0](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.0) (2017-08-14)
21 | **Feature**
22 | - integrate tk.mybatis.mapper
23 | - add DynamicQueryMapper
24 | - remove generating insert/delete method since we can use DynamicQueryMapper.
25 | - add CustomFilterDescriptor
26 | - support serialize FilterDescriptor/FilterGroupDescriptor/CustomFilterDescriptor to json
27 |
28 | ## [v2.0.1](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.1) (2017-09-11)
29 | **Feature**
30 | - support serialize DynamicQuery to json
31 |
32 | ## [v2.0.2](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.2) (2017-10-05)
33 | **Feature**
34 | - add custom sort descriptor
35 | - get query column ([tableName].[columnName])
36 | - createInstance for MybatisQueryProvider
37 |
38 | ## [v2.0.3](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.3) (2018-11-14)
39 | **Feature**
40 | - add Select Fields(columns)
41 |
42 | ## [v2.0.4](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.4) (2018-11-18)
43 | **Feature**
44 | - change select fields to select property
45 |
46 | ## [v2.0.5](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.5) (2018-11-18)
47 | **bug**
48 | - change "selectProperties" field to "selectedProperties"
49 |
50 | ## [v2.0.6](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.6) (2018-11-18)
51 | **Feature**
52 | - add MapUnderscoreToCamelCase to dynamic query
53 |
54 | ## [v2.0.7](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.7) (2019-04-12)
55 | **Feature**
56 | - remove MapUnderscoreToCamelCase from dynamic query
57 | - read MapUnderscoreToCamelCase from config
58 |
59 | ## [v2.0.9](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.9) (2019-04-13)
60 | **Bug**
61 | - fix mapping issue
62 |
63 | ## [v2.0.10](https://github.com/wz2cool/mybatis-dynamic-query/tree/v2.0.10) (2019-05-10)
64 | **Bug**
65 | - change selectCount result type from long to int
66 |
67 | ## v2.0.11 (2019-05-10)
68 | **Bug**
69 | - fix sonar issue
70 |
71 | ## v2.0.12 (2019-05-22)
72 | **feature**
73 | - add InsertList support
--------------------------------------------------------------------------------
/README1.X.md:
--------------------------------------------------------------------------------
1 | MyBatis Dynamic Query
2 | =====================================
3 |
4 | [](http://www.apache.org/licenses/LICENSE-2.0.html)
5 | [](https://travis-ci.org/wz2cool/mybatis-dynamic-query)
6 | [](https://coveralls.io/github/wz2cool/mybatis-dynamic-query?branch=master)
7 | [](https://www.versioneye.com/user/projects/597283ce368b08005906060c)
8 | [](https://maven-badges.herokuapp.com/maven-central/com.github.wz2cool/mybatis-dynamic-query)
9 |
10 | The MyBatis Dynamic Query framework makes it easier to generate "where" and "order" expression dynamically in mapper xml.
11 | mybatis-dynamic-query comes to solve four problem:
12 | - no need write lots of code in xml.
13 | - filtering or sorting maintained by java code.
14 | - hot update "where" and "order" expression.
15 | - save filter or sort descriptor and re-use them.
16 |
17 | ## Database support
18 | - H2
19 | - MySql
20 | - SqlServer
21 | - Postresql (BETA)
22 | - Oracle (TODO)
23 |
24 | ## Maven
25 | ```xml
26 |
27 | com.github.wz2cool
28 | mybatis-dynamic-query
29 | 1.0.2
30 |
31 | ```
32 |
33 | ## Example
34 | - create a table by sql.
35 | ```sql
36 | CREATE TABLE product (
37 | product_id INT PRIMARY KEY,
38 | category_id INT NOT NULL,
39 | product_name VARCHAR (50) NOT NULL,
40 | price DECIMAL
41 | );
42 | ```
43 | - create a model map to this table.
44 | ```java
45 | public class Product {
46 | @Column(name = "product_id") // custom column name
47 | private Integer productId;
48 | private String productName;
49 | private BigDecimal price;
50 | private Integer categoryId;
51 |
52 | // get, set method.
53 | }
54 | ```
55 | - create a dynamic select in mapper interface / xml.
56 | ```java
57 | List getProductByDynamic(Map params);
58 | ```
59 | ```xml
60 |
66 | ```
67 | - generate expression and param map (NOTE: expression string also put into map).
68 | ```java
69 | @Test
70 | public void simpleDemo() throws Exception {
71 | and
72 | FilterDescriptor idFilter =
73 | new FilterDescriptor(FilterCondition.AND, "productId", FilterOperator.EQUAL, 2);
74 | and
75 | Map queryParams =
76 | mybatisQueryProvider.getWhereQueryParamMap(
77 | Product.class, "whereExpression", idFilter);
78 | // pass query params.
79 | Product productView =
80 | northwindDao.getProductByDynamic(queryParams).stream().findFirst().orElse(null);
81 |
82 | assertEquals(Integer.valueOf(2), productView.getProductID());
83 | }
84 | ```
85 | ## Docs
86 | [中文文档](https://wz2cool.gitbooks.io/mybatis-dynamic-query-zh-cn/content/)
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/BaseFilterDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonSubTypes;
4 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
5 |
6 | /**
7 | * @author Frank
8 | */
9 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, property = "type")
10 | @JsonSubTypes({
11 | @JsonSubTypes.Type(value = FilterDescriptor.class, name = "filterDescriptor"),
12 | @JsonSubTypes.Type(value = FilterGroupDescriptor.class, name = "filterGroupDescriptor"),
13 | @JsonSubTypes.Type(value = CustomFilterDescriptor.class, name = "customFilterDescriptor")
14 | })
15 | @SuppressWarnings("java:S2326")
16 | public interface BaseFilterDescriptor {
17 | /**
18 | * get condition of and
19 | *
20 | * @return condition of and
21 | */
22 | FilterCondition getCondition();
23 |
24 | /**
25 | * set condition
26 | *
27 | * @param condition condition of and
28 | */
29 | void setCondition(FilterCondition condition);
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/BaseSortDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonSubTypes;
4 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
5 |
6 | /**
7 | * @author Frank
8 | */
9 | @SuppressWarnings("squid:S1610")
10 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, property = "type")
11 | @JsonSubTypes({
12 | @JsonSubTypes.Type(value = SortDescriptor.class, name = "SortDescriptor"),
13 | @JsonSubTypes.Type(value = CustomSortDescriptor.class, name = "CustomSortDescriptor")
14 | })
15 | public abstract class BaseSortDescriptor {
16 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/CustomFilterDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * @author Frank
9 | */
10 | @SuppressWarnings("squid:S1948")
11 | @JsonTypeName("customFilterDescriptor")
12 | public class CustomFilterDescriptor implements Serializable, BaseFilterDescriptor {
13 | private static final long serialVersionUID = 7448086874396793224L;
14 |
15 | private FilterCondition condition = FilterCondition.AND;
16 |
17 | private String expression;
18 | private Object[] params;
19 |
20 | public String getExpression() {
21 | return expression;
22 | }
23 |
24 | public void setExpression(String expression) {
25 | this.expression = expression;
26 | }
27 |
28 | public Object[] getParams() {
29 | return params;
30 | }
31 |
32 | public void setParams(Object... params) {
33 | this.params = params;
34 | }
35 |
36 | public CustomFilterDescriptor() {
37 | // create empty constructor
38 | }
39 |
40 | public CustomFilterDescriptor(String expression, Object... params) {
41 | this.setExpression(expression);
42 | this.setParams(params);
43 | }
44 |
45 | public CustomFilterDescriptor(FilterCondition condition, String expression, Object... params) {
46 | this.setCondition(condition);
47 | this.setExpression(expression);
48 | this.setParams(params);
49 | }
50 |
51 | @Override
52 | public FilterCondition getCondition() {
53 | return condition;
54 | }
55 |
56 | @Override
57 | public void setCondition(FilterCondition condition) {
58 | this.condition = condition;
59 | }
60 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/CustomSortDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * @author Frank
9 | */
10 | @SuppressWarnings("squid:S1948")
11 | @JsonTypeName("customSortDescriptor")
12 | public class CustomSortDescriptor extends BaseSortDescriptor implements Serializable {
13 | private static final long serialVersionUID = -8776490725097358688L;
14 | private String expression;
15 | private Object[] params;
16 |
17 | public String getExpression() {
18 | return expression;
19 | }
20 |
21 | public void setExpression(String expression) {
22 | this.expression = expression;
23 | }
24 |
25 | public Object[] getParams() {
26 | return params;
27 | }
28 |
29 | public void setParams(Object... params) {
30 | this.params = params;
31 | }
32 |
33 | public CustomSortDescriptor() {
34 | // create empty constructor
35 | }
36 |
37 | public CustomSortDescriptor(String expression, Object... params) {
38 | this.setExpression(expression);
39 | this.setParams(params);
40 | }
41 | }
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/DynamicQuery.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4 | import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
5 | import com.github.wz2cool.dynamic.helper.CommonsHelper;
6 | import com.github.wz2cool.dynamic.lambda.GetCommonPropertyFunction;
7 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
8 | import com.github.wz2cool.dynamic.mybatis.ParamExpression;
9 | import com.github.wz2cool.dynamic.mybatis.QueryHelper;
10 | import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
11 | import org.apache.commons.lang3.ArrayUtils;
12 |
13 | import java.util.Map;
14 |
15 | /**
16 | * @author Frank
17 | **/
18 | @JsonIgnoreProperties(ignoreUnknown = true)
19 | public class DynamicQuery extends BaseDynamicQuery> {
20 |
21 | private static final long serialVersionUID = -4044703018297658438L;
22 |
23 | public DynamicQuery() {
24 | // for json
25 | }
26 |
27 | public DynamicQuery(Class entityClass) {
28 | this.setEntityClass(entityClass);
29 | }
30 |
31 | public static DynamicQuery createQuery(Class entityClass) {
32 | return new DynamicQuery<>(entityClass);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/FilterCondition.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * The enum Filter condition.
7 | * @author Frank
8 | */
9 | public enum FilterCondition implements Serializable {
10 | /**
11 | * And and condition.
12 | */
13 | AND,
14 | /**
15 | * Or and condition.
16 | */
17 | OR
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/FilterDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | /**
6 | * @author Frank
7 | **/
8 | @SuppressWarnings({"squid:S1172", "squid:S2326"})
9 | @JsonTypeName("filterDescriptor")
10 | public class FilterDescriptor implements BaseFilterDescriptor {
11 |
12 | private FilterCondition condition = FilterCondition.AND;
13 | private FilterOperator operator = FilterOperator.EQUAL;
14 | private String propertyName;
15 | private Object value;
16 |
17 | @Override
18 | public FilterCondition getCondition() {
19 | return condition;
20 | }
21 |
22 | @Override
23 | public void setCondition(FilterCondition condition) {
24 | this.condition = condition;
25 | }
26 |
27 | public FilterOperator getOperator() {
28 | return operator;
29 | }
30 |
31 | public void setOperator(FilterOperator operator) {
32 | this.operator = operator;
33 | }
34 |
35 | public String getPropertyName() {
36 | return propertyName;
37 | }
38 |
39 | public void setPropertyName(String propertyName) {
40 | this.propertyName = propertyName;
41 | }
42 |
43 | public Object getValue() {
44 | return value;
45 | }
46 |
47 | public void setValue(Object value) {
48 | this.value = value;
49 | }
50 |
51 | public FilterDescriptor() {
52 | // for json
53 | }
54 |
55 | public FilterDescriptor(String propertyName, FilterOperator operator, Object value) {
56 | this.propertyName = propertyName;
57 | this.operator = operator;
58 | this.value = value;
59 | }
60 |
61 | public FilterDescriptor(FilterCondition condition, String propertyName, FilterOperator operator, Object value) {
62 | this.condition = condition;
63 | this.propertyName = propertyName;
64 | this.operator = operator;
65 | this.value = value;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/FilterGroupDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | /**
6 | * @author Frank
7 | **/
8 | @SuppressWarnings("squid:S1172")
9 | @JsonTypeName("filterGroupDescriptor")
10 | public class FilterGroupDescriptor
11 | extends BaseFilterGroup>
12 | implements BaseFilterDescriptor {
13 |
14 | private FilterCondition condition = FilterCondition.AND;
15 |
16 | private Class clazz;
17 |
18 | public FilterGroupDescriptor() {
19 | // hide construct
20 | }
21 |
22 | public FilterGroupDescriptor(Class clazz) {
23 | this.clazz = clazz;
24 | }
25 |
26 | public static FilterGroupDescriptor create(Class clazz) {
27 | return new FilterGroupDescriptor<>(clazz);
28 | }
29 |
30 | public Class getClazz() {
31 | return clazz;
32 | }
33 |
34 | @Override
35 | public FilterCondition getCondition() {
36 | return condition;
37 | }
38 |
39 | @Override
40 | public void setCondition(FilterCondition condition) {
41 | this.condition = condition;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/FilterOperator.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 |
4 | import java.io.Serializable;
5 |
6 | /**
7 | * The enum Filter operator.
8 | * @author Frank
9 | */
10 | public enum FilterOperator implements Serializable {
11 | /**
12 | * Less than and operator.
13 | */
14 | LESS_THAN,
15 | /**
16 | * Less than or equal and operator.
17 | */
18 | LESS_THAN_OR_EQUAL,
19 | /**
20 | * Equal and operator.
21 | */
22 | EQUAL,
23 | /**
24 | * Not equal and operator.
25 | */
26 | NOT_EQUAL,
27 | /**
28 | * Greater than or equal and operator.
29 | */
30 | GREATER_THAN_OR_EQUAL,
31 | /**
32 | * Greater than and operator.
33 | */
34 | GREATER_THAN,
35 | /**
36 | * Start with and operator.
37 | */
38 | START_WITH,
39 | /**
40 | * End with and operator.
41 | */
42 | END_WITH,
43 | /**
44 | * Contains and operator.
45 | */
46 | CONTAINS,
47 | /**
48 | * In and operator.
49 | */
50 | IN,
51 | /**
52 | * Not in and operator.
53 | */
54 | NOT_IN,
55 | /**
56 | * Between and operator.
57 | */
58 | BETWEEN,
59 | /**
60 | *
61 | */
62 | NOT_CONTAINS
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/FilterOperators.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.github.wz2cool.dynamic.builder.opeartor.*;
4 |
5 | import java.util.Collection;
6 |
7 | public final class FilterOperators {
8 |
9 | /// region and operator
10 |
11 | public LessThan lessThan(R value) {
12 | return new LessThan<>(value);
13 | }
14 |
15 | public LessThanOrEqual lessThanOrEqual(R value) {
16 | return new LessThanOrEqual<>(value);
17 | }
18 |
19 | public Equal isEqual(R value) {
20 | return new Equal<>(value);
21 | }
22 |
23 | public NotEqual notEqual(R value) {
24 | return new NotEqual<>(value);
25 | }
26 |
27 | public GreaterThanOrEqual greaterThanOrEqual(R value) {
28 | return new GreaterThanOrEqual<>(value);
29 | }
30 |
31 | public GreaterThan greaterThan(R value) {
32 | return new GreaterThan<>(value);
33 | }
34 |
35 | public StartWith startWith(R value) {
36 | return new StartWith<>(value);
37 | }
38 |
39 | public EndWith endWith(R value) {
40 | return new EndWith<>(value);
41 | }
42 |
43 | public Contains contains(R value) {
44 | return new Contains<>(value);
45 | }
46 |
47 | public NotContains notContains(R value) {
48 | return new NotContains<>(value);
49 | }
50 |
51 | @SafeVarargs
52 | public final In in(R... values) {
53 | return new In<>(values);
54 | }
55 |
56 | public In in(Collection values) {
57 | return new In<>(values);
58 | }
59 |
60 | @SafeVarargs
61 | public final NotIn notIn(R... values) {
62 | return new NotIn<>(values);
63 | }
64 |
65 | public NotIn notIn(Collection values) {
66 | return new NotIn<>(values);
67 | }
68 |
69 | public Between between(R value1, R value2) {
70 | return new Between<>(value1, value2);
71 | }
72 |
73 | /// endregion
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/GroupedQuery.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
4 | import com.github.wz2cool.dynamic.helper.CommonsHelper;
5 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
6 | import com.github.wz2cool.dynamic.mybatis.ParamExpression;
7 | import com.github.wz2cool.dynamic.mybatis.QueryHelper;
8 | import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
9 | import org.apache.commons.lang3.ArrayUtils;
10 |
11 | import java.util.Map;
12 |
13 | /**
14 | * @author Frank
15 | **/
16 | public class GroupedQuery extends BaseFilterGroup> {
17 |
18 | private static final QueryHelper QUERY_HELPER = new QueryHelper();
19 | private final GroupByQuery groupByQuery;
20 |
21 | public GroupedQuery(GroupByQuery groupByQuery) {
22 | this.groupByQuery = groupByQuery;
23 | }
24 |
25 | public Class getQueryClass() {
26 | return this.groupByQuery.tQueryClass;
27 | }
28 |
29 | public Class getSelectClass() {
30 | return this.groupByQuery.tSelectClass;
31 | }
32 |
33 | /// region sort
34 |
35 | private BaseSortDescriptor[] sorts = new BaseSortDescriptor[]{};
36 |
37 | public BaseSortDescriptor[] getSorts() {
38 | return sorts;
39 | }
40 |
41 | public void setSorts(BaseSortDescriptor[] sorts) {
42 | this.sorts = sorts;
43 | }
44 |
45 | public void addSorts(BaseSortDescriptor... newSorts) {
46 | setSorts(ArrayUtils.addAll(sorts, newSorts));
47 | }
48 |
49 | public void removeSorts(BaseSortDescriptor... newSorts) {
50 | for (BaseSortDescriptor newSort : newSorts) {
51 | setSorts(ArrayUtils.removeAllOccurences(sorts, newSort));
52 | }
53 | }
54 |
55 | public GroupedQuery orderBy(GetPropertyFunction getPropertyFunc, ISortDirection sortDirection) {
56 | return orderBy(true, getPropertyFunc, sortDirection);
57 | }
58 |
59 | public GroupedQuery orderBy(
60 | boolean enable, GetPropertyFunction getPropertyFunc, ISortDirection sortDirection) {
61 | if (enable) {
62 | String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
63 | SortDirection direction = sortDirection.getDirection();
64 | SortDescriptor sortDescriptor = new SortDescriptor();
65 | sortDescriptor.setPropertyName(propertyName);
66 | sortDescriptor.setDirection(direction);
67 | addSorts(sortDescriptor);
68 | }
69 | return this;
70 | }
71 |
72 | public GroupedQuery orderByNull() {
73 | // 增加性能
74 | SortDescriptor sortDescriptor = new SortDescriptor();
75 | sortDescriptor.setPropertyName(null);
76 | sortDescriptor.setDirection(SortDirection.DESC);
77 | addSorts(sortDescriptor);
78 | return this;
79 | }
80 |
81 | /// endregion
82 |
83 | public Map toQueryParamMap(boolean isMapUnderscoreToCamelCase) {
84 | // 筛选
85 | ParamExpression whereParamExpression = QUERY_HELPER.toWhereExpression(
86 | this.getQueryClass(), this.groupByQuery.getFilters());
87 | String whereExpression = whereParamExpression.getExpression();
88 | Map paramMap = whereParamExpression.getParamMap();
89 | for (Map.Entry param : paramMap.entrySet()) {
90 | String key = param.getKey();
91 | String newKey = String.format("%s.%s", MapperConstants.GROUPED_QUERY_PARAMS, key);
92 | whereExpression = whereExpression.replace(key, newKey);
93 | }
94 | paramMap.put(MapperConstants.WHERE_EXPRESSION, whereExpression);
95 | // 分组
96 | String groupColumnExpression = QUERY_HELPER.toGroupByColumnsExpression(
97 | this.groupByQuery.tQueryClass, this.groupByQuery.getGroupedProperties());
98 | paramMap.put(MapperConstants.GROUP_COLUMNS_EXPRESSION, groupColumnExpression);
99 | // having
100 | ParamExpression havingParamExpression = QUERY_HELPER.toWhereExpression(
101 | this.getSelectClass(), this.getFilters());
102 | String havingExpression = havingParamExpression.getExpression();
103 | for (Map.Entry param : havingParamExpression.getParamMap().entrySet()) {
104 | String key = param.getKey();
105 | String newKey = String.format("%s.%s", MapperConstants.GROUPED_QUERY_PARAMS, key);
106 | havingExpression = havingExpression.replace(key, newKey);
107 | paramMap.put(key, param.getValue());
108 | }
109 | paramMap.put(MapperConstants.HAVING_EXPRESSION, havingExpression);
110 | // 排序
111 | ParamExpression sortExpression = QUERY_HELPER.toSortExpression(this.getSelectClass(), sorts);
112 | paramMap.put(MapperConstants.SORT_EXPRESSION, sortExpression.getExpression());
113 | // 选择
114 | String selectColumnExpression = QUERY_HELPER.toSelectColumnsExpression(
115 | this.groupByQuery.tSelectClass,
116 | this.groupByQuery.getSelectedProperties(),
117 | this.groupByQuery.getIgnoredProperties(), isMapUnderscoreToCamelCase, false);
118 | paramMap.put(MapperConstants.SELECT_COLUMNS_EXPRESSION, selectColumnExpression);
119 | this.groupByQuery.initDefaultQueryParams();
120 | paramMap.putAll(this.groupByQuery.getCustomDynamicQueryParams());
121 | return paramMap;
122 |
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/NormPagingQuery.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | /**
4 | * @author Frank
5 | */
6 | public class NormPagingQuery extends BaseDynamicQuery> {
7 |
8 | private final int pageNum;
9 | private final int pageSize;
10 | private final boolean calcTotal;
11 | private boolean autoBackIfEmpty;
12 |
13 | public int getPageNum() {
14 | return pageNum;
15 | }
16 |
17 | public int getPageSize() {
18 | return pageSize;
19 | }
20 |
21 | public boolean isCalcTotal() {
22 | return calcTotal;
23 | }
24 |
25 | public boolean isAutoBackIfEmpty() {
26 | return autoBackIfEmpty;
27 | }
28 |
29 | public void setAutoBackIfEmpty(boolean autoBackIfEmpty) {
30 | this.autoBackIfEmpty = autoBackIfEmpty;
31 | }
32 |
33 | private NormPagingQuery(Class clazz, int pageNum, int pageSize, boolean autoBackIfEmpty, boolean calcTotal) {
34 | this.setEntityClass(clazz);
35 | this.pageNum = pageNum;
36 | this.pageSize = pageSize;
37 | this.autoBackIfEmpty = autoBackIfEmpty;
38 | this.calcTotal = calcTotal;
39 | }
40 |
41 | public static NormPagingQuery createQuery(
42 | Class clazz, int pageNum, int pageSize, boolean autoBackIfEmpty, boolean calcTotal) {
43 | return new NormPagingQuery<>(clazz, pageNum, pageSize, autoBackIfEmpty, calcTotal);
44 | }
45 |
46 | public static NormPagingQuery createQuery(
47 | Class clazz, int pageNum, int pageSize, boolean autoBackIfEmpty) {
48 | return new NormPagingQuery<>(clazz, pageNum, pageSize, autoBackIfEmpty, true);
49 | }
50 |
51 | public static NormPagingQuery createQuery(Class clazz, int pageNum, int pageSize) {
52 | return new NormPagingQuery<>(clazz, pageNum, pageSize, false, true);
53 | }
54 |
55 | public DynamicQuery getDynamicQuery() {
56 | DynamicQuery dynamicQuery = DynamicQuery.createQuery(getEntityClass());
57 | dynamicQuery.addFilters(this.getFilters());
58 | dynamicQuery.addSorts(this.getSorts());
59 | dynamicQuery.setDistinct(this.isDistinct());
60 | dynamicQuery.setSelectedProperties(this.getSelectedProperties());
61 | dynamicQuery.setIgnoredProperties(this.getIgnoredProperties());
62 | dynamicQuery.customDynamicQueryParams.putAll(this.customDynamicQueryParams);
63 | return dynamicQuery;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/NormPagingQueryWrapper.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | /**
4 | * @author Frank
5 | **/
6 | public class NormPagingQueryWrapper> {
7 |
8 | private final S searchQuery;
9 | private int pageNum = 1;
10 | private int pageSize = 10;
11 | private boolean calcTotal = true;
12 | private boolean autoBackIfEmpty = false;
13 |
14 | public S getSearchQuery() {
15 | return searchQuery;
16 | }
17 |
18 | public int getPageNum() {
19 | return pageNum;
20 | }
21 |
22 | public int getPageSize() {
23 | return pageSize;
24 | }
25 |
26 | public boolean isCalcTotal() {
27 | return calcTotal;
28 | }
29 |
30 | public boolean isAutoBackIfEmpty() {
31 | return autoBackIfEmpty;
32 | }
33 |
34 | public void setPageNum(int pageNum) {
35 | this.pageNum = pageNum;
36 | }
37 |
38 | public void setPageSize(int pageSize) {
39 | this.pageSize = pageSize;
40 | }
41 |
42 | public void setCalcTotal(boolean calcTotal) {
43 | this.calcTotal = calcTotal;
44 | }
45 |
46 | public void setAutoBackIfEmpty(boolean autoBackIfEmpty) {
47 | this.autoBackIfEmpty = autoBackIfEmpty;
48 | }
49 |
50 | public NormPagingQueryWrapper(S searchQuery) {
51 | this.searchQuery = searchQuery;
52 | }
53 |
54 | public static > NormPagingQueryWrapper create(S searchQuery) {
55 | return new NormPagingQueryWrapper<>(searchQuery);
56 | }
57 |
58 | public static > NormPagingQueryWrapper create(S searchQuery, int pageNum, int pageSize) {
59 | return create(searchQuery, pageNum, pageSize, true);
60 | }
61 |
62 | public static > NormPagingQueryWrapper create(
63 | S searchQuery, int pageNum, int pageSize, boolean calcTotal) {
64 | return create(searchQuery, pageNum, pageSize, calcTotal, false);
65 | }
66 |
67 | public static > NormPagingQueryWrapper create(
68 | S searchQuery, int pageNum, int pageSize, boolean calcTotal, boolean autoBackIfEmpty) {
69 | final NormPagingQueryWrapper normPagingQueryWrapper = new NormPagingQueryWrapper<>(searchQuery);
70 | normPagingQueryWrapper.setPageNum(pageNum);
71 | normPagingQueryWrapper.setPageSize(pageSize);
72 | normPagingQueryWrapper.setCalcTotal(calcTotal);
73 | normPagingQueryWrapper.setAutoBackIfEmpty(autoBackIfEmpty);
74 | return normPagingQueryWrapper;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/SortDescriptor.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 |
4 | import com.github.wz2cool.dynamic.helper.CommonsHelper;
5 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
6 |
7 | import java.io.Serializable;
8 |
9 | /**
10 | * The type Sort descriptor.
11 | *
12 | * @author Frank
13 | */
14 | public class SortDescriptor extends BaseSortDescriptor implements Serializable {
15 | private static final long serialVersionUID = 819843464658066502L;
16 |
17 | private String propertyName;
18 | private SortDirection direction = SortDirection.ASC;
19 |
20 | /**
21 | * Instantiates a new Sort descriptor.
22 | */
23 | public SortDescriptor() {
24 | // create a empty instance.
25 | }
26 |
27 | /**
28 | * Instantiates a new Sort descriptor.
29 | *
30 | * @param propertyName the property path
31 | * @param direction the sort direction
32 | */
33 | public SortDescriptor(String propertyName, SortDirection direction) {
34 | this.propertyName = propertyName;
35 | this.direction = direction;
36 | }
37 |
38 | public SortDescriptor(GetPropertyFunction getFieldFunc, SortDirection direction) {
39 | this.propertyName = CommonsHelper.getPropertyInfo(getFieldFunc).getPropertyName();
40 | this.direction = direction;
41 | }
42 |
43 | /**
44 | * Gets property path.
45 | *
46 | * @return the property path
47 | */
48 | public String getPropertyName() {
49 | return propertyName;
50 | }
51 |
52 | /**
53 | * Sets property path.
54 | *
55 | * @param propertyName the property path
56 | */
57 | public void setPropertyName(String propertyName) {
58 | this.propertyName = propertyName;
59 | }
60 |
61 | public void setPropertyPath(GetPropertyFunction getFieldFunc) {
62 | this.propertyName = CommonsHelper.getPropertyInfo(getFieldFunc).getPropertyName();
63 | }
64 |
65 | /**
66 | * Gets sort direction.
67 | *
68 | * @return the sort direction
69 | */
70 | public SortDirection getDirection() {
71 | return direction;
72 | }
73 |
74 | /**
75 | * Sets sort direction.
76 | *
77 | * @param direction the sort direction
78 | */
79 | public void setDirection(SortDirection direction) {
80 | this.direction = direction;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/SortDirection.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 |
4 | import java.io.Serializable;
5 |
6 | /**
7 | * The enum Sort direction.
8 | * @author Frank
9 | */
10 | public enum SortDirection implements Serializable {
11 | /**
12 | * Asc sort direction.
13 | */
14 | ASC,
15 | /**
16 | * Desc sort direction.
17 | */
18 | DESC
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/SortDirections.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 |
4 | import com.github.wz2cool.dynamic.builder.direction.Ascending;
5 | import com.github.wz2cool.dynamic.builder.direction.Descending;
6 |
7 | /**
8 | * The enum Sort direction.
9 | *
10 | * @author Frank
11 | */
12 | public final class SortDirections {
13 | /// region sort direction
14 |
15 | private static final Ascending ASC = new Ascending();
16 | private static final Descending DESC = new Descending();
17 |
18 | public Ascending asc() {
19 | return ASC;
20 | }
21 |
22 | public Descending desc() {
23 | return DESC;
24 | }
25 |
26 | /// endregion
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/UpDown.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic;
2 |
3 | /**
4 | * @author Frank
5 | **/
6 | public enum UpDown {
7 | /**
8 | * NONE
9 | */
10 | NONE,
11 | /**
12 | * UP
13 | */
14 | UP,
15 | /**
16 | * DOWN
17 | */
18 | DOWN
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/ConditionClauseBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder;
2 |
3 | import com.github.wz2cool.dynamic.*;
4 | import com.github.wz2cool.dynamic.builder.opeartor.IFilterOperator;
5 | import com.github.wz2cool.dynamic.helper.CommonsHelper;
6 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
7 | import org.apache.commons.lang3.ArrayUtils;
8 |
9 | /**
10 | * @author Frank
11 | */
12 | public class ConditionClauseBuilder extends BaseConditionClauseBuilder, T> {
13 |
14 | private final FilterCondition condition;
15 | private final String propertyName;
16 | private final FilterOperator filterOperator;
17 | private final Object value;
18 | private final ConditionClauseBuilder[] subConditionClauseBuilders;
19 |
20 | public ConditionClauseBuilder(
21 | FilterCondition condition,
22 | GetPropertyFunction getPropertyFunction,
23 | IFilterOperator operator,
24 | ConditionClauseBuilder[] subConditionClauseBuilders) {
25 | this.condition = condition;
26 | this.propertyName = CommonsHelper.getPropertyName(getPropertyFunction);
27 | this.filterOperator = operator.getOperator();
28 | this.value = operator.getValue();
29 | this.subConditionClauseBuilders = subConditionClauseBuilders;
30 | }
31 |
32 | public BaseFilterDescriptor toFilter() {
33 | FilterDescriptor filterDescriptor = new FilterDescriptor();
34 |
35 | filterDescriptor.setCondition(this.condition);
36 | filterDescriptor.setPropertyName(this.propertyName);
37 | filterDescriptor.setOperator(this.filterOperator);
38 | filterDescriptor.setValue(this.value);
39 |
40 | if (ArrayUtils.isEmpty(subConditionClauseBuilders)) {
41 | return filterDescriptor;
42 | }
43 |
44 | FilterGroupDescriptor filterGroupDescriptor = new FilterGroupDescriptor();
45 | filterGroupDescriptor.setCondition(this.condition);
46 | filterGroupDescriptor.addFilters(filterDescriptor);
47 |
48 | for (ConditionClauseBuilder subClause : subConditionClauseBuilders) {
49 | filterGroupDescriptor.addFilters(subClause.toFilter());
50 | }
51 | return filterGroupDescriptor;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/IDynamicQueryBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder;
2 |
3 | import com.github.wz2cool.dynamic.DynamicQuery;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public interface IDynamicQueryBuilder {
9 | /**
10 | * build to dynamic Query.
11 | *
12 | * @return dynamic query.
13 | */
14 | DynamicQuery build();
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/OrderByClauseBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder;
2 |
3 | import com.github.wz2cool.dynamic.BaseSortDescriptor;
4 | import com.github.wz2cool.dynamic.DynamicQuery;
5 | import com.github.wz2cool.dynamic.SortDescriptor;
6 | import com.github.wz2cool.dynamic.builder.direction.Ascending;
7 | import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
8 | import com.github.wz2cool.dynamic.helper.CommonsHelper;
9 | import com.github.wz2cool.dynamic.lambda.GetCommonPropertyFunction;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | /**
15 | * @author Frank
16 | */
17 | public class OrderByClauseBuilder implements IDynamicQueryBuilder {
18 |
19 | private final List sortDescriptors = new ArrayList<>();
20 | private final DynamicQueryBuilder dynamicQueryBuilder;
21 |
22 | public OrderByClauseBuilder(
23 | DynamicQueryBuilder dynamicQueryBuilder,
24 | GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
25 | this.dynamicQueryBuilder = dynamicQueryBuilder;
26 | dynamicQueryBuilder.setOrderByClauseBuilder(this);
27 | addSort(getPropertyFunction, sortDirection);
28 | }
29 |
30 | public OrderByClauseBuilder(
31 | DynamicQueryBuilder dynamicQueryBuilder,
32 | GetCommonPropertyFunction getPropertyFunction) {
33 | this.dynamicQueryBuilder = dynamicQueryBuilder;
34 | dynamicQueryBuilder.setOrderByClauseBuilder(this);
35 | addSort(getPropertyFunction, new Ascending());
36 | }
37 |
38 | public OrderByClauseBuilder thenBy(
39 | GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
40 | addSort(getPropertyFunction, sortDirection);
41 | return this;
42 | }
43 |
44 | public OrderByClauseBuilder thenBy(
45 | GetCommonPropertyFunction getPropertyFunction) {
46 | addSort(getPropertyFunction, new Ascending());
47 | return this;
48 | }
49 |
50 | @Override
51 | public DynamicQuery build() {
52 | return dynamicQueryBuilder.build();
53 | }
54 |
55 | BaseSortDescriptor[] getSorts() {
56 | return this.sortDescriptors.toArray(new BaseSortDescriptor[0]);
57 | }
58 |
59 | private void addSort(GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
60 | String propertyName = CommonsHelper.getPropertyName(getPropertyFunction);
61 | SortDescriptor sortDescriptor = new SortDescriptor();
62 | sortDescriptor.setPropertyName(propertyName);
63 | sortDescriptor.setDirection(sortDirection.getDirection());
64 | this.sortDescriptors.add(sortDescriptor);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/WhereClauseBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder;
2 |
3 | import com.github.wz2cool.dynamic.BaseFilterDescriptor;
4 | import com.github.wz2cool.dynamic.DynamicQuery;
5 | import com.github.wz2cool.dynamic.FilterCondition;
6 | import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
7 | import com.github.wz2cool.dynamic.builder.opeartor.IFilterOperator;
8 | import com.github.wz2cool.dynamic.lambda.GetCommonPropertyFunction;
9 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
10 |
11 | /**
12 | * @author Frank
13 | */
14 | public class WhereClauseBuilder
15 | extends BaseConditionClauseBuilder, T>
16 | implements IDynamicQueryBuilder {
17 |
18 | private final DynamicQueryBuilder dynamicQueryBuilder;
19 |
20 | public WhereClauseBuilder(
21 | DynamicQueryBuilder dynamicQueryBuilder,
22 | GetPropertyFunction getPropertyFunction,
23 | IFilterOperator operator,
24 | ConditionClauseBuilder[] conditionClauseBuilders) {
25 | this.dynamicQueryBuilder = dynamicQueryBuilder;
26 | dynamicQueryBuilder.setWhereClauseBuilder(this);
27 | andOrInternal(FilterCondition.AND, getPropertyFunction, operator, conditionClauseBuilders);
28 | }
29 |
30 | public OrderByClauseBuilder orderBy(GetCommonPropertyFunction getPropertyFunction) {
31 | return new OrderByClauseBuilder<>(dynamicQueryBuilder, getPropertyFunction);
32 | }
33 |
34 | public OrderByClauseBuilder orderBy(GetCommonPropertyFunction getPropertyFunction, ISortDirection direction) {
35 | return new OrderByClauseBuilder<>(dynamicQueryBuilder, getPropertyFunction, direction);
36 | }
37 |
38 | @Override
39 | public DynamicQuery build() {
40 | return dynamicQueryBuilder.build();
41 | }
42 |
43 | BaseFilterDescriptor[] getFilters() {
44 | return this.conditionClauseBuilders.stream().map(ConditionClauseBuilder::toFilter)
45 | .toArray(BaseFilterDescriptor[]::new);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/direction/Ascending.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.direction;
2 |
3 | import com.github.wz2cool.dynamic.SortDirection;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class Ascending implements ISortDirection {
9 | @Override
10 | public SortDirection getDirection() {
11 | return SortDirection.ASC;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/direction/Descending.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.direction;
2 |
3 | import com.github.wz2cool.dynamic.SortDirection;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class Descending implements ISortDirection {
9 | @Override
10 | public SortDirection getDirection() {
11 | return SortDirection.DESC;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/direction/ISortDirection.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.direction;
2 |
3 | import com.github.wz2cool.dynamic.SortDirection;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public interface ISortDirection {
9 | /**
10 | * Get sort direction.
11 | *
12 | * @return SortDirection enum.
13 | */
14 | SortDirection getDirection();
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/Between.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * @author Frank
10 | */
11 | public class Between implements ITwoValueFilterOperator {
12 |
13 | private final List value;
14 |
15 | public Between(T value1, T value2) {
16 | List values = new ArrayList<>();
17 | values.add(value1);
18 | values.add(value2);
19 | this.value = values;
20 | }
21 |
22 | @Override
23 | public List getValue() {
24 | return value;
25 | }
26 |
27 | @Override
28 | public FilterOperator getOperator() {
29 | return FilterOperator.BETWEEN;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/Contains.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class Contains implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public Contains(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public T getValue() {
18 | return this.value;
19 | }
20 |
21 | @Override
22 | public FilterOperator getOperator() {
23 | return FilterOperator.CONTAINS;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/EndWith.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class EndWith implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public EndWith(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public T getValue() {
18 | return this.value;
19 | }
20 |
21 | @Override
22 | public FilterOperator getOperator() {
23 | return FilterOperator.END_WITH;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/Equal.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class Equal implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public Equal(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public T getValue() {
18 | return this.value;
19 | }
20 |
21 | @Override
22 | public FilterOperator getOperator() {
23 | return FilterOperator.EQUAL;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/GreaterThan.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class GreaterThan implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public GreaterThan(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public FilterOperator getOperator() {
18 | return FilterOperator.GREATER_THAN;
19 | }
20 |
21 | @Override
22 | public T getValue() {
23 | return this.value;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/GreaterThanOrEqual.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class GreaterThanOrEqual implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public GreaterThanOrEqual(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public FilterOperator getOperator() {
18 | return FilterOperator.GREATER_THAN_OR_EQUAL;
19 | }
20 |
21 | @Override
22 | public T getValue() {
23 | return this.value;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/IFilterOperator.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | @SuppressWarnings("squid:S2326")
9 | public interface IFilterOperator {
10 | /**
11 | * Get and operator.
12 | *
13 | * @return filterOperator enum.
14 | */
15 | FilterOperator getOperator();
16 |
17 | /**
18 | * Get and value.
19 | *
20 | * @return and value.
21 | */
22 | Object getValue();
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/IMultipleValueFilterOperator.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public interface IMultipleValueFilterOperator extends IFilterOperator {
9 | /**
10 | * Get and value.
11 | *
12 | * @return and value.
13 | */
14 | @Override
15 | List getValue();
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/ISingleValueFilterOperator.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | /**
4 | * @author Frank
5 | */
6 | public interface ISingleValueFilterOperator extends IFilterOperator {
7 | /**
8 | * Get and value.
9 | *
10 | * @return and value.
11 | */
12 | @Override
13 | T getValue();
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/ITwoValueFilterOperator.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public interface ITwoValueFilterOperator extends IFilterOperator {
9 | /**
10 | * Get and value.
11 | *
12 | * @return and value.
13 | */
14 | @Override
15 | List getValue();
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/In.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | import java.util.*;
6 |
7 | /**
8 | * @author Frank
9 | */
10 | public class In implements IMultipleValueFilterOperator {
11 |
12 | private List value;
13 |
14 | public In(T[] values) {
15 | if (Objects.isNull(values)) {
16 | this.value = new ArrayList<>();
17 | } else {
18 | this.value = Arrays.asList(values);
19 | }
20 | }
21 |
22 | public In(Collection values) {
23 | if (Objects.isNull(values)) {
24 | this.value = new ArrayList<>();
25 | } else {
26 | this.value = new ArrayList<>(values);
27 | }
28 | }
29 |
30 | @Override
31 | public FilterOperator getOperator() {
32 | return FilterOperator.IN;
33 | }
34 |
35 | @Override
36 | public List getValue() {
37 | return this.value;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/LessThan.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class LessThan implements ISingleValueFilterOperator {
9 | private final T value;
10 |
11 | public LessThan(T value) {
12 | this.value = value;
13 | }
14 |
15 | @Override
16 | public FilterOperator getOperator() {
17 | return FilterOperator.LESS_THAN;
18 | }
19 |
20 | @Override
21 | public T getValue() {
22 | return this.value;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/LessThanOrEqual.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class LessThanOrEqual implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public LessThanOrEqual(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public FilterOperator getOperator() {
18 | return FilterOperator.LESS_THAN_OR_EQUAL;
19 | }
20 |
21 | @Override
22 | public T getValue() {
23 | return this.value;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/NotContains.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class NotContains implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public NotContains(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public T getValue() {
18 | return this.value;
19 | }
20 |
21 | @Override
22 | public FilterOperator getOperator() {
23 | return FilterOperator.NOT_CONTAINS;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/NotEqual.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class NotEqual implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public NotEqual(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public FilterOperator getOperator() {
18 | return FilterOperator.NOT_EQUAL;
19 | }
20 |
21 | @Override
22 | public T getValue() {
23 | return this.value;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/NotIn.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | import java.util.*;
6 |
7 | /**
8 | * @author Frank
9 | */
10 | public class NotIn implements IMultipleValueFilterOperator {
11 |
12 | private List value;
13 |
14 | public NotIn(T[] values) {
15 | if (Objects.isNull(values)) {
16 | this.value = new ArrayList<>();
17 | } else {
18 | this.value = Arrays.asList(values);
19 | }
20 | }
21 |
22 | public NotIn(Collection values) {
23 | if (Objects.isNull(values)) {
24 | this.value = new ArrayList<>();
25 | } else {
26 | this.value = new ArrayList<>(values);
27 | }
28 | }
29 |
30 | @Override
31 | public FilterOperator getOperator() {
32 | return FilterOperator.NOT_IN;
33 | }
34 |
35 | @Override
36 | public List getValue() {
37 | return this.value;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/builder/opeartor/StartWith.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.builder.opeartor;
2 |
3 | import com.github.wz2cool.dynamic.FilterOperator;
4 |
5 | /**
6 | * @author Frank
7 | */
8 | public class StartWith implements ISingleValueFilterOperator {
9 |
10 | private final T value;
11 |
12 | public StartWith(T value) {
13 | this.value = value;
14 | }
15 |
16 | @Override
17 | public FilterOperator getOperator() {
18 | return FilterOperator.START_WITH;
19 | }
20 |
21 | @Override
22 | public T getValue() {
23 | return this.value;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/exception/InternalRuntimeException.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.exception;
2 |
3 | /**
4 | * @author Frank
5 | */
6 | public class InternalRuntimeException extends RuntimeException {
7 | public InternalRuntimeException(Throwable cause) {
8 | super(cause);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/exception/PropertyNotFoundException.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.exception;
2 |
3 | /**
4 | * @author Frank
5 | */
6 | public class PropertyNotFoundException extends Exception {
7 | public PropertyNotFoundException(String message) {
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/exception/PropertyNotFoundInternalException.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.exception;
2 |
3 | /**
4 | * @author Frank
5 | */
6 | public class PropertyNotFoundInternalException extends RuntimeException {
7 | public PropertyNotFoundInternalException(String message) {
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/github/wz2cool/dynamic/helper/CommonsHelper.java:
--------------------------------------------------------------------------------
1 | package com.github.wz2cool.dynamic.helper;
2 |
3 | import com.github.wz2cool.dynamic.exception.InternalRuntimeException;
4 | import com.github.wz2cool.dynamic.lambda.GetPropertyFunction;
5 | import com.github.wz2cool.dynamic.model.PropertyInfo;
6 |
7 | import java.lang.invoke.SerializedLambda;
8 | import java.lang.reflect.Array;
9 | import java.lang.reflect.Method;
10 | import java.util.ArrayList;
11 | import java.util.Collection;
12 | import java.util.concurrent.ConcurrentHashMap;
13 |
14 |
15 | /**
16 | * @author Frank
17 | */
18 | public class CommonsHelper {
19 | private static ConcurrentHashMap classMap = new ConcurrentHashMap<>();
20 |
21 | private CommonsHelper() {
22 | throw new UnsupportedOperationException();
23 | }
24 |
25 | public static boolean isNumeric(final Class targetClass) {
26 | if (targetClass == null) {
27 | return false;
28 | }
29 |
30 | return Number.class.isAssignableFrom(targetClass);
31 | }
32 |
33 | public static boolean isArrayOrCollection(final Object value) {
34 | return isArray(value) || isCollection(value);
35 | }
36 |
37 | public static boolean isArray(final Object value) {
38 | return value != null && value.getClass().isArray();
39 | }
40 |
41 | public static boolean isCollection(final Object value) {
42 | return value instanceof Iterable;
43 | }
44 |
45 | public static Object[] getCollectionValues(final Object inputValue) {
46 | if (inputValue == null) {
47 | throw new NullPointerException("inputValue");
48 | }
49 |
50 | if (!isArrayOrCollection(inputValue)) {
51 | throw new IllegalArgumentException("inputValue should be array or collection");
52 | }
53 |
54 | Collection