Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findFirst(int), findLast(int) and findRandom(int) #6778

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Owner> 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<Owner> 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<Owner> owners = realm.where(Owner.class).findRandom(2);
assertEquals(2, subQueryResult.size());
}

@Test
public void georgian() {
String words[] = {"მონაცემთა ბაზა", "მიწისქვეშა გადასასვლელი", "რუსთაველის გამზირი",
Expand Down
50 changes: 50 additions & 0 deletions realm/realm-library/src/main/java/io/realm/RealmQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,56 @@ 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<E> 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<E> findLast(int limit) {
if (limit < 1) {
throw new IllegalArgumentException("Only positive numbers above 0 is allowed. Yours was: " + limit);
}

RealmResults<E> results = findAll();
results = results.subList(Math.max(results.size() - limit, 0), results.size());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.util.SubList cannot be cast to io.realm.RealmResults, this will throw ClassCastException at runtime

Collections.reverse(results);
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<E> findRandom(int limit) {
if (limit < 1) {
throw new IllegalArgumentException("Only positive numbers above 0 is allowed. Yours was: " + limit);
}

RealmResults<E> results = findAll();
Collections.shuffle(results);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shuffle is not supported by 'RealmResults' or 'OrderedRealmCollectionSnapshot', this will throw a UnsupportedOperationException at runtime.

results = results.subList(0, Math.min(limit, results.size()));
return results;
}

/**
* Finds all objects that fulfill the query conditions.
*
Expand Down