Skip to content

Commit eb31c35

Browse files
committed
Rename the method from "associateAndReplace" to "associateWith" and simplify the method.
1 parent 43a9eb1 commit eb31c35

File tree

8 files changed

+71
-97
lines changed

8 files changed

+71
-97
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/command/AssociateCommand.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.seasar.doma.internal.util.Pair;
1111
import org.seasar.doma.jdbc.command.Command;
1212
import org.seasar.doma.jdbc.command.SelectCommand;
13-
import org.seasar.doma.jdbc.criteria.context.AssociationPair;
1413
import org.seasar.doma.jdbc.criteria.context.SelectContext;
1514
import org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel;
1615
import org.seasar.doma.jdbc.entity.EntityType;
@@ -68,26 +67,19 @@ public List<ENTITY> execute() {
6867
private void associate(
6968
Map<EntityKey, Object> cache,
7069
Map<EntityMetamodel<?>, Pair<EntityKey, Object>> associationCandidate) {
71-
for (Map.Entry<
72-
Pair<EntityMetamodel<?>, EntityMetamodel<?>>,
73-
BiFunction<Object, Object, AssociationPair<?, ?>>>
70+
for (Map.Entry<Pair<EntityMetamodel<?>, EntityMetamodel<?>>, BiFunction<Object, Object, Object>>
7471
e : context.associations.entrySet()) {
7572
Pair<EntityMetamodel<?>, EntityMetamodel<?>> metamodelPair = e.getKey();
76-
BiFunction<Object, Object, AssociationPair<?, ?>> associator = e.getValue();
73+
BiFunction<Object, Object, Object> associator = e.getValue();
7774
Pair<EntityKey, Object> keyAndEntity1 = associationCandidate.get(metamodelPair.fst);
7875
Pair<EntityKey, Object> keyAndEntity2 = associationCandidate.get(metamodelPair.snd);
7976
if (keyAndEntity1 == null || keyAndEntity2 == null) {
8077
continue;
8178
}
82-
AssociationPair<?, ?> associationPair =
83-
associator.apply(keyAndEntity1.snd, keyAndEntity2.snd);
84-
if (associationPair != null) {
85-
cache.replace(keyAndEntity1.fst, associationPair.getFirst());
86-
cache.replace(keyAndEntity2.fst, associationPair.getSecond());
87-
associationCandidate.replace(
88-
metamodelPair.fst, new Pair<>(keyAndEntity1.fst, associationPair.getFirst()));
89-
associationCandidate.replace(
90-
metamodelPair.snd, new Pair<>(keyAndEntity2.fst, associationPair.getSecond()));
79+
Object newEntity = associator.apply(keyAndEntity1.snd, keyAndEntity2.snd);
80+
if (newEntity != null) {
81+
cache.replace(keyAndEntity1.fst, newEntity);
82+
associationCandidate.replace(metamodelPair.fst, new Pair<>(keyAndEntity1.fst, newEntity));
9183
}
9284
}
9385
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/context/AssociationPair.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/context/SelectContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public class SelectContext implements Context {
2828
public Integer limit;
2929
public Integer offset;
3030
public ForUpdate forUpdate = new ForUpdate(ForUpdateOption.none());
31-
public final Map<
32-
Pair<EntityMetamodel<?>, EntityMetamodel<?>>,
33-
BiFunction<Object, Object, AssociationPair<?, ?>>>
31+
public final Map<Pair<EntityMetamodel<?>, EntityMetamodel<?>>, BiFunction<Object, Object, Object>>
3432
associations = new LinkedHashMap<>();
3533
public final SelectSettings settings = new SelectSettings();
3634

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/declaration/SelectFromDeclaration.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.function.Consumer;
1212
import org.seasar.doma.DomaException;
1313
import org.seasar.doma.internal.util.Pair;
14-
import org.seasar.doma.jdbc.criteria.context.AssociationPair;
1514
import org.seasar.doma.jdbc.criteria.context.ForUpdate;
1615
import org.seasar.doma.jdbc.criteria.context.Join;
1716
import org.seasar.doma.jdbc.criteria.context.JoinKind;
@@ -144,6 +143,7 @@ public void selectTo(
144143
new Projection.EntityMetamodels(entityMetamodel, new ArrayList<>(projectionTargets));
145144
}
146145

146+
@SuppressWarnings("unchecked")
147147
public <ENTITY1, ENTITY2> void associate(
148148
EntityMetamodel<ENTITY1> first,
149149
EntityMetamodel<ENTITY2> second,
@@ -153,35 +153,45 @@ public <ENTITY1, ENTITY2> void associate(
153153
Objects.requireNonNull(second);
154154
Objects.requireNonNull(associator);
155155
Objects.requireNonNull(option);
156-
associateAndReplace(
157-
first,
158-
second,
156+
if (!context.getEntityMetamodels().contains(first)) {
157+
if (option == AssociationOption.Kind.MANDATORY) {
158+
throw new DomaException(Message.DOMA6001, "first");
159+
}
160+
return;
161+
}
162+
if (!context.getEntityMetamodels().contains(second)) {
163+
if (option == AssociationOption.Kind.MANDATORY) {
164+
throw new DomaException(Message.DOMA6001, "second");
165+
}
166+
return;
167+
}
168+
context.associations.put(
169+
new Pair<>(first, second),
159170
(entity1, entity2) -> {
160-
associator.accept(entity1, entity2);
171+
associator.accept((ENTITY1) entity1, (ENTITY2) entity2);
161172
return null;
162-
},
163-
option);
173+
});
164174
}
165175

166176
@SuppressWarnings("unchecked")
167-
public <ENTITY1, ENTITY2> void associateAndReplace(
177+
public <ENTITY1, ENTITY2> void associateWith(
168178
EntityMetamodel<ENTITY1> first,
169179
EntityMetamodel<ENTITY2> second,
170-
BiFunction<ENTITY1, ENTITY2, AssociationPair<ENTITY1, ENTITY2>> associator,
180+
BiFunction<ENTITY1, ENTITY2, ENTITY1> associator,
171181
AssociationOption option) {
172182
Objects.requireNonNull(first);
173183
Objects.requireNonNull(second);
174184
Objects.requireNonNull(associator);
175185
Objects.requireNonNull(option);
176186
if (!context.getEntityMetamodels().contains(first)) {
177187
if (option == AssociationOption.Kind.MANDATORY) {
178-
throw new DomaException(Message.DOMA6001, "first");
188+
throw new DomaException(Message.DOMA6010, "first");
179189
}
180190
return;
181191
}
182192
if (!context.getEntityMetamodels().contains(second)) {
183193
if (option == AssociationOption.Kind.MANDATORY) {
184-
throw new DomaException(Message.DOMA6001, "second");
194+
throw new DomaException(Message.DOMA6010, "second");
185195
}
186196
return;
187197
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/EntityqlSelectStarting.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.function.Consumer;
99
import org.seasar.doma.jdbc.Config;
1010
import org.seasar.doma.jdbc.command.Command;
11-
import org.seasar.doma.jdbc.criteria.context.AssociationPair;
1211
import org.seasar.doma.jdbc.criteria.declaration.JoinDeclaration;
1312
import org.seasar.doma.jdbc.criteria.declaration.OrderByNameDeclaration;
1413
import org.seasar.doma.jdbc.criteria.declaration.SelectFromDeclaration;
@@ -84,27 +83,27 @@ public <ENTITY1, ENTITY2> EntityqlSelectStarting<ENTITY> associate(
8483
return this;
8584
}
8685

87-
public <ENTITY1, ENTITY2> EntityqlSelectStarting<ENTITY> associateAndReplace(
86+
public <ENTITY1, ENTITY2> EntityqlSelectStarting<ENTITY> associateWith(
8887
EntityMetamodel<ENTITY1> first,
8988
EntityMetamodel<ENTITY2> second,
90-
BiFunction<ENTITY1, ENTITY2, AssociationPair<ENTITY1, ENTITY2>> associator) {
89+
BiFunction<ENTITY1, ENTITY2, ENTITY1> associator) {
9190
Objects.requireNonNull(first);
9291
Objects.requireNonNull(second);
9392
Objects.requireNonNull(associator);
94-
declaration.associateAndReplace(first, second, associator, AssociationOption.mandatory());
93+
declaration.associateWith(first, second, associator, AssociationOption.mandatory());
9594
return this;
9695
}
9796

98-
public <ENTITY1, ENTITY2> EntityqlSelectStarting<ENTITY> associateAndReplace(
97+
public <ENTITY1, ENTITY2> EntityqlSelectStarting<ENTITY> associateWith(
9998
EntityMetamodel<ENTITY1> first,
10099
EntityMetamodel<ENTITY2> second,
101-
BiFunction<ENTITY1, ENTITY2, AssociationPair<ENTITY1, ENTITY2>> associator,
100+
BiFunction<ENTITY1, ENTITY2, ENTITY1> associator,
102101
AssociationOption option) {
103102
Objects.requireNonNull(first);
104103
Objects.requireNonNull(second);
105104
Objects.requireNonNull(associator);
106105
Objects.requireNonNull(option);
107-
declaration.associateAndReplace(first, second, associator, option);
106+
declaration.associateWith(first, second, associator, option);
108107
return this;
109108
}
110109

doma-core/src/main/java/org/seasar/doma/message/Message.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,10 @@ public enum Message implements MessageResource {
949949
DOMA6009(
950950
"The parameter \"{0}\" is unknown. "
951951
+ "Ensure that you have passed it to the from, the innerJoin, or the leftJoin method before invoking the select method."),
952+
DOMA6010(
953+
"The parameter \"{0}\" is unknown. "
954+
+ "Ensure that you have passed it to the from, the innerJoin, or the leftJoin method before invoking the associateWith method. "
955+
+ "If the innerJoin or leftJoin method call is optional, pass the AssociationKind.OPTIONAL value to the associateWith method."),
952956
;
953957

954958
private final String messagePattern;

test-criteria/src/main/java/example/Emp.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ public Emp(
7373
this.manager = manager;
7474
}
7575

76+
public Emp withDept(Dept dept) {
77+
return new Emp(
78+
this.getEmployeeId(),
79+
this.getEmployeeNo(),
80+
this.getEmployeeName(),
81+
this.getManagerId(),
82+
this.getHiredate(),
83+
this.getSalary(),
84+
this.getDepartmentId(),
85+
this.getAddressId(),
86+
this.getVersion(),
87+
dept,
88+
this.getManager());
89+
}
90+
91+
public Emp withManager(Emp manager) {
92+
return new Emp(
93+
this.getEmployeeId(),
94+
this.getEmployeeNo(),
95+
this.getEmployeeName(),
96+
this.getManagerId(),
97+
this.getHiredate(),
98+
this.getSalary(),
99+
this.getDepartmentId(),
100+
this.getAddressId(),
101+
this.getVersion(),
102+
this.getDepartment(),
103+
manager);
104+
}
105+
76106
public Integer getEmployeeId() {
77107
return employeeId;
78108
}

test-criteria/src/test/java/example/ImmutableTest.java

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.junit.jupiter.api.extension.ExtendWith;
1010
import org.seasar.doma.jdbc.Config;
1111
import org.seasar.doma.jdbc.criteria.Entityql;
12-
import org.seasar.doma.jdbc.criteria.context.AssociationPair;
1312

1413
@ExtendWith(Env.class)
1514
public class ImmutableTest {
@@ -29,7 +28,7 @@ void fetch() {
2928
}
3029

3130
@Test
32-
void associateAndReplace() {
31+
void associateWith() {
3332
Emp_ e = new Emp_();
3433
Emp_ m = new Emp_();
3534
Dept_ d = new Dept_();
@@ -40,44 +39,8 @@ void associateAndReplace() {
4039
.innerJoin(d, on -> on.eq(e.departmentId, d.departmentId))
4140
.leftJoin(m, on -> on.eq(e.managerId, m.employeeId))
4241
.where(c -> c.eq(d.departmentName, "SALES"))
43-
.associateAndReplace(
44-
e,
45-
d,
46-
(emp, dept) -> {
47-
Emp newEmp =
48-
new Emp(
49-
emp.getEmployeeId(),
50-
emp.getEmployeeNo(),
51-
emp.getEmployeeName(),
52-
emp.getManagerId(),
53-
emp.getHiredate(),
54-
emp.getSalary(),
55-
emp.getDepartmentId(),
56-
emp.getAddressId(),
57-
emp.getVersion(),
58-
dept,
59-
emp.getManager());
60-
return new AssociationPair<>(newEmp, dept);
61-
})
62-
.associateAndReplace(
63-
e,
64-
m,
65-
(emp, manager) -> {
66-
Emp newEmp =
67-
new Emp(
68-
emp.getEmployeeId(),
69-
emp.getEmployeeNo(),
70-
emp.getEmployeeName(),
71-
emp.getManagerId(),
72-
emp.getHiredate(),
73-
emp.getSalary(),
74-
emp.getDepartmentId(),
75-
emp.getAddressId(),
76-
emp.getVersion(),
77-
emp.getDepartment(),
78-
manager);
79-
return new AssociationPair<>(newEmp, manager);
80-
})
42+
.associateWith(e, d, Emp::withDept)
43+
.associateWith(e, m, Emp::withManager)
8144
.fetch();
8245

8346
assertEquals(6, list.size());

0 commit comments

Comments
 (0)