From aaf490eb2425838a036d757d45b0d1b6b78a5ddd Mon Sep 17 00:00:00 2001 From: Wen Chia Yang Date: Mon, 16 Mar 2020 17:17:09 -0700 Subject: [PATCH 1/4] add findFirst(n) in RealmQuery --- .../src/main/java/io/realm/RealmQuery.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/realm/realm-library/src/main/java/io/realm/RealmQuery.java b/realm/realm-library/src/main/java/io/realm/RealmQuery.java index 3f57236013..ccadbb61e8 100644 --- a/realm/realm-library/src/main/java/io/realm/RealmQuery.java +++ b/realm/realm-library/src/main/java/io/realm/RealmQuery.java @@ -1765,6 +1765,18 @@ public long count() { return lazyFindAll().size(); } + /** + * Finds first n objects that fulfill the query conditions. + * + * @return a {@link io.realm.RealmResults} containing objects. If no objects match the condition, a list with zero + * objects is returned. + * @see io.realm.RealmResults + */ + @SuppressWarnings("unchecked") + public RealmResults findFirst(int limit) { + return limit(limit).findAll(); + } + /** * Finds all objects that fulfill the query conditions. * From 2ffd09e971dabd35c78f45ec9fc1d5afc55a4ceb Mon Sep 17 00:00:00 2001 From: Wen Chia Yang Date: Mon, 16 Mar 2020 18:08:55 -0700 Subject: [PATCH 2/4] add findLast(n) in RealmQuery --- .../src/main/java/io/realm/RealmQuery.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/realm/realm-library/src/main/java/io/realm/RealmQuery.java b/realm/realm-library/src/main/java/io/realm/RealmQuery.java index ccadbb61e8..ffc3cf610f 100644 --- a/realm/realm-library/src/main/java/io/realm/RealmQuery.java +++ b/realm/realm-library/src/main/java/io/realm/RealmQuery.java @@ -1777,6 +1777,25 @@ public RealmResults findFirst(int limit) { return limit(limit).findAll(); } + /** + * Finds last n objects that fulfill the query conditions. + * + * @return a {@link io.realm.RealmResults} containing objects. If no objects match the condition, a list with zero + * objects is returned. + * @see io.realm.RealmResults + */ + @SuppressWarnings("unchecked") + public RealmResults findLast(int limit) { + if (limit < 1) { + throw new IllegalArgumentException("Only positive numbers above 0 is allowed. Yours was: " + limit); + } + + RealmResults results = findAll(); + results = results.subList(Math.max(results.size() - limit, 0), results.size()); + Collections.reverse(results); + return results; + } + /** * Finds all objects that fulfill the query conditions. * From 6b6dec7fcce56f36f897c405badd40d88288e759 Mon Sep 17 00:00:00 2001 From: Wen Chia Yang Date: Mon, 16 Mar 2020 18:21:32 -0700 Subject: [PATCH 3/4] add findRandom(n) to RealmQuery --- .../src/main/java/io/realm/RealmQuery.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/realm/realm-library/src/main/java/io/realm/RealmQuery.java b/realm/realm-library/src/main/java/io/realm/RealmQuery.java index ffc3cf610f..424b067d47 100644 --- a/realm/realm-library/src/main/java/io/realm/RealmQuery.java +++ b/realm/realm-library/src/main/java/io/realm/RealmQuery.java @@ -1796,6 +1796,25 @@ public RealmResults findLast(int limit) { return results; } + /** + * Finds random n objects that fulfill the query conditions. + * + * @return a {@link io.realm.RealmResults} containing objects. If no objects match the condition, a list with zero + * objects is returned. + * @see io.realm.RealmResults + */ + @SuppressWarnings("unchecked") + public RealmResults findRandom(int limit) { + if (limit < 1) { + throw new IllegalArgumentException("Only positive numbers above 0 is allowed. Yours was: " + limit); + } + + RealmResults results = findAll(); + Collections.shuffle(results); + results = results.subList(0, Math.min(limit, results.size())); + return results; + } + /** * Finds all objects that fulfill the query conditions. * From 121dcf38eab381ac04399e78d8174268f93109c5 Mon Sep 17 00:00:00 2001 From: zihuaweng Date: Mon, 16 Mar 2020 18:55:31 -0700 Subject: [PATCH 4/4] Add findFirst_limit, findLast_limit, findRandom_limit test case --- .../java/io/realm/RealmQueryTests.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java b/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java index a29e98a8c5..5fae8df1b9 100644 --- a/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java +++ b/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java @@ -1372,6 +1372,72 @@ public void findFirst_subQuery_withSorting() { assertEquals("Bella", dog.getName()); } + @Test + public void findFirst_limit() { + realm.beginTransaction(); + + Owner owner1 = realm.createObject(Owner.class); + owner1.setName("Owner 1"); + + Owner owner2 = realm.createObject(Owner.class); + owner2.setName("Owner 2"); + + Owner owner3 = realm.createObject(Owner.class); + owner3.setName("Owner 3"); + + Owner owner4 = realm.createObject(Owner.class); + owner4.setName("Owner 4"); + + realm.commitTransaction(); + + RealmResults owners = realm.where(Owner.class).findFirst(2); + assertEquals(2, subQueryResult.size()); + } + + @Test + public void findLast_limit() { + realm.beginTransaction(); + + Owner owner1 = realm.createObject(Owner.class); + owner1.setName("Owner 1"); + + Owner owner2 = realm.createObject(Owner.class); + owner2.setName("Owner 2"); + + Owner owner3 = realm.createObject(Owner.class); + owner3.setName("Owner 3"); + + Owner owner4 = realm.createObject(Owner.class); + owner4.setName("Owner 4"); + + realm.commitTransaction(); + + RealmResults owners = realm.where(Owner.class).findLast(2); + assertEquals(2, subQueryResult.size()); + } + + @Test + public void findRandom_limit() { + realm.beginTransaction(); + + Owner owner1 = realm.createObject(Owner.class); + owner1.setName("Owner 1"); + + Owner owner2 = realm.createObject(Owner.class); + owner2.setName("Owner 2"); + + Owner owner3 = realm.createObject(Owner.class); + owner3.setName("Owner 3"); + + Owner owner4 = realm.createObject(Owner.class); + owner4.setName("Owner 4"); + + realm.commitTransaction(); + + RealmResults owners = realm.where(Owner.class).findRandom(2); + assertEquals(2, subQueryResult.size()); + } + @Test public void georgian() { String words[] = {"მონაცემთა ბაზა", "მიწისქვეშა გადასასვლელი", "რუსთაველის გამზირი",