Skip to content

Commit 699efcc

Browse files
committed
Cursor: extracted checkApplyToManyToDb for sub classes
1 parent 1b33d2f commit 699efcc

2 files changed

Lines changed: 17 additions & 24 deletions

File tree

objectbox-java/src/main/java/io/objectbox/Cursor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.objectbox.annotation.apihint.Beta;
2626
import io.objectbox.annotation.apihint.Internal;
2727
import io.objectbox.annotation.apihint.Temporary;
28+
import io.objectbox.relation.ToMany;
2829

2930
@SuppressWarnings({"unchecked", "SameParameterValue", "unused"})
3031
@Beta
@@ -309,6 +310,20 @@ public void modifyRelationsSingle(int relationId, long key, long targetKey, bool
309310
nativeModifyRelationsSingle(cursor, relationId, key, targetKey, remove);
310311
}
311312

313+
protected <TARGET> void checkApplyToManyToDb(List<TARGET> orders, Class<TARGET> targetClass) {
314+
if (orders instanceof ToMany) {
315+
ToMany<TARGET> toMany = (ToMany<TARGET>) orders;
316+
if (toMany.internalCheckApplyToDbRequired()) {
317+
Cursor<TARGET> targetCursor = getRelationTargetCursor(targetClass);
318+
try {
319+
toMany.internalApplyToDb(this, targetCursor);
320+
} finally {
321+
targetCursor.close();
322+
}
323+
}
324+
}
325+
}
326+
312327
@Override
313328
public String toString() {
314329
return "Cursor " + Long.toString(cursor, 16) + (isClosed() ? "(closed)" : "");

tests/objectbox-java-test/src/main/java/io/objectbox/relation/CustomerCursor.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,8 @@ public final long put(Customer entity) {
7474
entity.setId(__assignedId);
7575
entity.__boxStore = boxStoreForEntities;
7676

77-
if (entity.orders instanceof ToMany) {
78-
ToMany<Order> toMany = (ToMany<Order>) entity.orders;
79-
if (toMany.internalCheckApplyToDbRequired()) {
80-
Cursor<Order> targetCursor = getRelationTargetCursor(Order.class);
81-
try {
82-
toMany.internalApplyToDb(this, targetCursor);
83-
} finally {
84-
targetCursor.close();
85-
}
86-
}
87-
}
88-
89-
List<Order> ordersStandalone = entity.getOrdersStandalone();
90-
if (ordersStandalone instanceof ToMany) {
91-
ToMany<Order> toMany = (ToMany<Order>) ordersStandalone;
92-
if (toMany.internalCheckApplyToDbRequired()) {
93-
Cursor<Order> targetCursor = getRelationTargetCursor(Order.class);
94-
try {
95-
toMany.internalApplyToDb(this, targetCursor);
96-
} finally {
97-
targetCursor.close();
98-
}
99-
}
100-
}
77+
checkApplyToManyToDb(entity.orders, Order.class);
78+
checkApplyToManyToDb(entity.getOrdersStandalone(), Order.class);
10179

10280
return __assignedId;
10381
}

0 commit comments

Comments
 (0)