Skip to content

Commit 88435a0

Browse files
committed
use To[Many|One]Getters in RelationInfo.java
1 parent 4fec87f commit 88435a0

File tree

7 files changed

+56
-22
lines changed

7 files changed

+56
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package io.objectbox.relation;
1+
package io.objectbox.internal;
22

33
import java.io.Serializable;
44

55
import io.objectbox.annotation.apihint.Internal;
6+
import io.objectbox.relation.ToMany;
67

78
@Internal
89
public interface ToManyGetter<SOURCE> extends Serializable {
9-
<TARGET> ToMany<TARGET> getToOne(SOURCE object);
10+
<TARGET> ToMany<TARGET> getToMany(SOURCE object);
1011
}

objectbox-java/src/main/java/io/objectbox/relation/ToOneGetter.java renamed to objectbox-java/src/main/java/io/objectbox/internal/ToOneGetter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package io.objectbox.relation;
1+
package io.objectbox.internal;
22

33
import java.io.Serializable;
44

55
import io.objectbox.annotation.apihint.Internal;
6+
import io.objectbox.relation.ToOne;
67

78
@Internal
89
public interface ToOneGetter<SOURCE> extends Serializable {

objectbox-java/src/main/java/io/objectbox/relation/RelationInfo.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.objectbox.EntityInfo;
88
import io.objectbox.Property;
99
import io.objectbox.annotation.apihint.Internal;
10+
import io.objectbox.internal.ToOneGetter;
1011

1112
@Internal
1213
@Immutable
@@ -22,36 +23,55 @@ public class RelationInfo<TARGET> implements Serializable {
2223
/** For relations based on a target ID property (null for stand-alone relations). */
2324
public final Property targetIdProperty;
2425

26+
/** Only set for ToOne relations */
27+
public final io.objectbox.internal.ToOneGetter toOneGetter;
28+
29+
private final io.objectbox.internal.ToManyGetter toManyGetter;
2530
/** For ToMany relations based on backlinks (null otherwise). */
26-
public final ToOneGetter backlinkToOneGetter;
31+
public final io.objectbox.internal.ToOneGetter backlinkToOneGetter;
2732

2833
/** For stand-alone to-many relations (0 otherwise). */
2934
public final int relationId;
3035

3136
/**
3237
* ToOne
3338
*/
34-
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, Property targetIdProperty) {
35-
this(sourceInfo, targetInfo, targetIdProperty, null);
39+
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, Property targetIdProperty,
40+
io.objectbox.internal.ToOneGetter toOneGetter) {
41+
this.sourceInfo = sourceInfo;
42+
this.targetInfo = targetInfo;
43+
this.targetIdProperty = targetIdProperty;
44+
this.toOneGetter = toOneGetter;
45+
this.backlinkToOneGetter = null;
46+
this.toManyGetter = null;
47+
this.relationId = 0;
3648
}
3749

50+
/**
51+
* ToMany as a ToOne backlink
52+
*/
3853
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, Property targetIdProperty,
39-
ToOneGetter backlinkToOneGetter) {
54+
io.objectbox.internal.ToManyGetter toManyGetter, ToOneGetter backlinkToOneGetter) {
4055
this.sourceInfo = sourceInfo;
4156
this.targetInfo = targetInfo;
4257
this.targetIdProperty = targetIdProperty;
58+
this.toManyGetter = toManyGetter;
4359
this.backlinkToOneGetter = backlinkToOneGetter;
44-
relationId = 0;
60+
this.toOneGetter = null;
61+
this.relationId = 0;
4562
}
4663

4764
/**
48-
* Stand-alone to-many.
65+
* Stand-alone ToMany.
4966
*/
50-
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, int relationId) {
67+
public RelationInfo(EntityInfo sourceInfo, EntityInfo<TARGET> targetInfo, int relationId,
68+
io.objectbox.internal.ToManyGetter toManyGetter) {
5169
this.sourceInfo = sourceInfo;
5270
this.targetInfo = targetInfo;
5371
this.relationId = relationId;
72+
this.toManyGetter = toManyGetter;
5473
this.targetIdProperty = null;
74+
this.toOneGetter = null;
5575
this.backlinkToOneGetter = null;
5676
}
5777
}

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
import io.objectbox.annotation.apihint.Experimental;
3535
import io.objectbox.annotation.apihint.Internal;
3636
import io.objectbox.exception.DbDetachedException;
37-
import io.objectbox.internal.IdGetter;
38-
import io.objectbox.internal.ReflectionCache;
37+
import io.objectbox.internal.*;
3938
import io.objectbox.relation.ListFactory.CopyOnWriteArrayListFactory;
4039

4140
/**
@@ -479,7 +478,7 @@ public boolean internalCheckApplyToDbRequired() {
479478
if ((setAdded == null || setAdded.isEmpty()) && (setRemoved == null || setRemoved.isEmpty())) {
480479
return false;
481480
}
482-
ToOneGetter backlinkToOneGetter = relationInfo.backlinkToOneGetter;
481+
io.objectbox.internal.ToOneGetter backlinkToOneGetter = relationInfo.backlinkToOneGetter;
483482
long entityId = relationInfo.sourceInfo.getIdGetter().getId(entity);
484483
if (entityId == 0) {
485484
throw new IllegalStateException("Source entity has no ID (should have been put before)");

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11

22
package io.objectbox.relation;
33

4-
import javax.annotation.Nullable;
5-
6-
import io.objectbox.BoxStore;
7-
import io.objectbox.Cursor;
84
import io.objectbox.EntityInfo;
95
import io.objectbox.Property;
10-
import io.objectbox.Transaction;
116
import io.objectbox.annotation.apihint.Internal;
127
import io.objectbox.internal.CursorFactory;
138
import io.objectbox.internal.IdGetter;
9+
import io.objectbox.internal.ToManyGetter;
10+
import io.objectbox.internal.ToOneGetter;
1411
import io.objectbox.relation.CustomerCursor.Factory;
1512

1613
// THIS CODE IS ADAPTED from generated resources of the test-entity-annotations project
@@ -93,14 +90,24 @@ public long getId(Customer object) {
9390
}
9491

9592
static final RelationInfo<Order> orders =
96-
new RelationInfo<>(Customer_.__INSTANCE, Order_.__INSTANCE, Order_.customerId, new ToOneGetter<Order>() {
93+
new RelationInfo<>(Customer_.__INSTANCE, Order_.__INSTANCE, Order_.customerId, new ToManyGetter<Customer>() {
94+
@Override
95+
public ToMany<Order> getToMany(Customer customer) {
96+
return (ToMany<Order>) customer.getOrders();
97+
}
98+
}, new ToOneGetter<Order>() {
9799
@Override
98100
public ToOne<Customer> getToOne(Order order) {
99101
return order.customer__toOne;
100102
}
101103
});
102104

103105
static final RelationInfo<Order> ordersStandalone =
104-
new RelationInfo<>(Customer_.__INSTANCE, Order_.__INSTANCE, 1);
106+
new RelationInfo<>(Customer_.__INSTANCE, Order_.__INSTANCE, 1, new ToManyGetter<Customer>() {
107+
@Override
108+
public ToMany<Order> getToMany(Customer customer) {
109+
return (ToMany<Order>) customer.getOrders();
110+
}
111+
});
105112

106113
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.objectbox.annotation.apihint.Internal;
1212
import io.objectbox.internal.CursorFactory;
1313
import io.objectbox.internal.IdGetter;
14+
import io.objectbox.internal.ToOneGetter;
1415
import io.objectbox.relation.OrderCursor.Factory;
1516

1617
// THIS CODE IS ADAPTED from generated resources of the test-entity-annotations project
@@ -97,6 +98,11 @@ public long getId(Order object) {
9798
}
9899
}
99100

100-
static final RelationInfo<Customer> customer = new RelationInfo<>(Order_.__INSTANCE, Customer_.__INSTANCE, customerId);
101+
static final RelationInfo<Customer> customer = new RelationInfo<>(Order_.__INSTANCE, Customer_.__INSTANCE, customerId, new ToOneGetter<Order>() {
102+
@Override
103+
public ToOne getToOne(Order object) {
104+
return object.customer__toOne;
105+
}
106+
});
101107

102108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void testTargetId_withTargetIdProperty() {
3030
}
3131

3232
private RelationInfo<Customer> getRelationInfo(Property targetIdProperty) {
33-
return new RelationInfo<>(new Order_(), new Customer_(), targetIdProperty);
33+
return new RelationInfo<>(new Order_(), new Customer_(), targetIdProperty, null);
3434
}
3535

3636
@Test

0 commit comments

Comments
 (0)