Skip to content

Commit 9532ded

Browse files
committed
Duplicate code elimination
1 parent 17ffeb7 commit 9532ded

File tree

3 files changed

+97
-67
lines changed

3 files changed

+97
-67
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2016-2022 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.common;
17+
18+
import org.mybatis.dynamic.sql.SqlTable;
19+
import org.mybatis.dynamic.sql.where.WhereModel;
20+
21+
/**
22+
* Builder class shared between the delete and update model builders.
23+
*
24+
* @param <T> type of the implementing builder
25+
*/
26+
public abstract class CommonBuilder<T extends CommonBuilder<T>> {
27+
private SqlTable table;
28+
private String tableAlias;
29+
private WhereModel whereModel;
30+
private Long limit;
31+
private OrderByModel orderByModel;
32+
33+
public SqlTable table() {
34+
return table;
35+
}
36+
37+
public String tableAlias() {
38+
return tableAlias;
39+
}
40+
41+
public WhereModel whereModel() {
42+
return whereModel;
43+
}
44+
45+
public Long limit() {
46+
return limit;
47+
}
48+
49+
public OrderByModel orderByModel() {
50+
return orderByModel;
51+
}
52+
53+
public T withTable(SqlTable table) {
54+
this.table = table;
55+
return getThis();
56+
}
57+
58+
public T withTableAlias(String tableAlias) {
59+
this.tableAlias = tableAlias;
60+
return getThis();
61+
}
62+
63+
public T withWhereModel(WhereModel whereModel) {
64+
this.whereModel = whereModel;
65+
return getThis();
66+
}
67+
68+
public T withLimit(Long limit) {
69+
this.limit = limit;
70+
return getThis();
71+
}
72+
73+
public T withOrderByModel(OrderByModel orderByModel) {
74+
this.orderByModel = orderByModel;
75+
return getThis();
76+
}
77+
78+
protected abstract T getThis();
79+
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.jetbrains.annotations.NotNull;
2222
import org.mybatis.dynamic.sql.SqlTable;
23+
import org.mybatis.dynamic.sql.common.CommonBuilder;
2324
import org.mybatis.dynamic.sql.common.OrderByModel;
2425
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
2526
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
@@ -34,11 +35,11 @@ public class DeleteModel {
3435
private final OrderByModel orderByModel;
3536

3637
private DeleteModel(Builder builder) {
37-
table = Objects.requireNonNull(builder.table);
38-
whereModel = builder.whereModel;
39-
tableAlias = builder.tableAlias;
40-
limit = builder.limit;
41-
orderByModel = builder.orderByModel;
38+
table = Objects.requireNonNull(builder.table());
39+
whereModel = builder.whereModel();
40+
tableAlias = builder.tableAlias();
41+
limit = builder.limit();
42+
orderByModel = builder.orderByModel();
4243
}
4344

4445
public SqlTable table() {
@@ -73,35 +74,9 @@ public static Builder withTable(SqlTable table) {
7374
return new Builder().withTable(table);
7475
}
7576

76-
public static class Builder {
77-
private SqlTable table;
78-
private String tableAlias;
79-
private WhereModel whereModel;
80-
private Long limit;
81-
private OrderByModel orderByModel;
82-
83-
public Builder withTable(SqlTable table) {
84-
this.table = table;
85-
return this;
86-
}
87-
88-
public Builder withTableAlias(String tableAlias) {
89-
this.tableAlias = tableAlias;
90-
return this;
91-
}
92-
93-
public Builder withWhereModel(WhereModel whereModel) {
94-
this.whereModel = whereModel;
95-
return this;
96-
}
97-
98-
public Builder withLimit(Long limit) {
99-
this.limit = limit;
100-
return this;
101-
}
102-
103-
public Builder withOrderByModel(OrderByModel orderByModel) {
104-
this.orderByModel = orderByModel;
77+
public static class Builder extends CommonBuilder<Builder> {
78+
@Override
79+
protected Builder getThis() {
10580
return this;
10681
}
10782

src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.jetbrains.annotations.NotNull;
2626
import org.mybatis.dynamic.sql.SqlTable;
27+
import org.mybatis.dynamic.sql.common.CommonBuilder;
2728
import org.mybatis.dynamic.sql.common.OrderByModel;
2829
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2930
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -42,12 +43,12 @@ public class UpdateModel {
4243
private final OrderByModel orderByModel;
4344

4445
private UpdateModel(Builder builder) {
45-
table = Objects.requireNonNull(builder.table);
46-
whereModel = builder.whereModel;
46+
table = Objects.requireNonNull(builder.table());
47+
whereModel = builder.whereModel();
4748
columnMappings = Objects.requireNonNull(builder.columnMappings);
48-
tableAlias = builder.tableAlias;
49-
limit = builder.limit;
50-
orderByModel = builder.orderByModel;
49+
tableAlias = builder.tableAlias();
50+
limit = builder.limit();
51+
orderByModel = builder.orderByModel();
5152

5253
if (columnMappings.isEmpty()) {
5354
throw new InvalidSqlException(Messages.getString("ERROR.17")); //$NON-NLS-1$
@@ -90,41 +91,16 @@ public static Builder withTable(SqlTable table) {
9091
return new Builder().withTable(table);
9192
}
9293

93-
public static class Builder {
94-
private SqlTable table;
95-
private String tableAlias;
96-
private WhereModel whereModel;
94+
public static class Builder extends CommonBuilder<Builder> {
9795
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
98-
private Long limit;
99-
private OrderByModel orderByModel;
100-
101-
public Builder withTable(SqlTable table) {
102-
this.table = table;
103-
return this;
104-
}
105-
106-
public Builder withTableAlias(String tableAlias) {
107-
this.tableAlias = tableAlias;
108-
return this;
109-
}
11096

11197
public Builder withColumnMappings(List<AbstractColumnMapping> columnMappings) {
11298
this.columnMappings.addAll(columnMappings);
11399
return this;
114100
}
115101

116-
public Builder withWhereModel(WhereModel whereModel) {
117-
this.whereModel = whereModel;
118-
return this;
119-
}
120-
121-
public Builder withLimit(Long limit) {
122-
this.limit = limit;
123-
return this;
124-
}
125-
126-
public Builder withOrderByModel(OrderByModel orderByModel) {
127-
this.orderByModel = orderByModel;
102+
@Override
103+
protected Builder getThis() {
128104
return this;
129105
}
130106

0 commit comments

Comments
 (0)