Skip to content

Commit 08cbae3

Browse files
committed
Remove duplicate code through abstraction
1 parent a79770d commit 08cbae3

File tree

4 files changed

+77
-69
lines changed

4 files changed

+77
-69
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select;
17+
18+
import java.util.Objects;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.common.OrderByModel;
22+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
23+
24+
public abstract class AbstractSelectModel {
25+
private final OrderByModel orderByModel;
26+
private final PagingModel pagingModel;
27+
protected final StatementConfiguration statementConfiguration;
28+
29+
protected AbstractSelectModel(AbstractBuilder<?> builder) {
30+
orderByModel = builder.orderByModel;
31+
pagingModel = builder.pagingModel;
32+
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
33+
}
34+
35+
public Optional<OrderByModel> orderByModel() {
36+
return Optional.ofNullable(orderByModel);
37+
}
38+
39+
public Optional<PagingModel> pagingModel() {
40+
return Optional.ofNullable(pagingModel);
41+
}
42+
43+
public abstract static class AbstractBuilder<T extends AbstractBuilder<T>> {
44+
private OrderByModel orderByModel;
45+
private PagingModel pagingModel;
46+
private StatementConfiguration statementConfiguration;
47+
48+
public T withOrderByModel(OrderByModel orderByModel) {
49+
this.orderByModel = orderByModel;
50+
return getThis();
51+
}
52+
53+
public T withPagingModel(PagingModel pagingModel) {
54+
this.pagingModel = pagingModel;
55+
return getThis();
56+
}
57+
58+
public T withStatementConfiguration(StatementConfiguration statementConfiguration) {
59+
this.statementConfiguration = statementConfiguration;
60+
return getThis();
61+
}
62+
63+
protected abstract T getThis();
64+
}
65+
}

src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,22 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Objects;
21-
import java.util.Optional;
2221
import java.util.stream.Stream;
2322

2423
import org.jetbrains.annotations.NotNull;
25-
import org.mybatis.dynamic.sql.common.OrderByModel;
26-
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2724
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2825
import org.mybatis.dynamic.sql.select.render.MultiSelectRenderer;
2926
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
3027
import org.mybatis.dynamic.sql.util.Validator;
3128

32-
public class MultiSelectModel {
29+
public class MultiSelectModel extends AbstractSelectModel {
3330
private final SelectModel initialSelect;
3431
private final List<UnionQuery> unionQueries;
35-
private final OrderByModel orderByModel;
36-
private final PagingModel pagingModel;
37-
private final StatementConfiguration statementConfiguration;
3832

3933
private MultiSelectModel(Builder builder) {
34+
super(builder);
4035
initialSelect = Objects.requireNonNull(builder.initialSelect);
4136
unionQueries = builder.unionQueries;
42-
orderByModel = builder.orderByModel;
43-
pagingModel = builder.pagingModel;
44-
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
4537
Validator.assertNotEmpty(unionQueries, "ERROR.35"); //$NON-NLS-1$
4638
}
4739

@@ -53,14 +45,6 @@ public Stream<UnionQuery> unionQueries() {
5345
return unionQueries.stream();
5446
}
5547

56-
public Optional<OrderByModel> orderByModel() {
57-
return Optional.ofNullable(orderByModel);
58-
}
59-
60-
public Optional<PagingModel> pagingModel() {
61-
return Optional.ofNullable(pagingModel);
62-
}
63-
6448
@NotNull
6549
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
6650
return new MultiSelectRenderer.Builder()
@@ -71,12 +55,9 @@ public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
7155
.render();
7256
}
7357

74-
public static class Builder {
58+
public static class Builder extends AbstractBuilder<Builder> {
7559
private SelectModel initialSelect;
7660
private final List<UnionQuery> unionQueries = new ArrayList<>();
77-
private OrderByModel orderByModel;
78-
private PagingModel pagingModel;
79-
private StatementConfiguration statementConfiguration;
8061

8162
public Builder withInitialSelect(SelectModel initialSelect) {
8263
this.initialSelect = initialSelect;
@@ -88,18 +69,8 @@ public Builder withUnionQueries(List<UnionQuery> unionQueries) {
8869
return this;
8970
}
9071

91-
public Builder withOrderByModel(OrderByModel orderByModel) {
92-
this.orderByModel = orderByModel;
93-
return this;
94-
}
95-
96-
public Builder withPagingModel(PagingModel pagingModel) {
97-
this.pagingModel = pagingModel;
98-
return this;
99-
}
100-
101-
public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
102-
this.statementConfiguration = statementConfiguration;
72+
@Override
73+
protected Builder getThis() {
10374
return this;
10475
}
10576

src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,28 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Objects;
21-
import java.util.Optional;
2221
import java.util.stream.Stream;
2322

2423
import org.jetbrains.annotations.NotNull;
25-
import org.mybatis.dynamic.sql.common.OrderByModel;
26-
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2724
import org.mybatis.dynamic.sql.render.RenderingContext;
2825
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2926
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
3027
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
3128
import org.mybatis.dynamic.sql.util.Validator;
3229

33-
public class SelectModel {
30+
public class SelectModel extends AbstractSelectModel {
3431
private final List<QueryExpressionModel> queryExpressions;
35-
private final OrderByModel orderByModel;
36-
private final PagingModel pagingModel;
37-
private final StatementConfiguration statementConfiguration;
3832

3933
private SelectModel(Builder builder) {
34+
super(builder);
4035
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
4136
Validator.assertNotEmpty(queryExpressions, "ERROR.14"); //$NON-NLS-1$
42-
orderByModel = builder.orderByModel;
43-
pagingModel = builder.pagingModel;
44-
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
4537
}
4638

4739
public Stream<QueryExpressionModel> queryExpressions() {
4840
return queryExpressions.stream();
4941
}
5042

51-
public Optional<OrderByModel> orderByModel() {
52-
return Optional.ofNullable(orderByModel);
53-
}
54-
55-
public Optional<PagingModel> pagingModel() {
56-
return Optional.ofNullable(pagingModel);
57-
}
58-
5943
@NotNull
6044
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
6145
RenderingContext renderingContext = RenderingContext.withRenderingStrategy(renderingStrategy)
@@ -82,11 +66,8 @@ public static Builder withQueryExpressions(List<QueryExpressionModel> queryExpre
8266
return new Builder().withQueryExpressions(queryExpressions);
8367
}
8468

85-
public static class Builder {
69+
public static class Builder extends AbstractBuilder<Builder> {
8670
private final List<QueryExpressionModel> queryExpressions = new ArrayList<>();
87-
private OrderByModel orderByModel;
88-
private PagingModel pagingModel;
89-
private StatementConfiguration statementConfiguration;
9071

9172
public Builder withQueryExpression(QueryExpressionModel queryExpression) {
9273
this.queryExpressions.add(queryExpression);
@@ -98,18 +79,8 @@ public Builder withQueryExpressions(List<QueryExpressionModel> queryExpressions)
9879
return this;
9980
}
10081

101-
public Builder withOrderByModel(OrderByModel orderByModel) {
102-
this.orderByModel = orderByModel;
103-
return this;
104-
}
105-
106-
public Builder withPagingModel(PagingModel pagingModel) {
107-
this.pagingModel = pagingModel;
108-
return this;
109-
}
110-
111-
public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
112-
this.statementConfiguration = statementConfiguration;
82+
@Override
83+
protected Builder getThis() {
11384
return this;
11485
}
11586

src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ void testInvalidNullInsertColumnList() {
162162

163163
@Test
164164
void testInvalidSelectStatementWithoutQueryExpressions() {
165-
SelectModel.Builder builder = new SelectModel.Builder();
165+
SelectModel.Builder builder =
166+
new SelectModel.Builder().withStatementConfiguration(new StatementConfiguration());
166167

167168
assertThatExceptionOfType(InvalidSqlException.class).isThrownBy(builder::build)
168169
.withMessage(Messages.getString("ERROR.14"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy