Search in sources :

Example 6 with AllJavaTypesUnsupportedTypes

use of io.realm.entities.AllJavaTypesUnsupportedTypes in project realm-java by realm.

the class RealmQueryTests method limit_withSortAndDistinct.

@Test
public void limit_withSortAndDistinct() {
    // The order of operators matter when using limit()
    // If applying sort/distinct without limit, any order will result in the same query result.
    realm.beginTransaction();
    // Root object;
    RealmList<AllJavaTypesUnsupportedTypes> list = realm.createObject(AllJavaTypesUnsupportedTypes.class, -1).getFieldList();
    for (int i = 0; i < 5; i++) {
        AllJavaTypesUnsupportedTypes obj = realm.createObject(AllJavaTypesUnsupportedTypes.class, i);
        obj.setFieldLong(i);
        list.add(obj);
    }
    realm.commitTransaction();
    RealmResults<AllJavaTypesUnsupportedTypes> results = list.where().sort(AllJavaTypesUnsupportedTypes.FIELD_LONG, // [4, 4, 3, 3, 2, 2, 1, 1, 0, 0]
    Sort.DESCENDING).distinct(// [4, 3, 2, 1, 0]
    AllJavaTypesUnsupportedTypes.FIELD_LONG).limit(// [4, 3]
    2).findAll();
    assertEquals(2, results.size());
    assertEquals(4, results.first().getFieldLong());
    assertEquals(3, results.last().getFieldLong());
    results = list.where().limit(// [0, 1]
    2).distinct(// [ 0, 1]
    AllJavaTypesUnsupportedTypes.FIELD_LONG).sort(AllJavaTypesUnsupportedTypes.FIELD_LONG, // [1, 0]
    Sort.DESCENDING).findAll();
    assertEquals(2, results.size());
    assertEquals(1, results.first().getFieldLong());
    assertEquals(0, results.last().getFieldLong());
    results = list.where().distinct(// [ 0, 1, 2, 3, 4]
    AllJavaTypesUnsupportedTypes.FIELD_LONG).limit(// [0, 1]
    2).sort(AllJavaTypesUnsupportedTypes.FIELD_LONG, // [1, 0]
    Sort.DESCENDING).findAll();
    assertEquals(2, results.size());
    assertEquals(1, results.first().getFieldLong());
    assertEquals(0, results.last().getFieldLong());
}
Also used : AllJavaTypesUnsupportedTypes(io.realm.entities.AllJavaTypesUnsupportedTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 7 with AllJavaTypesUnsupportedTypes

use of io.realm.entities.AllJavaTypesUnsupportedTypes in project realm-java by realm.

the class RealmQueryTests method populateForDistinctInvalidTypesLinked.

private void populateForDistinctInvalidTypesLinked(Realm realm) {
    realm.beginTransaction();
    AllJavaTypesUnsupportedTypes notEmpty = new AllJavaTypesUnsupportedTypes();
    notEmpty.setFieldBinary(new byte[] { 1, 2, 3 });
    notEmpty.setFieldObject(notEmpty);
    notEmpty.setFieldList(new RealmList<AllJavaTypesUnsupportedTypes>(notEmpty));
    realm.copyToRealm(notEmpty);
    realm.commitTransaction();
}
Also used : AllJavaTypesUnsupportedTypes(io.realm.entities.AllJavaTypesUnsupportedTypes)

Example 8 with AllJavaTypesUnsupportedTypes

use of io.realm.entities.AllJavaTypesUnsupportedTypes in project realm-java by realm.

the class QueryTests method createIsEmptyDataSet.

@SuppressWarnings("unchecked")
protected final void createIsEmptyDataSet(Realm realm) {
    realm.beginTransaction();
    AllJavaTypesUnsupportedTypes emptyValues = new AllJavaTypesUnsupportedTypes();
    emptyValues.setFieldId(1);
    emptyValues.setFieldString("");
    emptyValues.setFieldBinary(new byte[0]);
    emptyValues.setFieldObject(emptyValues);
    emptyValues.setFieldList(new RealmList<>());
    emptyValues.setColumnRealmDictionary(new RealmDictionary<>());
    AllJavaTypesUnsupportedTypes emptyValuesManaged = realm.copyToRealm(emptyValues);
    AllJavaTypesUnsupportedTypes nonEmpty = new AllJavaTypesUnsupportedTypes();
    nonEmpty.setFieldId(2);
    nonEmpty.setFieldString("Foo");
    nonEmpty.setFieldBinary(new byte[] { 1, 2, 3 });
    nonEmpty.setFieldObject(nonEmpty);
    nonEmpty.setFieldList(new RealmList<>(emptyValuesManaged));
    AllJavaTypesUnsupportedTypes nonEmptyManaged = realm.copyToRealmOrUpdate(nonEmpty);
    AllJavaTypesUnsupportedTypes emptyValues2 = new AllJavaTypesUnsupportedTypes();
    emptyValues2.setFieldId(3);
    emptyValues2.setFieldString("");
    emptyValues2.setFieldBinary(new byte[0]);
    emptyValues2.setFieldObject(null);
    emptyValues2.setFieldList(new RealmList<>(nonEmptyManaged));
    emptyValues2.setColumnRealmDictionary(new RealmDictionary<>());
    realm.copyToRealm(emptyValues2);
    realm.commitTransaction();
}
Also used : AllJavaTypesUnsupportedTypes(io.realm.entities.AllJavaTypesUnsupportedTypes)

Aggregations

AllJavaTypesUnsupportedTypes (io.realm.entities.AllJavaTypesUnsupportedTypes)8 Test (org.junit.Test)4 UiThreadTest (androidx.test.annotation.UiThreadTest)2 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Decimal128 (org.bson.types.Decimal128)1 ObjectId (org.bson.types.ObjectId)1