Search in sources :

Example 6 with AnnotationIndexTypes

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

the class RealmQueryTests method distinct.

@Test
public void distinct() {
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    RealmResults<AnnotationIndexTypes> distinctBool = realm.where(AnnotationIndexTypes.class).distinct(AnnotationIndexTypes.FIELD_INDEX_BOOL);
    assertEquals(2, distinctBool.size());
    for (String field : new String[] { AnnotationIndexTypes.FIELD_INDEX_LONG, AnnotationIndexTypes.FIELD_INDEX_DATE, AnnotationIndexTypes.FIELD_INDEX_STRING }) {
        RealmResults<AnnotationIndexTypes> distinct = realm.where(AnnotationIndexTypes.class).distinct(field);
        assertEquals(field, numberOfBlocks, distinct.size());
    }
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) Test(org.junit.Test)

Example 7 with AnnotationIndexTypes

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

the class RealmQueryTests method populateForDistinct.

// RealmQuery.distinct(): requires indexing, and type = boolean, integer, date, string.
private void populateForDistinct(Realm realm, long numberOfBlocks, long numberOfObjects, boolean withNull) {
    realm.beginTransaction();
    for (int i = 0; i < numberOfObjects * numberOfBlocks; i++) {
        for (int j = 0; j < numberOfBlocks; j++) {
            AnnotationIndexTypes obj = realm.createObject(AnnotationIndexTypes.class);
            obj.setIndexBoolean(j % 2 == 0);
            obj.setIndexLong(j);
            obj.setIndexDate(withNull ? null : new Date(1000 * j));
            obj.setIndexString(withNull ? null : "Test " + j);
            obj.setNotIndexBoolean(j % 2 == 0);
            obj.setNotIndexLong(j);
            obj.setNotIndexDate(withNull ? null : new Date(1000 * j));
            obj.setNotIndexString(withNull ? null : "Test " + j);
            obj.setFieldObject(obj);
        }
    }
    realm.commitTransaction();
}
Also used : Date(java.util.Date) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes)

Example 8 with AnnotationIndexTypes

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

the class RealmQueryTests method distinct_withNullValues.

@Test
public void distinct_withNullValues() {
    final long numberOfBlocks = 25;
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, true);
    for (String field : new String[] { AnnotationIndexTypes.FIELD_INDEX_DATE, AnnotationIndexTypes.FIELD_INDEX_STRING }) {
        RealmResults<AnnotationIndexTypes> distinct = realm.where(AnnotationIndexTypes.class).distinct(field);
        assertEquals(field, 1, distinct.size());
    }
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) Test(org.junit.Test)

Example 9 with AnnotationIndexTypes

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

the class RealmQueryTests method distinctAsync.

@Test
@RunTestInLooperThread
public void distinctAsync() throws Throwable {
    final AtomicInteger changeListenerCalled = new AtomicInteger(4);
    final Realm realm = looperThread.realm;
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    final RealmResults<AnnotationIndexTypes> distinctBool = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_BOOL);
    final RealmResults<AnnotationIndexTypes> distinctLong = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_LONG);
    final RealmResults<AnnotationIndexTypes> distinctDate = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_DATE);
    final RealmResults<AnnotationIndexTypes> distinctString = realm.where(AnnotationIndexTypes.class).distinctAsync(AnnotationIndexTypes.FIELD_INDEX_STRING);
    assertFalse(distinctBool.isLoaded());
    assertTrue(distinctBool.isValid());
    assertTrue(distinctBool.isEmpty());
    assertFalse(distinctLong.isLoaded());
    assertTrue(distinctLong.isValid());
    assertTrue(distinctLong.isEmpty());
    assertFalse(distinctDate.isLoaded());
    assertTrue(distinctDate.isValid());
    assertTrue(distinctDate.isEmpty());
    assertFalse(distinctString.isLoaded());
    assertTrue(distinctString.isValid());
    assertTrue(distinctString.isEmpty());
    final Runnable endTest = new Runnable() {

        @Override
        public void run() {
            if (changeListenerCalled.decrementAndGet() == 0) {
                looperThread.testComplete();
            }
        }
    };
    looperThread.keepStrongReference.add(distinctBool);
    looperThread.keepStrongReference.add(distinctLong);
    looperThread.keepStrongReference.add(distinctDate);
    looperThread.keepStrongReference.add(distinctString);
    distinctBool.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(2, distinctBool.size());
            endTest.run();
        }
    });
    distinctLong.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctLong.size());
            endTest.run();
        }
    });
    distinctDate.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctDate.size());
            endTest.run();
        }
    });
    distinctString.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            assertEquals(numberOfBlocks, distinctString.size());
            endTest.run();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 10 with AnnotationIndexTypes

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

the class RealmAsyncQueryTests method populateForDistinct.

private void populateForDistinct(Realm realm, long numberOfBlocks, long numberOfObjects, boolean withNull) {
    realm.beginTransaction();
    for (int i = 0; i < numberOfObjects * numberOfBlocks; i++) {
        for (int j = 0; j < numberOfBlocks; j++) {
            AnnotationIndexTypes obj = realm.createObject(AnnotationIndexTypes.class);
            obj.setIndexBoolean(j % 2 == 0);
            obj.setIndexLong(j);
            obj.setIndexDate(withNull ? null : new Date(1000 * j));
            obj.setIndexString(withNull ? null : "Test " + j);
            obj.setNotIndexBoolean(j % 2 == 0);
            obj.setNotIndexLong(j);
            obj.setNotIndexDate(withNull ? null : new Date(1000 * j));
            obj.setNotIndexString(withNull ? null : "Test " + j);
        }
    }
    realm.commitTransaction();
}
Also used : Date(java.util.Date) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes)

Aggregations

AnnotationIndexTypes (io.realm.entities.AnnotationIndexTypes)12 Test (org.junit.Test)8 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Date (java.util.Date)3 UiThreadTest (android.support.test.annotation.UiThreadTest)2 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)2 AllTypes (io.realm.entities.AllTypes)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 CountDownLatch (java.util.concurrent.CountDownLatch)1