Skip to content

Commit cf8a07c

Browse files
authored
Fix tests and docs around RealmAny sorting (#7477)
1 parent f135fe3 commit cf8a07c

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## 10.6.0-BETA.2 (2021-05-17)
2+
3+
### Breaking Changes
4+
* None.
5+
6+
### Enhancements
7+
* None.
8+
9+
### Fixed
10+
* Removed wrong `@Nullable` annotation on `RealmQuery.maxRealmAny()`.
11+
12+
### Compatibility
13+
* File format: Generates Realms with format v21. Unsynced Realms will be upgraded from Realm Java 2.0 and later. Synced Realms can only be read and upgraded if created with Realm Java v10.0.0-BETA.1.
14+
* APIs are backwards compatible with all previous release of realm-java in the 10.6.y series.
15+
* Realm Studio 11.0.0-alpha.0 or above is required to open Realms created by this version.
16+
17+
### Internal
18+
* None.
19+
20+
121
## 10.6.0-BETA.1 (2021-05-17)
222

323
### Breaking Changes

realm/realm-library/src/androidTest/kotlin/io/realm/realmany/RealmAnyQueryTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ class RealmAnyQueryTests {
147147
@Test
148148
fun max() {
149149
initializeTestData()
150-
val value = realm.where<RealmAnyNotIndexed>().maxRealmAny(RealmAnyNotIndexed.FIELD_REALM_ANY)
151-
assertEquals(RealmAny.valueOf(UUID.fromString("00000004-aa12-4afa-9219-e20cc3018599")), value)
150+
val value = realm.where<RealmAnyNotIndexed>().maxRealmAny(RealmAnyNotIndexed.FIELD_REALM_ANY)!!
151+
assertEquals("item 2", value.asRealmModel(PrimaryKeyAsString::class.java).name)
152152
}
153153

154154
@Test
@@ -157,7 +157,7 @@ class RealmAnyQueryTests {
157157
val results = realm.where<RealmAnyNotIndexed>().sort(RealmAnyNotIndexed.FIELD_REALM_ANY).findAll()
158158
assertEquals(112, results.size)
159159
assertTrue(results.first()!!.realmAny!!.isNull)
160-
assertEquals(RealmAny.Type.UUID, results.last()!!.realmAny!!.type)
160+
assertEquals(RealmAny.Type.OBJECT, results.last()!!.realmAny!!.type)
161161
}
162162

163163
@Test

realm/realm-library/src/main/java/io/realm/RealmAny.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@
111111
* value wrapped by the RealmAny instance. If the resulting class is
112112
* a realization of {@link io.realm.RealmModel} asRealmModel() can be
113113
* called to cast the RealmAny value to a Realm object reference.
114+
* <p>
115+
* RealmAny values can also be sorted. The sorting order used between
116+
* different RealmAny types, from lowest to highest, is:
117+
* <ol>
118+
* <li>Boolean</li>
119+
* <li>Byte/Short/Integer/Long/Float/Double/Decimal128</li>
120+
* <li>byte[]/String</li>
121+
* <li>Date</li>
122+
* <li>ObjectId</li>
123+
* <li>UUID</li>
124+
* <li>RealmObject</li>
125+
* </ol>
126+
* This has implications on how {@link RealmQuery#sort(String)},
127+
* {@link RealmQuery#minRealmAny(String)} and {@link RealmQuery#maxRealmAny(String)}
128+
* work. Especially {@code min()} and {@code max()} will not only take
129+
* numeric fields into account, but will use the sorting order to determine
130+
* the "largest" or "lowest" value.
114131
*/
115132
public class RealmAny {
116133
@Nonnull

realm/realm-library/src/main/java/io/realm/RealmQuery.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,9 +2193,11 @@ public RealmQuery<E> containsEntry(String fieldName, Map.Entry<String, ?> entry)
21932193
* Calculates the sum of a given field.
21942194
*
21952195
* @param fieldName the field to sum. Only number and RealmAny fields are supported.
2196-
* @return the sum of fields of the matching objects. If no objects exist or they all have {@code null} as the value
2197-
* for the given field, {@code 0} will be returned. When computing the sum, objects with {@code null} values
2198-
* are ignored. When applied to a RealmAny field, the returning type will be {@code Decimal128}.
2196+
* @return the sum of fields of the matching objects. If no objects exist or they all have
2197+
* {@code null} as the value for the given field, {@code 0} will be returned. When computing the
2198+
* sum, objects with {@code null} values are ignored. When applied to a RealmAny field, only
2199+
* numeric values will be summed up (Byte/Integer/Integer/Long/Float/Double/Decimal128) and the
2200+
* returning type will be {@code Decimal128}.
21992201
* @throws java.lang.IllegalArgumentException if the field is not a number type.
22002202
* @throws RealmException if called from the UI thread after opting out via {@link RealmConfiguration.Builder#allowQueriesOnUiThread(boolean)}.
22012203
*/
@@ -2339,12 +2341,12 @@ public Date minimumDate(String fieldName) {
23392341
}
23402342

23412343
/**
2342-
* Finds the minimum value of a field.
2344+
* Finds the minimum value of a {@link RealmAny} field.
23432345
*
2344-
* @param fieldName the field name
2345-
* @return if no objects exist or they all have {@code null} as the value for the given RealmAny field, {@code null}
2346+
* @param fieldName the field containing a RealmAny value.
2347+
* @return if no objects exist or they all have {@code null} as the value for the given RealmAny field, {@link RealmAny.Type#NULL}
23462348
* will be returned. Otherwise the minimum RealmAny is returned. When determining the minimum RealmAny, objects with
2347-
* {@code null} values are ignored.
2349+
* {@code null} values are ignored. See the {@link RealmAny} documentation for more details on how RealmAny values are compared.
23482350
* @throws java.lang.UnsupportedOperationException if the query is not valid ("syntax error").
23492351
* @throws RealmException if called from the UI thread after opting out via {@link RealmConfiguration.Builder#allowQueriesOnUiThread(boolean)}.
23502352
*/
@@ -2406,15 +2408,14 @@ public Date maximumDate(String fieldName) {
24062408
}
24072409

24082410
/**
2409-
* Finds the maximum value of a field.
2411+
* Finds the maximum value of a {@link RealmAny} field.
24102412
*
2411-
* @param fieldName the field name.
2412-
* @return if no objects exist or they all have {@code null} as the value for the given RealmAny field, {@code null}
2413+
* @param fieldName the field containing a RealmAny value.
2414+
* @return if no objects exist or they all have {@code null} as the value for the given RealmAny field, {@link RealmAny.Type#NULL}
24132415
* will be returned. Otherwise the maximum RealmAny is returned. When determining the maximum RealmAny, objects with
2414-
* {@code null} values are ignored.
2416+
* {@code null} values are ignored. See the {@link RealmAny} documentation for more details on how RealmAny values are compared.
24152417
* @throws java.lang.UnsupportedOperationException if the query is not valid ("syntax error").
24162418
*/
2417-
@Nullable
24182419
public RealmAny maxRealmAny(String fieldName) {
24192420
realm.checkIfValid();
24202421
realm.checkAllowQueriesOnUiThread();

0 commit comments

Comments
 (0)